Skip to content

Commit

Permalink
💫 Update: Exp - AdaptiveModal
Browse files Browse the repository at this point in the history
Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`.
  • Loading branch information
dominicstop committed Jun 16, 2023
1 parent cfa574e commit 317e20d
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,45 @@ extension AdaptiveModalManager: UIViewControllerAnimatedTransitioning {
func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?
) -> TimeInterval {
// to be implemented
return 1;

return self.modalConfig.snapAnimationConfig.springAnimationSettlingTime;
};

func animateTransition(
using transitionContext: UIViewControllerContextTransitioning
) {
//TBA
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);
};

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)"
);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@

import UIKit

extension AdaptiveModalManager: UIViewControllerTransitioningDelegate {

func animationController(
forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
return self;
extension AdaptiveModalManager: UIAdaptivePresentationControllerDelegate {
func adaptivePresentationStyle(
for controller: UIPresentationController,
traitCollection: UITraitCollection
) -> UIModalPresentationStyle {

return .custom;
};
};

func animationController(
forDismissed dismissed: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
return self;
};

extension AdaptiveModalManager: UIViewControllerTransitioningDelegate {

func presentationController(
forPresented presented: UIViewController,
Expand All @@ -35,7 +32,35 @@ extension AdaptiveModalManager: UIViewControllerTransitioningDelegate {
modalManager: self
);

//presentationController.delegate = 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;
};

func animationController(
forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {

return self;
};

func animationController(
forDismissed dismissed: UIViewController
) -> UIViewControllerAnimatedTransitioning? {

return self;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,22 @@ class AdaptiveModalManager: NSObject {
// MARK: - Functions - Setup
// -------------------------

private func setupViewControllers() {
func setupViewControllers() {
guard let modalVC = self.modalViewController else { return };

modalVC.modalPresentationStyle = .custom;
modalVC.transitioningDelegate = self;
};

private func setupInitViews() {
func setupInitViews() {
self.modalBackgroundView = UIView();
self.modalBackgroundVisualEffectView = UIVisualEffectView();

self.backgroundDimmingView = UIView();
self.backgroundVisualEffectView = UIVisualEffectView();
};

private func setupGestureHandler() {
func setupGestureHandler() {
guard let modalView = self.modalView else { return };

modalView.gestureRecognizers?.removeAll();
Expand All @@ -260,7 +260,7 @@ class AdaptiveModalManager: NSObject {
);
};

private func setupDummyModalView() {
func setupDummyModalView() {
guard let targetView = self.targetView else { return };
let dummyModalView = self.dummyModalView;

Expand All @@ -274,7 +274,7 @@ class AdaptiveModalManager: NSObject {
targetView.addSubview(dummyModalView);
};

private func setupAddViews() {
func setupAddViews() {
guard let modalView = self.modalView,
let targetView = self.targetView
else { return };
Expand Down Expand Up @@ -320,7 +320,7 @@ class AdaptiveModalManager: NSObject {
};
};

private func setupViewConstraints() {
func setupViewConstraints() {
guard let modalView = self.modalView,
let targetView = self.targetView
else { return };
Expand Down Expand Up @@ -906,13 +906,6 @@ class AdaptiveModalManager: NSObject {
self.clearGestureValues();
self.clearAnimators();
self.cleanupViews();

print(
"\n - self.backgroundVisualEffectView: \(self.backgroundVisualEffectView)",
"\n - self.backgroundDimmingView: \(self.backgroundDimmingView)",
"\n - self.modalBackgroundVisualEffectView: \(self.modalBackgroundVisualEffectView)",
"\n - self.modalBackgroundView: \(self.modalBackgroundView)"
);
};

// MARK: - Functions
Expand Down Expand Up @@ -1127,6 +1120,23 @@ class AdaptiveModalManager: NSObject {
};
};

func debug(prefix: String? = ""){
print(
"\n - AdaptiveModalManager.debug - \(prefix ?? "N/A")"
+ "\n - modalView: \(self.modalView?.debugDescription ?? "N/A")"
+ "\n - modalView frame: \(self.modalView?.frame.debugDescription ?? "N/A")"
+ "\n - modalView superview: \(self.modalView?.superview.debugDescription ?? "N/A")"
+ "\n - targetView: \(self.targetView?.debugDescription ?? "N/A")"
+ "\n - targetView frame: \(self.targetView?.frame.debugDescription ?? "N/A")"
+ "\n - targetView superview: \(self.targetView?.superview.debugDescription ?? "N/A")"
+ "\n - modalViewController: \(self.modalViewController?.debugDescription ?? "N/A")"
+ "\n - targetViewController: \(self.targetViewController?.debugDescription ?? "N/A")"
+ "\n - currentInterpolationIndex: \(self.currentInterpolationIndex)"
+ "\n - modalView gestureRecognizers: \(self.modalView?.gestureRecognizers.debugDescription ?? "N/A")"
+ "\n"
);
};

// MARK: - Functions - DisplayLink-Related
// ---------------------------------------

Expand Down Expand Up @@ -1250,34 +1260,39 @@ class AdaptiveModalManager: NSObject {
// ------------------------------

func prepareForPresentation(
modalView: UIView,
targetView: UIView
modalView: UIView? = nil,
targetView: UIView? = nil
) {
let didViewsChange =
modalView !== self.modalView || targetView !== self.targetView;

if didViewsChange {
self.cleanup();
};
guard let modalView = modalView ?? self.modalView,
let targetView = targetView ?? self.targetView
else { return };

self.cleanup();

self.modalView = modalView;
self.targetView = targetView;

self.computeSnapPoints();
self.setupViewControllers();

if didViewsChange {
self.setupInitViews();
self.setupDummyModalView();
self.setupGestureHandler();

self.setupAddViews();
self.setupViewConstraints();
};

self.setupInitViews();
self.setupDummyModalView();
self.setupGestureHandler();

self.setupAddViews();
self.setupViewConstraints();

self.updateModal();
};

func prepareForPresentation(
presentingViewController presentingVC: UIViewController
) {
self.modalViewController = presentingVC;
self.modalView = presentingVC.view;

self.setupViewControllers();
};

func notifyDidLayoutSubviews() {
self.computeSnapPoints();
self.updateModal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ class AdaptiveModalPresentationController: UIPresentationController {
self.modalManager = modalManager;
};

override func presentationTransitionWillBegin() {
override func presentationTransitionWillBegin() {
print(
"AdaptiveModalPresentationController.presentationTransitionWillBegin"
+ "\n - presentedViewController: \(self.presentedViewController)"
+ "\n - presenting: \(self.presentingViewController)"
+ "\n - presentedViewController - frame: \(self.presentedViewController.view.frame)"
+ "\n - presenting - frame: \(self.presentingViewController.view.frame)"
+ "\n - presentedViewController - superview: \(self.presentedViewController.view.superview)"
+ "\n - presenting - superview: \(self.presentingViewController.view.superview)"
+ "\n"
);

};
};

override func presentationTransitionDidEnd(_ completed: Bool) {

};
override func presentationTransitionDidEnd(_ completed: Bool) {
print(
"AdaptiveModalPresentationController.presentationTransitionDidEnd"
+ "\n - presentedViewController: \(self.presentedViewController)"
+ "\n - presenting: \(self.presentingViewController)"
+ "\n"
);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

class TestViewController: UIViewController {
class TestModalViewController: UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .white;
};
Expand Down Expand Up @@ -68,7 +68,11 @@ class AdaptiveModalPresentationTestViewController : UIViewController {
};

@objc func onPressButtonPresentViewController(_ sender: UIButton) {
let testVC = TestViewController();
let testVC = TestModalViewController();

self.adaptiveModalManager.prepareForPresentation(
presentingViewController: testVC
);

self.present(testVC, animated: true);
};
Expand Down
2 changes: 1 addition & 1 deletion experiments/swift-programmatic-modal/Test/TestRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit

enum TestRoutes {
static let rootRouteKey: Self = .RNIDraggableTest;
static let rootRouteKey: Self = .AdaptiveModalPresentationTest;

case RNILayoutTest;
case RNIDraggableTest;
Expand Down

0 comments on commit 317e20d

Please sign in to comment.