Skip to content

Commit

Permalink
Do not call actions from state changes if relevant state fields have …
Browse files Browse the repository at this point in the history
…not changed

Update user bindings update to be slightly later
Update package to require latest Xcode but keep language mode to 5
Closes #56
Closes #58
  • Loading branch information
LeoNatan committed Oct 5, 2024
1 parent ee110c1 commit cd5fbb2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.9
// swift-tools-version:6.0

import PackageDescription

Expand Down Expand Up @@ -30,6 +30,9 @@ let package = Package(
dependencies: [
.product(name: "LNSwiftUIUtils", package: "LNSwiftUIUtils"),
.product(name: "LNPopupController-Static", package: "LNPopupController")
],
swiftSettings: [
.swiftLanguageMode(.v5)
])
]
)
32 changes: 26 additions & 6 deletions Sources/LNPopupUI/Private/LNPopupProxyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ internal class LNPopupProxyViewController<Content, PopupContent> : UIHostingCont
return LNPopupImplicitAnimationController(withWindow: view.window!)
}()

var currentSmallPopupState: LNPopupSmallState = (false, nil)
func handlePopupState(_ state: LNPopupState<PopupContent>) {
currentPopupState = state

Expand Down Expand Up @@ -259,14 +260,21 @@ internal class LNPopupProxyViewController<Content, PopupContent> : UIHostingCont
self?.implicitAnimationController.pop()
}

if self.currentPopupState.isBarPresented.wrappedValue == true {
let newSmallState = self.currentPopupState.smallState
guard self.currentSmallPopupState != newSmallState else {
return
}

self.currentSmallPopupState = newSmallState

if newSmallState.isBarPresented == true {
popupContentHandler()

let targetPresentationState: UIViewController.PopupPresentationState = UIViewController.PopupPresentationState(rawValue: target.value(forKeyPath: "ln_popupController.popupControllerTargetState") as! Int)!

if targetPresentationState.rawValue >= UIViewController.PopupPresentationState.barPresented.rawValue {
if let isPopupOpen = self.currentPopupState.isPopupOpen {
if isPopupOpen.wrappedValue == true {
if let isPopupOpen = newSmallState.isPopupOpen {
if isPopupOpen {
target.openPopup(animated: true) {
endImplicitAnims()
}
Expand Down Expand Up @@ -330,12 +338,14 @@ internal class LNPopupProxyViewController<Content, PopupContent> : UIHostingCont
//MARK: LNPopupPresentationDelegate

func popupPresentationControllerWillPresentPopupBar(_ popupPresentationController: UIViewController, animated: Bool) {
currentSmallPopupState = (true, currentSmallPopupState.isPopupOpen)
DispatchQueue.main.async {
self.currentPopupState?.isBarPresented.wrappedValue = true
}
}

func popupPresentationControllerWillDismissPopupBar(_ popupPresentationController: UIViewController, animated: Bool) {
currentSmallPopupState = (false, currentSmallPopupState.isPopupOpen)
DispatchQueue.main.async {
self.currentPopupState?.isBarPresented.wrappedValue = false
}
Expand All @@ -347,15 +357,25 @@ internal class LNPopupProxyViewController<Content, PopupContent> : UIHostingCont
}

func popupPresentationController(_ popupPresentationController: UIViewController, willOpenPopupWithContentController popupContentController: UIViewController, animated: Bool) {
currentPopupState?.isPopupOpen?.wrappedValue = true
if currentPopupState.isPopupOpen != nil {
currentSmallPopupState = (currentSmallPopupState.isBarPresented, true)
}
DispatchQueue.main.async {
self.currentPopupState?.isPopupOpen?.wrappedValue = true
}
}

func popupPresentationController(_ popupPresentationController: UIViewController, didOpenPopupWithContentController popupContentController: UIViewController, animated: Bool) {
currentPopupState?.onOpen?()
}

func popupPresentationController(_ popupPresentationController: UIViewController, willClosePopupWithContentController popupContentController: UIViewController, animated: Bool) {
currentPopupState?.isPopupOpen?.wrappedValue = false
if currentPopupState.isPopupOpen != nil {
currentSmallPopupState = (currentSmallPopupState.isBarPresented, false)
}
DispatchQueue.main.async {
self.currentPopupState?.isPopupOpen?.wrappedValue = false
}
}

func popupPresentationController(_ popupPresentationController: UIViewController, didClosePopupWithContentController popupContentController: UIViewController, animated: Bool) {
Expand Down
6 changes: 6 additions & 0 deletions Sources/LNPopupUI/Private/LNPopupState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal struct LNPopupBarCustomView {
let popupBarCustomBarView: AnyView
}

typealias LNPopupSmallState = (isBarPresented: Bool, isPopupOpen: Bool?)

internal struct LNPopupState<PopupContent: View> {
var isBarPresented: Binding<Bool>
var isPopupOpen: Binding<Bool>?
Expand Down Expand Up @@ -47,4 +49,8 @@ internal struct LNPopupState<PopupContent: View> {
let onClose: (() -> Void)?
let barCustomizer: LNPopupEnvironmentConsumer<((LNPopupBar) -> Void)>?
let contentViewCustomizer: LNPopupEnvironmentConsumer<((LNPopupContentView) -> Void)>?

var smallState: LNPopupSmallState {
return (isBarPresented.wrappedValue, isPopupOpen?.wrappedValue)
}
}

0 comments on commit cd5fbb2

Please sign in to comment.