From c72a65dbd2e98c1439d4cd009786c9c28b02ab56 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Sat, 10 Jun 2023 08:46:16 +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`. --- .../AdaptiveModal/AdaptiveModalManager.swift | 49 ++++++++----------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift index 3dfb6d45..c3edccd2 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift @@ -1009,18 +1009,22 @@ class AdaptiveModalManager: NSObject { ); }; - func shouldSnap(toInterpolationPointIndex nextIndex: Int) -> Bool { + func adjustInterpolationIndex(for nextIndex: Int) -> Int { if nextIndex == 0 { - return self.shouldSnapToInitialSnapPoint; + return self.shouldSnapToInitialSnapPoint + ? nextIndex + : 1; }; let lastIndex = self.interpolationSteps.count - 1; if nextIndex == lastIndex { - return self.shouldSnapToOvershootSnapPoint; + return self.shouldSnapToOvershootSnapPoint + ? nextIndex + : lastIndex - 1; }; - return true; + return nextIndex; }; // MARK: - Functions - DisplayLink-Related @@ -1173,23 +1177,12 @@ class AdaptiveModalManager: NSObject { let coord = point[keyPath: self.modalConfig.inputValueKeyForPoint]; let closestSnapPoint = self.getClosestSnapPoint(forCoord: coord); - let (nextInterpolationIndex, nextInterpolationPoint): - (Int, AdaptiveModalInterpolationPoint) = { - - let shouldSnap = self.shouldSnap( - toInterpolationPointIndex: closestSnapPoint.interpolationIndex - ); - - if shouldSnap { - return ( - closestSnapPoint.interpolationIndex, - closestSnapPoint.interpolationPoint - ); - }; - - return (1, self.interpolationSteps[1]); - }(); + let nextInterpolationIndex = + self.adjustInterpolationIndex(for: closestSnapPoint.interpolationIndex); + let nextInterpolationPoint = + self.interpolationSteps[nextInterpolationIndex]; + let prevFrame = self.modalFrame; let nextFrame = nextInterpolationPoint.computedRect; @@ -1225,19 +1218,19 @@ class AdaptiveModalManager: NSObject { func snapToClosestSnapPoint(){ let closestSnapPoint = self.getClosestSnapPoint(forRect: self.modalFrame); - let shouldSnap = self.shouldSnap( - toInterpolationPointIndex: closestSnapPoint.interpolationIndex - ); + let nextInterpolationIndex = + self.adjustInterpolationIndex(for: closestSnapPoint.interpolationIndex); + + let nextInterpolationPoint = + self.interpolationSteps[nextInterpolationIndex]; let prevFrame = self.modalFrame; - let nextFrame = closestSnapPoint.interpolationPoint.computedRect; + let nextFrame = nextInterpolationPoint.computedRect; - guard shouldSnap, - prevFrame != nextFrame - else { return }; + guard prevFrame != nextFrame else { return }; self.animateModal( - to: closestSnapPoint.interpolationPoint + to: nextInterpolationPoint ); };