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 11, 2023
1 parent 6e436b2 commit 8af2b02
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ struct AdaptiveModalConfig {
let snapAnimationConfig: AdaptiveModalSnapAnimationConfig;
let interpolationClampingConfig: AdaptiveModalClampingConfig;

// rename to undershootSnapPoint
let initialSnapPoint: AdaptiveModalSnapPointPreset;
let overshootSnapPoint: AdaptiveModalSnapPointPreset;

let initialSnapPointIndex: Int;

// let entranceConfig: AdaptiveModalEntranceConfig;
// let snapSwipeVelocityThreshold: CGFloat = 0;

Expand Down Expand Up @@ -102,6 +105,7 @@ struct AdaptiveModalConfig {
snapPercentStrategy: SnapPercentStrategy = .position,
snapAnimationConfig: AdaptiveModalSnapAnimationConfig = .default,
interpolationClampingConfig: AdaptiveModalClampingConfig = .default,
initialSnapPointIndex: Int = 1,
initialSnapPoint: AdaptiveModalSnapPointPreset? = nil,
overshootSnapPoint: AdaptiveModalSnapPointPreset? = nil
) {
Expand All @@ -113,6 +117,8 @@ struct AdaptiveModalConfig {
self.snapAnimationConfig = snapAnimationConfig;
self.interpolationClampingConfig = interpolationClampingConfig;

self.initialSnapPointIndex = initialSnapPointIndex;

self.initialSnapPoint = initialSnapPoint
?? .getDefaultInitialSnapPoint(forDirection: snapDirection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ extension AdaptiveModalManager: UIViewControllerAnimatedTransitioning {
func animateTransition(
using transitionContext: UIViewControllerContextTransitioning
) {
//TBA
guard let fromViewController = transitionContext.viewController(forKey: .from),
let toViewController = transitionContext.view(forKey: .to)
else { return };

self.targetView = transitionContext.containerView;

self.currentInterpolationIndex = 1;

self.computeSnapPoints();

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

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

self.updateModal();

print(
"transitionContext"
+ "\n - containerView: \(transitionContext.containerView)"
+ "\n - modalView: + \(self.modalView)"
+ "\n - targetView: + \(self.targetView)"
+ "\n - currentInterpolationIndex: + \(self.currentInterpolationIndex)"
+ "\n - currentInterpolationStep: + \(self.currentInterpolationStep)"
);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import UIKit



extension AdaptiveModalManager: UIViewControllerTransitioningDelegate {

func animationController(
Expand All @@ -30,10 +31,13 @@ extension AdaptiveModalManager: UIViewControllerTransitioningDelegate {
source: UIViewController
) -> UIPresentationController? {

return AdaptiveModalPresentationController(
let presentationController = AdaptiveModalPresentationController(
presentedViewController: presented,
presenting: presenting,
modalManager: self
);

//presentationController.delegate = self;
return presentationController;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

class AdaptiveModalManager: NSObject {
class AdaptiveModalManager: NSObject, UIGestureRecognizerDelegate {

// MARK: - Properties - Config-Related
// ------------------------------------
Expand Down Expand Up @@ -73,7 +73,7 @@ class AdaptiveModalManager: NSObject {
var prevInterpolationIndex = 0;
var nextInterpolationIndex: Int?;

var currentInterpolationIndex = 1 {
var currentInterpolationIndex = 0 {
didSet {
self.prevInterpolationIndex = oldValue;
}
Expand Down Expand Up @@ -216,38 +216,12 @@ class AdaptiveModalManager: NSObject {
// MARK: - Init
// ------------

init(
modalConfig: AdaptiveModalConfig,
modalView: UIView,
targetView: UIView,
currentSizeProvider: (() -> CGSize)? = nil
) {
self.modalConfig = modalConfig;

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

super.init();

self.computeSnapPoints();

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

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

init(modalConfig: AdaptiveModalConfig) {
self.modalConfig = modalConfig;

super.init();

self.computeSnapPoints();
self.setupViewControllers();
self.setupInitViews();
self.setupDummyModalView();
};

deinit {
Expand All @@ -257,31 +231,15 @@ class AdaptiveModalManager: NSObject {
// MARK: - Functions - Setup
// -------------------------

func setupViewControllers() {
modalViewController?.modalPresentationStyle = .custom;
modalViewController?.transitioningDelegate = self;
};

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

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

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

modalView.addGestureRecognizer(
UIPanGestureRecognizer(
target: self,
action: #selector(self.onDragPanGesture(_:))
)
);
};

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

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

private func setupAddViews(){
func setupAddViews(){
guard let modalView = self.modalView,
let targetView = self.targetView
else { return };
Expand All @@ -317,6 +275,7 @@ class AdaptiveModalManager: NSObject {

modalView.clipsToBounds = true;
modalView.backgroundColor = .clear;

modalWrapperView.addSubview(modalView);

if let modalBackgroundView = self.modalBackgroundView {
Expand All @@ -332,7 +291,6 @@ class AdaptiveModalManager: NSObject {
modalView.sendSubviewToBack(modalBGVisualEffectView);

modalBGVisualEffectView.clipsToBounds = true;
modalBGVisualEffectView.backgroundColor = .clear;
modalBGVisualEffectView.isUserInteractionEnabled = false;
};
};
Expand Down Expand Up @@ -395,6 +353,29 @@ class AdaptiveModalManager: NSObject {
]);
};
};

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

let gesture = UIPanGestureRecognizer(
target: self,
action: #selector(Self.onDragPanGesture(_:))
);

gesture.isEnabled = true;
gesture.minimumNumberOfTouches = 1;
gesture.maximumNumberOfTouches = 1;
gesture.delegate = self;

modalView.addGestureRecognizer(gesture);
modalView.isUserInteractionEnabled = true;
modalView.backgroundColor = .systemBackground;
};

func setupViewControllers() {
modalViewController?.modalPresentationStyle = .custom;
modalViewController?.transitioningDelegate = self;
};

// MARK: - Functions - Interpolation-Related Helpers
// -------------------------------------------------
Expand Down Expand Up @@ -1042,6 +1023,13 @@ class AdaptiveModalManager: NSObject {
return nextIndex;
};

public func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
) -> Bool {
return true;
};

@objc func onDragPanGesture(_ sender: UIPanGestureRecognizer) {
let gesturePoint = sender.location(in: self.targetView);
self.gesturePoint = gesturePoint;
Expand Down Expand Up @@ -1143,27 +1131,37 @@ class AdaptiveModalManager: NSObject {

// MARK: - User-Invoked Functions
// ------------------------------
func set(
viewControllerToPresent: UIViewController,
presentingViewController: UIViewController

func prepareForPresentation(
modalView: UIView,
targetView: UIView
) {
self.modalViewController = viewControllerToPresent;
self.targetViewController = presentingViewController;
self.modalView = modalView;
self.targetView = targetView;

self.modalView = viewControllerToPresent.view;
self.targetView = presentingViewController.view;
modalView.isUserInteractionEnabled = true;

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

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

self.computeSnapPoints();
self.updateModal();

modalView.frame = self.modalWrapperView.frame;
};

func showModal(
completion: ((UIViewAnimatingPosition) -> Void)? = nil
){
self.currentInterpolationIndex = self.modalConfig.initialSnapPointIndex;
self.animateModal(
to: self.currentInterpolationStep,
completion: completion
);
};

func computeSnapPoints(
Expand All @@ -1190,6 +1188,10 @@ class AdaptiveModalManager: NSObject {
};
};

func snapToCurrentIndex(){
self.animateModal(to: self.currentInterpolationStep);
};

func snapToClosestSnapPoint(
forPoint point: CGPoint,
duration: CGFloat? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@ class AdaptiveModalPresentationController: UIPresentationController {
);

self.modalManager = modalManager;
}
};

override func presentationTransitionWillBegin() {

};

override func presentationTransitionDidEnd(_ completed: Bool) {

};
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ class AdaptiveModalPresentationTestViewController : UIViewController {
};

@objc func onPressButtonPresentViewController(_ sender: UIButton) {

let testVC = TestViewController();

self.adaptiveModalManager.set(
viewControllerToPresent: testVC,
presentingViewController: self
);

let manager = self.adaptiveModalManager;
manager.modalViewController = testVC;
manager.modalView = testVC.view;
manager.setupViewControllers();

self.present(testVC, animated: true);
};
Expand Down
Loading

0 comments on commit 8af2b02

Please sign in to comment.