Skip to content

Commit

Permalink
Safely call overlay container presentation controller callbacks
Browse files Browse the repository at this point in the history
FIX #62
  • Loading branch information
gaetanzanella committed Aug 11, 2020
1 parent bf53ea2 commit 116415a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ extension UIViewController {
}
return parent?.oc_findPresentationController(Controller.self)
}

func oc_findChildren<Controller: UIViewController>(_ type: Controller.Type) -> [Controller] {
if let controller = self as? Controller {
return [controller]
} else {
return children.map { $0.oc_findChildren(type) }.flatMap { $0 }
}
}
}
20 changes: 20 additions & 0 deletions Source/Classes/OverlayContainerPresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ import UIKit
/// the `OverlayContainerSheetPresentationController` class to see if it can be adapted to your presentation behavior.
open class OverlayContainerPresentationController: UIPresentationController {

// MARK: - Internal

open override func presentationTransitionWillBegin() {
super.presentationTransitionWillBegin()
findPresentedContainers().forEach { $0.overlayContainerPresentationTransitionWillBegin() }
}

open override func dismissalTransitionDidEnd(_ completed: Bool) {
super.dismissalTransitionDidEnd(completed)
findPresentedContainers().forEach { $0.overlayContainerDismissalTransitionDidEnd() }
}

// MARK: - Public

/// Tells the presentation controller when the user is about to start dragging the overlay view controller.
///
/// - parameter containerViewController: The container requesting this information.
Expand Down Expand Up @@ -65,4 +79,10 @@ open class OverlayContainerPresentationController: UIPresentationController {
open func overlayContainerViewController(_ containerViewController: OverlayContainerViewController,
willTranslateOverlay overlayViewController: UIViewController,
transitionCoordinator: OverlayContainerTransitionCoordinator) {}

// MARK: - Private

private func findPresentedContainers() -> [OverlayContainerViewController] {
presentedViewController.oc_findChildren(OverlayContainerViewController.self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ open class OverlayContainerSheetPresentationController: OverlayContainerPresenta
}

open override func presentationTransitionDidEnd(_ completed: Bool) {
super.presentationTransitionDidEnd(completed)
guard !completed else { return }
dimmingView?.removeFromSuperview()
tapGestureRecognizerView.removeFromSuperview()
Expand Down
17 changes: 16 additions & 1 deletion Source/Classes/OverlayContainerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ open class OverlayContainerViewController: UIViewController {
private var translationController: HeightConstraintOverlayTranslationController?
private var translationDrivers: [OverlayTranslationDriver] = []

// (gz) 2020-08-11 Uses to determine whether we can safely call `presentationController` or not.
// See issue #72
private var isPresentedInsideAnOverlayContainerPresentationController = false

// MARK: - Life Cycle

/// Creates an instance with the specified `style`.
Expand Down Expand Up @@ -153,6 +157,16 @@ open class OverlayContainerViewController: UIViewController {
performDeferredTranslations()
}

// MARK: - Internal

func overlayContainerPresentationTransitionWillBegin() {
isPresentedInsideAnOverlayContainerPresentationController = true
}

func overlayContainerDismissalTransitionDidEnd() {
isPresentedInsideAnOverlayContainerPresentationController = false
}

// MARK: - Public

/// Moves the overlay view controller to the specified notch.
Expand Down Expand Up @@ -311,7 +325,8 @@ open class OverlayContainerViewController: UIViewController {
extension OverlayContainerViewController: HeightConstraintOverlayTranslationControllerDelegate {

private var overlayPresentationController: OverlayContainerPresentationController? {
oc_findPresentationController(OverlayContainerPresentationController.self)
guard isPresentedInsideAnOverlayContainerPresentationController else { return nil }
return oc_findPresentationController(OverlayContainerPresentationController.self)
}

// MARK: - HeightOverlayTranslationControllerDelegate
Expand Down

0 comments on commit 116415a

Please sign in to comment.