Skip to content

Commit

Permalink
SwiftUI Updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
iDevelopper committed Jan 15, 2024
1 parent 872fc5a commit 4016ee6
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ extension PBPopupPresentationStyle
@objc optional func popupController(_ popupController: PBPopupController, willOpen popupContentViewController: UIViewController)

/**
Called just before the popup content view is about to be closed by typing the popup close button or dragging the popup content view..
Called just before the popup content view is about to be closed by typing the popup close button or dragging the popup content view.
- Parameter popupController: The popup controller object.
- Parameter popupContentViewController: The popup content view controller object.
Expand Down Expand Up @@ -468,14 +468,7 @@ extension PBPopupPresentationStyle
return vc.bottomBar.frame.height
}
}

private func PBLogIfEnabled<T>( _ object: @escaping @autoclosure() -> T, error: Bool = false) {
let PBLogEnabled: Bool = false
if PBLogEnabled {
PBLog(object, error: error)
}
}


private var disableInteractiveTransitioning = false

internal var popupPresentationController: PBPopupPresentationController?
Expand Down Expand Up @@ -995,6 +988,7 @@ extension PBPopupPresentationStyle
// TODO: SwiftUI
if NSStringFromClass(type(of: vc.popupContentViewController).self).contains("PBPopupUIContentController") {
if (vc.popupContentView.superview != nil) {
vc.popupContentViewController.view.removeFromSuperview()
vc.popupContentView.removeFromSuperview()
}
}
Expand Down Expand Up @@ -1117,8 +1111,7 @@ extension PBPopupPresentationStyle
if vc.popupBarIsHidden || vc.popupBar.isFloating {
frame.origin.y = self.wantsAdditionalSafeAreaInsetTop ? vc.view.bounds.minY : vc.view.bounds.height
}

PBLogIfEnabled("\(frame)")
PBLog("\(frame)")
return frame
}

Expand Down Expand Up @@ -1152,8 +1145,7 @@ extension PBPopupPresentationStyle
frame.size.width -= abs(vc.view.frame.minX)
}
}

PBLogIfEnabled("\(frame)")
PBLog("\(frame)")
return frame
}
}
Expand Down Expand Up @@ -1242,6 +1234,7 @@ extension PBPopupController: PBPopupInteractivePresentationDelegate
// TODO: SwiftUI
if NSStringFromClass(type(of: vc.popupContentViewController).self).contains("PBPopupUIContentController") {
if (vc.popupContentView.superview != nil) {
vc.popupContentViewController.view.removeFromSuperview()
vc.popupContentView.removeFromSuperview()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ internal struct PBPopupBarCustomizer: EnvironmentKey {
static let defaultValue: ((PBPopupBar) -> Void)? = nil
}

internal struct PBPopupContentViewCustomizer: EnvironmentKey {
static let defaultValue: ((PBPopupContentView) -> Void)? = nil
}

@available(iOS 14.0, *)
internal extension EnvironmentValues {
var popupCloseButtonStyle: PBPopupCloseButtonStyle {
Expand Down Expand Up @@ -227,4 +231,9 @@ internal extension EnvironmentValues {
get { self[PBPopupBarCustomizer.self] }
set { self[PBPopupBarCustomizer.self] = newValue }
}

var popupContentViewCustomizer: ((PBPopupContentView) -> Void)? {
get { self[PBPopupContentViewCustomizer.self] }
set { self[PBPopupContentViewCustomizer.self] = newValue }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ internal struct PBPopupTrailingBarItemsPreferenceKey: PBPopupNullablePreferenceK
typealias Value = PBPopupAnyViewWrapper?
}

@available(iOS 14.0, *)
internal struct PBPopupContentBackgroundPreferenceKey: PBPopupNullablePreferenceKey {
typealias Value = PBPopupAnyViewWrapper?
}

@available(iOS 14.0, *)
internal protocol PBPopupNullablePreferenceKey : PreferenceKey {
static var defaultValue: Value? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont
//readyForHandling = true
}

deinit {
print("Deinit: \(self)")
}

fileprivate var target: UIViewController {
//print("self.children: \(self.children)")
return children.first ?? self
Expand Down Expand Up @@ -100,9 +104,11 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont
}
}

/*
fileprivate func cast<T>(value: Any, to type: T) -> PBPopupUIContentController<T> where T: View {
return value as! PBPopupUIContentController<T>
}
*/

func viewHandler(_ state: PBPopupState<PopupContent>) -> (() -> Void) {
let view = {
Expand Down Expand Up @@ -171,12 +177,22 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont
self.createOrUpdateHostingControllerForAnyView(&self.trailingBarItemsController, view: anyView, barButtonItem: &self.trailingBarButtonItem, targetBarButtons: { popupBar.rightBarButtonItems = $0 }, leadSpacing: false, trailingSpacing: false)
}
}
.onPreferenceChange(PBPopupContentBackgroundPreferenceKey.self) { [weak self] view in
if let self = self, let anyView = view?.anyView, let popupViewController = self.popupViewController as? PBPopupUIContentController {
let hostingChild = UIHostingController(rootView: anyView)
if let swiftuiView = hostingChild.view {
popupViewController.swiftuiBackgroundView = swiftuiView
}
}
}
}()
return {
if self.popupViewController == nil {
self.popupViewController = PBPopupUIContentController(rootView: view)
//self.popupViewController = PBPopupUIContentController(rootView: view)
self.popupViewController = PBPopupUIContentController(anyView: AnyView(view))
} else {
self.cast(value: self.popupViewController!, to: view.self).rootView = view
//self.cast(value: self.popupViewController!, to: view.self).rootView = view
(self.popupViewController as! PBPopupUIContentController).setAnyView(AnyView(view))
}
}
}
Expand Down Expand Up @@ -239,6 +255,7 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont
}

