From 325cb812b08f09ee0ca26e2c4ebe7f0b6997bc12 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Mon, 19 Jun 2023 01:50:00 +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`. --- .../AdaptiveModalEntranceConfig.swift | 18 ---- .../AdaptiveModal/AdaptiveModalManager.swift | 15 ++-- .../RNILayout/RNILayoutValue+StaticInit.swift | 88 +++++++++++++++++++ .../RNILayout/RNILayoutValue.swift | 5 +- .../Test/AdaptiveModalConfigTestPresets.swift | 20 ++++- ...eModalPresentationTestViewController.swift | 28 +++++- .../project.pbxproj | 8 +- 7 files changed, 148 insertions(+), 34 deletions(-) delete mode 100644 experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalEntranceConfig.swift create mode 100644 experiments/swift-programmatic-modal/RNILayout/RNILayoutValue+StaticInit.swift diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalEntranceConfig.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalEntranceConfig.swift deleted file mode 100644 index ace3567d..00000000 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalEntranceConfig.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// AdaptiveModalEntranceConfig.swift -// swift-programmatic-modal -// -// Created by Dominic Go on 5/24/23. -// - -import Foundation - -struct AdaptiveModalSnapPointPresetConfig { - - let snapPoint: AdaptiveModalSnapPointPreset; - - // MARK: - Functions - Helpers - // --------------------------- - - -}; diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift index 19c23fed..fd096e60 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift @@ -74,7 +74,7 @@ class AdaptiveModalManager: NSObject { // ------------------------------------------ /// The computed frames of the modal based on the snap points - private(set) var rawInterpolationSteps: [AdaptiveModalInterpolationPoint]!; + private(set) var interpolationSteps: [AdaptiveModalInterpolationPoint]!; var prevInterpolationIndex = 0; var nextInterpolationIndex: Int?; @@ -89,11 +89,6 @@ class AdaptiveModalManager: NSObject { self.interpolationSteps[self.currentInterpolationIndex]; }; - // sorted based on the modal direction - var interpolationSteps: [AdaptiveModalInterpolationPoint]! { - self.modalConfig.sortInterpolationSteps(self.rawInterpolationSteps); - }; - private var interpolationRangeInput: [CGFloat]! { self.interpolationSteps.map { $0.percent @@ -1077,7 +1072,7 @@ class AdaptiveModalManager: NSObject { ) { let context = context ?? self.layoutValueContext; - self.rawInterpolationSteps = .Element.compute( + self.interpolationSteps = .Element.compute( usingModalConfig: self.modalConfig, layoutValueContext: context ); @@ -1410,6 +1405,12 @@ class AdaptiveModalManager: NSObject { if self.currentInterpolationIndex == 0 { self.notifyOnModalDidHide(); }; + + print( + "self.interpolationSteps.computedRect: \n -", + self.interpolationSteps.map({ $0.computedRect }), + "\n" + ); }; private func notifyOnModalWillHide(){ diff --git a/experiments/swift-programmatic-modal/RNILayout/RNILayoutValue+StaticInit.swift b/experiments/swift-programmatic-modal/RNILayout/RNILayoutValue+StaticInit.swift new file mode 100644 index 00000000..9ccfe61f --- /dev/null +++ b/experiments/swift-programmatic-modal/RNILayout/RNILayoutValue+StaticInit.swift @@ -0,0 +1,88 @@ +// +// RNILayoutValue+StaticInit.swift +// swift-programmatic-modal +// +// Created by Dominic Go on 6/19/23. +// + +import UIKit + +extension RNILayoutValue { + + static let stretch: Self = .init(mode: .stretch); + + static func stretch( + offsetValue: RNILayoutValueMode? = nil, + offsetOperation: RNIComputableOffset.OffsetOperation? = nil, + minValue: RNILayoutValueMode? = nil, + maxValue: RNILayoutValueMode? = nil + ) -> Self { + + return .init( + mode:. stretch, + offsetValue: offsetValue, + offsetOperation: offsetOperation, + minValue: minValue, + maxValue: maxValue + ); + }; + + static func constant(_ constantValue: CGFloat) -> Self { + return .init(mode: .constant(constantValue)) + }; + + static func percent( + relativeTo: RNILayoutValuePercentTarget = .targetSize, + percentValue: Double, + offsetValue: RNILayoutValueMode? = nil, + offsetOperation: RNIComputableOffset.OffsetOperation? = nil, + minValue: RNILayoutValueMode? = nil, + maxValue: RNILayoutValueMode? = nil + ) -> Self { + + return .init( + mode: .percent( + relativeTo: relativeTo, + percentValue: percentValue + ), + offsetValue: offsetValue, + offsetOperation: offsetOperation, + minValue: minValue, + maxValue: maxValue + ); + }; + + static func safeAreaInsets( + insetKey: KeyPath, + offsetValue: RNILayoutValueMode? = nil, + offsetOperation: RNIComputableOffset.OffsetOperation? = nil, + minValue: RNILayoutValueMode? = nil, + maxValue: RNILayoutValueMode? = nil + ) -> Self { + + return .init( + mode: .safeAreaInsets(insetKey: insetKey), + offsetValue: offsetValue, + offsetOperation: offsetOperation, + minValue: minValue, + maxValue: maxValue + ); + }; + + static func multipleValues( + _ values: [RNILayoutValueMode], + offsetValue: RNILayoutValueMode? = nil, + offsetOperation: RNIComputableOffset.OffsetOperation? = nil, + minValue: RNILayoutValueMode? = nil, + maxValue: RNILayoutValueMode? = nil + ) -> Self { + + return .init( + mode: .multipleValues(values), + offsetValue: offsetValue, + offsetOperation: offsetOperation, + minValue: minValue, + maxValue: maxValue + ); + }; +}; diff --git a/experiments/swift-programmatic-modal/RNILayout/RNILayoutValue.swift b/experiments/swift-programmatic-modal/RNILayout/RNILayoutValue.swift index c0e0c426..0cd14f5f 100644 --- a/experiments/swift-programmatic-modal/RNILayout/RNILayoutValue.swift +++ b/experiments/swift-programmatic-modal/RNILayout/RNILayoutValue.swift @@ -9,10 +9,13 @@ import UIKit public struct RNILayoutValue { + // MARK: - Types + // ------------- + public enum Axis { case horizontal, vertical; }; - + // MARK: - Properties // ------------------ diff --git a/experiments/swift-programmatic-modal/Test/AdaptiveModalConfigTestPresets.swift b/experiments/swift-programmatic-modal/Test/AdaptiveModalConfigTestPresets.swift index 2e3c0bf1..2691d92a 100644 --- a/experiments/swift-programmatic-modal/Test/AdaptiveModalConfigTestPresets.swift +++ b/experiments/swift-programmatic-modal/Test/AdaptiveModalConfigTestPresets.swift @@ -9,12 +9,13 @@ import UIKit enum AdaptiveModalConfigTestPresets: CaseIterable { - static let `default`: Self = .demo03; + static let `default`: Self = .testTopToBottom; case testModalTransform01; case testModalTransformScale; case testModalBorderAndShadow01; case testLeftToRight; + case testTopToBottom; case test01; case test02; @@ -281,6 +282,23 @@ enum AdaptiveModalConfigTestPresets: CaseIterable { layoutPreset: .edgeRight ) ); + + case .testTopToBottom: return AdaptiveModalConfig( + snapPoints: [ + .init( + snapPoint: .init( + horizontalAlignment: .center, + verticalAlignment: .top, + width: .stretch, + height: .percent(percentValue: 0.2) + ) + ) + ], + snapDirection: .topToBottom, + overshootSnapPoint: .init( + layoutPreset: .fitScreenVertically + ) + ); case .test01: return AdaptiveModalConfig( snapPoints: [ diff --git a/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift b/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift index 6cd63b8a..aa9c9520 100644 --- a/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift +++ b/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift @@ -97,12 +97,19 @@ class AdaptiveModalPresentationTestViewController : UIViewController { .demo03 ]; - var currentModalConfigPresetIndex = 0; + var currentModalConfigPresetCounter = 0; + + var currentModalConfigPresetIndex: Int { + self.currentModalConfigPresetCounter % self.modalConfigs.count + }; var currentModalConfigPreset: AdaptiveModalConfigTestPresets { - self.modalConfigs[self.currentModalConfigPresetIndex % self.modalConfigs.count]; + //self.modalConfigs[self.currentModalConfigPresetIndex]; + AdaptiveModalConfigTestPresets.default; }; + var counterLabel: UILabel?; + override func viewDidLoad() { self.view.backgroundColor = .white; @@ -125,6 +132,18 @@ class AdaptiveModalPresentationTestViewController : UIViewController { dummyBackgroundView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor), ]); + let counterLabel: UILabel = { + let label = UILabel(); + + label.text = "\(self.currentModalConfigPresetIndex)"; + label.font = .systemFont(ofSize: 24, weight: .bold); + label.textColor = .white; + + self.counterLabel = label; + + return label; + }(); + let presentButton: UIButton = { let button = UIButton(); button.setTitle("Present View Controller", for: .normal); @@ -160,6 +179,7 @@ class AdaptiveModalPresentationTestViewController : UIViewController { stack.alignment = .center; stack.spacing = 7; + stack.addArrangedSubview(counterLabel); stack.addArrangedSubview(presentButton); stack.addArrangedSubview(nextConfigButton); @@ -193,7 +213,9 @@ class AdaptiveModalPresentationTestViewController : UIViewController { }; @objc func onPressButtonNextConfig(_ sender: UIButton) { - self.currentModalConfigPresetIndex += 1; + self.currentModalConfigPresetCounter += 1; + self.counterLabel!.text = "\(self.currentModalConfigPresetIndex)"; + self.adaptiveModalManager.modalConfig = self.currentModalConfigPreset.config; }; }; diff --git a/experiments/swift-programmatic-modal/swift-programmatic-modal.xcodeproj/project.pbxproj b/experiments/swift-programmatic-modal/swift-programmatic-modal.xcodeproj/project.pbxproj index f21e9ee9..45ece935 100644 --- a/experiments/swift-programmatic-modal/swift-programmatic-modal.xcodeproj/project.pbxproj +++ b/experiments/swift-programmatic-modal/swift-programmatic-modal.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 880492582A23F89000D74E9F /* AdaptiveModalInterpolationPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 880492572A23F89000D74E9F /* AdaptiveModalInterpolationPoint.swift */; }; 88075E272A2121FE00B78388 /* AdaptiveModalUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88075E262A2121FE00B78388 /* AdaptiveModalUtilities.swift */; }; 88203DC12A122AC20088C8E2 /* RNIDynamicModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88203DC02A122AC20088C8E2 /* RNIDynamicModal.swift */; }; + 8849B6A72A3F7A7700A5F412 /* RNILayoutValue+StaticInit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8849B6A62A3F7A7700A5F412 /* RNILayoutValue+StaticInit.swift */; }; 884A18F82A30516B0044AA66 /* AdaptiveModalPresentationTestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 884A18F72A30516B0044AA66 /* AdaptiveModalPresentationTestViewController.swift */; }; 884A18FA2A3146CA0044AA66 /* AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 884A18F92A3146CA0044AA66 /* AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift */; }; 884A18FC2A3146FC0044AA66 /* AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift in Sources */ = {isa = PBXBuildFile; fileRef = 884A18FB2A3146FC0044AA66 /* AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift */; }; @@ -95,7 +96,6 @@ 88D018822A1D09DD004664D2 /* AdaptiveModalAnimationConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88D018812A1D09DD004664D2 /* AdaptiveModalAnimationConfig.swift */; }; 88D018862A1D0A1D004664D2 /* AdaptiveModalSnapPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88D018852A1D0A1D004664D2 /* AdaptiveModalSnapPoint.swift */; }; 88D018882A1D0A36004664D2 /* AdaptiveModalConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88D018872A1D0A36004664D2 /* AdaptiveModalConfig.swift */; }; - 88D0188C2A1DB02E004664D2 /* AdaptiveModalEntranceConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88D0188B2A1DB02E004664D2 /* AdaptiveModalEntranceConfig.swift */; }; 88D0188E2A1DCA61004664D2 /* AdaptiveModalManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88D0188D2A1DCA61004664D2 /* AdaptiveModalManager.swift */; }; 88E8C0182A224A8D008C2FF8 /* AdaptiveModalSnapAnimationConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E8C0172A224A8D008C2FF8 /* AdaptiveModalSnapAnimationConfig.swift */; }; 88E8C01A2A228289008C2FF8 /* AdaptiveModalClampingConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E8C0192A228289008C2FF8 /* AdaptiveModalClampingConfig.swift */; }; @@ -107,6 +107,7 @@ 880492572A23F89000D74E9F /* AdaptiveModalInterpolationPoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalInterpolationPoint.swift; sourceTree = ""; }; 88075E262A2121FE00B78388 /* AdaptiveModalUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalUtilities.swift; sourceTree = ""; }; 88203DC02A122AC20088C8E2 /* RNIDynamicModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RNIDynamicModal.swift; sourceTree = ""; }; + 8849B6A62A3F7A7700A5F412 /* RNILayoutValue+StaticInit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RNILayoutValue+StaticInit.swift"; sourceTree = ""; }; 884A18F72A30516B0044AA66 /* AdaptiveModalPresentationTestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalPresentationTestViewController.swift; sourceTree = ""; }; 884A18F92A3146CA0044AA66 /* AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift"; sourceTree = ""; }; 884A18FB2A3146FC0044AA66 /* AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift"; sourceTree = ""; }; @@ -196,7 +197,6 @@ 88D018812A1D09DD004664D2 /* AdaptiveModalAnimationConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalAnimationConfig.swift; sourceTree = ""; }; 88D018852A1D0A1D004664D2 /* AdaptiveModalSnapPoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalSnapPoint.swift; sourceTree = ""; }; 88D018872A1D0A36004664D2 /* AdaptiveModalConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalConfig.swift; sourceTree = ""; }; - 88D0188B2A1DB02E004664D2 /* AdaptiveModalEntranceConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalEntranceConfig.swift; sourceTree = ""; }; 88D0188D2A1DCA61004664D2 /* AdaptiveModalManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalManager.swift; sourceTree = ""; }; 88E8C0172A224A8D008C2FF8 /* AdaptiveModalSnapAnimationConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalSnapAnimationConfig.swift; sourceTree = ""; }; 88E8C0192A228289008C2FF8 /* AdaptiveModalClampingConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptiveModalClampingConfig.swift; sourceTree = ""; }; @@ -433,6 +433,7 @@ 886AFCB02A325B6F004AC9FB /* RNILayoutValueContext.swift */, 886AFCAC2A3209D5004AC9FB /* RNILayoutValueMode.swift */, 886AFCAE2A320DED004AC9FB /* RNILayoutValuePercentTarget.swift */, + 8849B6A62A3F7A7700A5F412 /* RNILayoutValue+StaticInit.swift */, ); path = RNILayout; sourceTree = ""; @@ -543,7 +544,6 @@ 88D018812A1D09DD004664D2 /* AdaptiveModalAnimationConfig.swift */, 88D018852A1D0A1D004664D2 /* AdaptiveModalSnapPoint.swift */, 88C2F45B2A275B2800DA7450 /* AdaptiveModalSnapPointPreset.swift */, - 88D0188B2A1DB02E004664D2 /* AdaptiveModalEntranceConfig.swift */, 88E8C0172A224A8D008C2FF8 /* AdaptiveModalSnapAnimationConfig.swift */, 88E8C0192A228289008C2FF8 /* AdaptiveModalClampingConfig.swift */, 88D018872A1D0A36004664D2 /* AdaptiveModalConfig.swift */, @@ -666,7 +666,6 @@ 88D0184A2A1B3030004664D2 /* RNILayout.swift in Sources */, 88D018472A1B3030004664D2 /* RNIDictionarySynthesizable+Default.swift in Sources */, 88D0184B2A1B3030004664D2 /* RNIAnimatorSize.swift in Sources */, - 88D0188C2A1DB02E004664D2 /* AdaptiveModalEntranceConfig.swift in Sources */, 88D0183B2A1B3030004664D2 /* Collection+Helpers.swift in Sources */, 88D0188E2A1DCA61004664D2 /* AdaptiveModalManager.swift in Sources */, 88D018772A1B3030004664D2 /* RNIComputableValue.swift in Sources */, @@ -692,6 +691,7 @@ 88D0187E2A1B6CB3004664D2 /* BlurEffectTestViewController.swift in Sources */, 88D018272A1B3030004664D2 /* RNIInternalCleanupMode.swift in Sources */, 88D018782A1B3030004664D2 /* RNIComputableSize.swift in Sources */, + 8849B6A72A3F7A7700A5F412 /* RNILayoutValue+StaticInit.swift in Sources */, 88D018732A1B3030004664D2 /* RNIModalCustomSheetDetent.swift in Sources */, 88075E272A2121FE00B78388 /* AdaptiveModalUtilities.swift in Sources */, 88D018252A1B3030004664D2 /* UIViewController+Helpers.swift in Sources */,