Skip to content

Commit

Permalink
Convenience dismiss & canBeDismissed handler. Fix iOS 12 crash (#22)
Browse files Browse the repository at this point in the history
* Provide ability to control dismiss behavior. Also add `dismissCompletion`

* Fix iOS 12 crash
  • Loading branch information
mikhailmaslo authored Jul 13, 2023
1 parent b847e09 commit 8e1883e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,26 @@ public final class DefaultBottomSheetModalDismissalHandler: BottomSheetModalDism
// MARK: - Private properties

private weak var presentingViewController: UIViewController?
private let _canBeDismissed: () -> Bool
private let dismissCompletion: (() -> Void)?

// MARK: - Init

init(
presentingViewController: UIViewController?,
canBeDismissed: @escaping (() -> Bool),
dismissCompletion: (() -> Void)?
) {
self.presentingViewController = presentingViewController
self._canBeDismissed = canBeDismissed
self.dismissCompletion = dismissCompletion
}

// MARK: - BottomSheetModalDismissalHandler

public let canBeDismissed = true
public var canBeDismissed: Bool {
_canBeDismissed()
}

public func performDismissal(animated: Bool) {
presentingViewController?.presentedViewController?.dismiss(animated: animated, completion: dismissCompletion)
Expand All @@ -76,14 +81,20 @@ public extension UIViewController {

private static var bottomSheetTransitionDelegateKey: UInt8 = 0

func presentBottomSheet(viewController: UIViewController, configuration: BottomSheetConfiguration) {
func presentBottomSheet(
viewController: UIViewController,
configuration: BottomSheetConfiguration,
canBeDismissed: @escaping (() -> Bool) = { true },
dismissCompletion: (() -> Void)? = nil
) {
weak var presentingViewController = self
weak var currentBottomSheetTransitionDelegate: UIViewControllerTransitioningDelegate?
let presentationControllerFactory = DefaultBottomSheetPresentationControllerFactory(configuration: configuration) {
DefaultBottomSheetModalDismissalHandler(presentingViewController: presentingViewController) {
DefaultBottomSheetModalDismissalHandler(presentingViewController: presentingViewController, canBeDismissed: canBeDismissed) {
if currentBottomSheetTransitionDelegate === presentingViewController?.bottomSheetTransitionDelegate {
presentingViewController?.bottomSheetTransitionDelegate = nil
}
dismissCompletion?()
}
}
bottomSheetTransitionDelegate = BottomSheetTransitioningDelegate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public final class BottomSheetNavigationController: UINavigationController {
super.init(rootViewController: rootViewController)
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
self.configuration = .default
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
Expand Down

0 comments on commit 8e1883e

Please sign in to comment.