self.currentPopupState.barCustomizer?(self.target.popupBar)
self.currentPopupState.contentViewCustomizer?(self.target.popupContentView)

self.target.popupController.delegate = self

Expand All @@ -247,18 +264,23 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont

let presentationState = self.target.popupController.popupPresentationState
if presentationState == .hidden || presentationState == .dismissed {
self.target.presentPopupBar(withPopupContentViewController: self.popupViewController, openPopup: self.currentPopupState.isOpen, animated: animated, completion: nil)
if let isOpen = self.currentPopupState.isOpen {
self.target.presentPopupBar(withPopupContentViewController: self.popupViewController, openPopup: isOpen.wrappedValue, animated: animated, completion: nil)
}
}
else {
if self.currentPopupState.isOpen == true {
self.target.openPopup(animated: true, completion: nil)
}
else {
self.target.closePopup(animated: true, completion: nil)
}
if self.currentPopupState.isHidden == true {
if !self.target.popupBarIsHidden {
self.target.hidePopupBar(animated: true)
if presentationState == .transitioning { return }
if let isOpen = self.currentPopupState.isOpen {
if isOpen.wrappedValue == true {
self.target.openPopup(animated: true, completion: nil)
}
else {
self.target.closePopup(animated: true, completion: nil)
}
if let isHidden = self.currentPopupState.isHidden, isHidden.wrappedValue == true {
if !self.target.popupBarIsHidden {
self.target.hidePopupBar(animated: true)
}
}
}
else {
Expand All @@ -268,8 +290,10 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont
}
}
} else {
print("state: \(self.target.popupController.popupPresentationState.description)")
self.target.dismissPopupBar(animated: animated, completion: nil)
//print("state: \(self.target.popupController.popupPresentationState.description)")
if self.target.popupController.popupPresentationState != .transitioning {
self.target.dismissPopupBar(animated: animated, completion: nil)
}
}
}
if readyForHandling {
Expand Down Expand Up @@ -297,7 +321,6 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont

func popupController(_ popupController: PBPopupController, didPresent popupBar: PBPopupBar) {
currentPopupState?.isPresented = true

currentPopupState?.onPresent?()
}

Expand All @@ -307,27 +330,40 @@ internal class PBPopupProxyViewController<Content, PopupContent> : UIHostingCont

func popupController(_ popupController: PBPopupController, didDismiss popupBar: PBPopupBar) {
currentPopupState?.isPresented = false

currentPopupState?.onDismiss?()
}

func popupController(_ popupController: PBPopupController, shouldOpen popupContentViewController: UIViewController) -> Bool {
currentPopupState?.shouldOpen?() ?? true
}

func popupController(_ popupController: PBPopupController, willOpen popupContentViewController: UIViewController) {
currentPopupState?.willOpen?()
}

func popupController(_ popupController: PBPopupController, didOpen popupContentViewController: UIViewController) {
currentPopupState?.isOpen = true

currentPopupState?.isOpen?.wrappedValue = true
currentPopupState?.onOpen?()
}

func popupController(_ popupController: PBPopupController, shouldClose popupContentViewController: UIViewController) -> Bool {
currentPopupState?.shouldClose?() ?? true
}

func popupController(_ popupController: PBPopupController, willClose popupContentViewController: UIViewController) {
currentPopupState?.willClose?()
}

func popupController(_ popupController: PBPopupController, didClose popupContentViewController: UIViewController) {
currentPopupState?.isOpen = false

currentPopupState?.isOpen?.wrappedValue = false
currentPopupState?.onClose?()
}

func popupControllerTapGestureShouldBegin(_ popupController: PBPopupController, state: PBPopupPresentationState) -> Bool {
currentPopupState?.tapGestureShouldBegin?(state) ?? true
}

func popupControllerPanGestureShouldBegin(_ popupController: PBPopupController, state: PBPopupPresentationState) -> Bool {
currentPopupState?.panGestureShouldBegin?(state) ?? true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal struct PBPopupBarCustomView {
@available(iOS 14.0, *)
internal struct PBPopupState<PopupContent: View> {
@Binding var isPresented: Bool
@Binding var isOpen: Bool
@Binding var isHidden: Bool
var isOpen: Binding<Bool>?
var isHidden: Binding<Bool>?
let closeButtonStyle: PBPopupCloseButtonStyle
let isFloating: Bool
let popupBarStyle: PBPopupBarStyle
Expand All @@ -45,12 +45,17 @@ internal struct PBPopupState<PopupContent: View> {
let popupContent: (() -> PopupContent)?
let popupContentViewController: UIViewController?
let barCustomizer: ((PBPopupBar) -> Void)?
let contentViewCustomizer: ((PBPopupContentView) -> Void)?
let willPresent: (() -> Void)?
let onPresent: (() -> Void)?
let willDismiss: (() -> Void)?
let onDismiss: (() -> Void)?
let shouldOpen: (() -> Bool)?
let willOpen: (() -> Void)?
let onOpen: (() -> Void)?
let shouldClose: (() -> Bool)?
let willClose: (() -> Void)?
let onClose: (() -> Void)?
let tapGestureShouldBegin: ((PBPopupPresentationState) -> Bool)?
let panGestureShouldBegin: ((PBPopupPresentationState) -> Bool)?
}
Loading

0 comments on commit 4016ee6

Please sign in to comment.