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 10, 2023
1 parent c69f25c commit 6e436b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ extension AdaptiveModalManager: UIViewControllerTransitioningDelegate {
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
return nil;
return self;
};

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

func presentationController(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,13 @@ class AdaptiveModalManager: NSObject {
super.init();

self.computeSnapPoints();

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

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

init(modalConfig: AdaptiveModalConfig) {
Expand Down Expand Up @@ -265,6 +270,17 @@ class AdaptiveModalManager: NSObject {
self.backgroundVisualEffectView = UIVisualEffectView();
};

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

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

private func setupDummyModalView(){
guard let targetView = self.targetView else { return };
let dummyModalView = self.dummyModalView;
Expand All @@ -276,7 +292,7 @@ class AdaptiveModalManager: NSObject {
targetView.addSubview(dummyModalView);
};

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

@objc func onDragPanGesture(_ sender: UIPanGestureRecognizer) {
let gesturePoint = sender.location(in: self.targetView);
self.gesturePoint = gesturePoint;

let gestureVelocity = sender.velocity(in: self.targetView);
self.gestureVelocity = gestureVelocity;

switch sender.state {
case .began:
self.gestureInitialPoint = gesturePoint;

case .changed:
self.modalAnimator?.stopAnimation(true);

self.applyInterpolationToModal(forGesturePoint: gesturePoint);
self.onModalWillSnap();

case .cancelled, .ended:
guard self.enableSnapping else {
self.clearGestureValues();
return;
};

let gestureFinalPoint = self.gestureFinalPoint ?? gesturePoint;

self.snapToClosestSnapPoint(forPoint: gestureFinalPoint) {
self.endDisplayLink();
self.onModalDidSnap();
};

self.clearGestureValues();
self.startDisplayLink();

default:
break;
};
};

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

Expand Down Expand Up @@ -1103,7 +1157,13 @@ class AdaptiveModalManager: NSObject {
self.setupViewControllers();
self.setupDummyModalView();
self.setupInitViews();
self.setupGestureHandler();

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

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

func computeSnapPoints(
Expand All @@ -1117,44 +1177,6 @@ class AdaptiveModalManager: NSObject {
);
};

func notifyOnDragPanGesture(_ gesture: UIPanGestureRecognizer){
let gesturePoint = gesture.location(in: self.targetView);
self.gesturePoint = gesturePoint;

let gestureVelocity = gesture.velocity(in: self.targetView);
self.gestureVelocity = gestureVelocity;

switch gesture.state {
case .began:
self.gestureInitialPoint = gesturePoint;

case .changed:
self.modalAnimator?.stopAnimation(true);

self.applyInterpolationToModal(forGesturePoint: gesturePoint);
self.onModalWillSnap();

case .cancelled, .ended:
guard self.enableSnapping else {
self.clearGestureValues();
return;
};

let gestureFinalPoint = self.gestureFinalPoint ?? gesturePoint;

self.snapToClosestSnapPoint(forPoint: gestureFinalPoint) {
self.endDisplayLink();
self.onModalDidSnap();
};

self.clearGestureValues();
self.startDisplayLink();

default:
break;
};
};

func updateModal(){
guard !self.isAnimating else { return };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class AdaptiveModalPresentationTestViewController : UIViewController {

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

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


self.present(testVC, animated: true);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,12 @@ class RNIDraggableTestViewController : UIViewController {
// alpha: 0
// );

view.addGestureRecognizer(
UIPanGestureRecognizer(
target: self,
action: #selector(self.onDragPanGestureView(_:))
)
);
// view.addGestureRecognizer(
// UIPanGestureRecognizer(
// target: self,
// action: #selector(self.onDragPanGestureView(_:))
// )
// );

let floatingViewLabel = self.floatingViewLabel;
view.addSubview(floatingViewLabel);
Expand Down Expand Up @@ -435,8 +435,8 @@ class RNIDraggableTestViewController : UIViewController {
dummyBackgroundView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
]);

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

override func viewDidLayoutSubviews() {
Expand All @@ -449,11 +449,11 @@ class RNIDraggableTestViewController : UIViewController {
// self.updateFloatingView(isAnimated: true);
};

@objc func onDragPanGestureView(_ sender: UIPanGestureRecognizer) {
// print("onDragPanGestureView - velocity: \(sender.velocity(in: self.view))");

self.modalManager.notifyOnDragPanGesture(sender);
};
// @objc func onDragPanGestureView(_ sender: UIPanGestureRecognizer) {
// // print("onDragPanGestureView - velocity: \(sender.velocity(in: self.view))");
//
// self.modalManager.notifyOnDragPanGesture(sender);
// };
};

extension RNIDraggableTestViewController: AdaptiveModalEventNotifiable {
Expand Down

0 comments on commit 6e436b2

Please sign in to comment.