From e78a191d6853f907a9b19f8b3af833335bbe2e11 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:34:52 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AB=20Update:=20Exp=20-=20`AdaptiveMod?= =?UTF-8?q?al`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`. --- ...IViewControllerAnimatedTransitioning.swift | 53 ++++++++----------- ...IViewControllerTransitioningDelegate.swift | 18 ++----- .../AdaptiveModal/AdaptiveModalManager.swift | 10 ++++ 3 files changed, 35 insertions(+), 46 deletions(-) diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift index 5fa8b60c..091581c8 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift @@ -19,38 +19,27 @@ extension AdaptiveModalManager: UIViewControllerAnimatedTransitioning { func animateTransition( using transitionContext: UIViewControllerContextTransitioning ) { - guard let fromVC = transitionContext.viewController(forKey: .from), - let toVC = transitionContext.viewController(forKey: .to) - else { return } - - self.targetView = transitionContext.containerView; - self.targetViewController = fromVC; - - self.prepareForPresentation(); - self.showModal() { - transitionContext.completeTransition(true); + guard let fromVC = transitionContext.viewController(forKey: .from) + else { return }; + + switch self.presentationState { + case .presenting: + self.targetView = transitionContext.containerView; + self.targetViewController = fromVC; + + self.prepareForPresentation(); + + self.showModal() { + transitionContext.completeTransition(true); + }; + + case .dismissing: + self.dismissModal(){ + transitionContext.completeTransition(true); + }; + + case .none: + break; }; - - self.debug(prefix: "AdaptiveModalPresentationController.animateTransition"); - - - - - // if self.currentInterpolationIndex == 0 { - // self.prepareForPresentation( - // presentingViewController: fromVC, - // presentedViewController: toVC - // ); - // }; - // - // self.showModal(){ - // transitionContext.completeTransition(true); - // }; - - print( - "AdaptiveModalPresentationController.animateTransition" - + "\n - fromVC: \(fromVC)" - + "\n - toVC: \(toVC)" - ); }; }; diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift index 4b95513f..14ac28ee 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift @@ -32,18 +32,6 @@ extension AdaptiveModalManager: UIViewControllerTransitioningDelegate { modalManager: self ); - print( - "AdaptiveModalManager+UIViewControllerTransitioningDelegate" - + "\n - presentationController" - + "\n - presented: \(presented)" - + "\n - presented.view.frame: \(presented.view.frame)" - + "\n - presenting: \(presenting)" - + "\n - presenting.view.frame: \(presenting?.view.frame)" - + "\n - source: \(source)" - + "\n - source.view.frame: \(source.view.frame)" - + "\n" - ); - presentationController.delegate = self; return presentationController; }; @@ -53,14 +41,16 @@ extension AdaptiveModalManager: UIViewControllerTransitioningDelegate { presenting: UIViewController, source: UIViewController ) -> UIViewControllerAnimatedTransitioning? { - + + self.presentationState = .presenting; return self; }; func animationController( forDismissed dismissed: UIViewController ) -> UIViewControllerAnimatedTransitioning? { - + + self.presentationState = .dismissing; return self; }; }; diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift index 1043fe3d..5ac0ed9d 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift @@ -9,6 +9,10 @@ import UIKit class AdaptiveModalManager: NSObject { + enum PresentationState { + case presenting, dismissing, none; + }; + // MARK: - Properties - Config-Related // ------------------------------------ @@ -193,6 +197,7 @@ class AdaptiveModalManager: NSObject { // ------------------- private var didTriggerSetup = false; + var presentationState: PresentationState = .none; weak var eventDelegate: AdaptiveModalEventNotifiable?; @@ -1389,4 +1394,9 @@ class AdaptiveModalManager: NSObject { let nextIndex = self.modalConfig.initialSnapPointIndex; self.snapTo(interpolationIndex: nextIndex, completion: completion); }; + + func dismissModal(completion: (() -> Void)? = nil){ + let nextIndex = 0; + self.snapTo(interpolationIndex: nextIndex, completion: completion); + }; };