From b151ca3f6e7190d106d7485e9a2c85121cc9c968 Mon Sep 17 00:00:00 2001 From: Almaz Ibragimov Date: Sun, 18 Jun 2023 17:55:37 +0300 Subject: [PATCH] Fixed bottom sheet dismission (#103) --- .../BottomSheetPresentedInteraction.swift | 4 +--- .../BottomSheetPresentationController.swift | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Sources/Addons/BottomSheet/Interaction/Interactions/BottomSheetPresentedInteraction.swift b/Sources/Addons/BottomSheet/Interaction/Interactions/BottomSheetPresentedInteraction.swift index a5c116b7..265a7d16 100644 --- a/Sources/Addons/BottomSheet/Interaction/Interactions/BottomSheetPresentedInteraction.swift +++ b/Sources/Addons/BottomSheet/Interaction/Interactions/BottomSheetPresentedInteraction.swift @@ -111,9 +111,7 @@ internal final class BottomSheetPresentedInteraction: BottomSheetInteraction { .transition .wantsInteractiveStart = !isGestureFinished - presentationController - .presentingViewController - .dismiss(animated: true) + presentationController.dismissPresentedViewController() } internal func start( diff --git a/Sources/Addons/BottomSheet/Presentation/BottomSheetPresentationController.swift b/Sources/Addons/BottomSheet/Presentation/BottomSheetPresentationController.swift index 8f9ffa24..965d5bb1 100644 --- a/Sources/Addons/BottomSheet/Presentation/BottomSheetPresentationController.swift +++ b/Sources/Addons/BottomSheet/Presentation/BottomSheetPresentationController.swift @@ -305,9 +305,15 @@ internal final class BottomSheetPresentationController: UIPresentationController } @objc private func onDimmingViewTapGesture() { - if delegate?.presentationControllerShouldDismiss?(self) ?? true { - presentingViewController.dismiss(animated: true) + guard state == .presented else { + return + } + + guard delegate?.presentationControllerShouldDismiss?(self) ?? true else { + return } + + dismissPresentedViewController() } @objc private func onTransitionViewPanGesture(recognizer: UIPanGestureRecognizer) { @@ -474,6 +480,16 @@ internal final class BottomSheetPresentationController: UIPresentationController animator.startAnimation() } + internal func dismissPresentedViewController() { + delegate?.presentationControllerWillDismiss?(self) + + presentingViewController.dismiss(animated: true) { + if self.state == .dismissed { + self.delegate?.presentationControllerDidDismiss?(self) + } + } + } + internal override func presentationTransitionWillBegin() { super.presentationTransitionWillBegin() @@ -506,8 +522,6 @@ internal final class BottomSheetPresentationController: UIPresentationController internal override func dismissalTransitionWillBegin() { super.dismissalTransitionWillBegin() - delegate?.presentationControllerWillDismiss?(self) - animateChangesAlongsideTransition { self.dimmingView?.alpha = .zero } @@ -521,8 +535,6 @@ internal final class BottomSheetPresentationController: UIPresentationController transition.wantsInteractiveStart = false if completed { - delegate?.presentationControllerDidDismiss?(self) - dimmingView?.removeFromSuperview() transitionView?.removeFromSuperview()