diff --git a/Sources/Snap/SnapCalculator.swift b/Sources/Snap/SnapCalculator.swift index ead8de2..3c6a6dc 100644 --- a/Sources/Snap/SnapCalculator.swift +++ b/Sources/Snap/SnapCalculator.swift @@ -9,7 +9,7 @@ struct SnapPointCalculator { let contentHeight: CGFloat static var zero: SnapResult { - return SnapResult(state: .invisible, offset: UIScreen.main.bounds.height, contentHeight: 0) + return SnapResult(state: .invisibleState, offset: UIScreen.main.bounds.height, contentHeight: 0) } } @@ -26,6 +26,8 @@ struct SnapPointCalculator { let results: [SnapResult] init(results: [SnapResult]) { + assert(!results.isEmpty, "Invalid Empty Calculation") + let ranges = results.pairs().map { SnapRange(upper: $0, lower: $1) } assert(ranges.allSatisfy { $0.upper.offset < $0.lower.offset }, "Ranges are invalidd") self.ranges = ranges @@ -44,9 +46,9 @@ struct SnapPointCalculator { let matchingRanges = ranges.filter { $0.offsetRange.contains(cardTopEdgeLocation) } guard let range = drag.translation.height > 0 ? matchingRanges.last : matchingRanges.first else { if drag.translation.height < 0 { - return ranges.first?.upper ?? .zero + return results.first! } else { - return ranges.last?.lower ?? .zero + return results.last! } } diff --git a/Sources/Snap/SnapDrawer.swift b/Sources/Snap/SnapDrawer.swift index 0b41934..0876a1b 100644 --- a/Sources/Snap/SnapDrawer.swift +++ b/Sources/Snap/SnapDrawer.swift @@ -31,7 +31,7 @@ public struct SnapDrawer self.state = state self.background = background self.content = content - self._currentResult = State(initialValue: calculator(state: .large)) + self._currentResult = State(initialValue: calculator(state: .largeState)) self.minDrag = self.calculator.results.first?.offset ?? 0 self.maxDrag = self.calculator.results.last?.offset ?? 0 } diff --git a/Sources/Snap/SnapState.swift b/Sources/Snap/SnapState.swift index be115b9..a0b0f8f 100644 --- a/Sources/Snap/SnapState.swift +++ b/Sources/Snap/SnapState.swift @@ -4,8 +4,10 @@ import Foundation public protocol SnapState: Equatable { associatedtype Visible - static var large: Self { get } - static var invisible: Self { get } + // State postfix was added until we upgrade to Swift 5.3 and get + // Enum cases as protocol witnesses + static var largeState: Self { get } + static var invisibleState: Self { get } var visible: Visible? { get } } @@ -15,6 +17,9 @@ public enum ModalSnapState: SnapState { case large } + public static let largeState: ModalSnapState = .large + public static let invisibleState: ModalSnapState = .invisible + case large case invisible @@ -33,6 +38,9 @@ public enum OvercastSnapState: SnapState { case large case tiny } + + public static let largeState: OvercastSnapState = .large + public static let invisibleState: OvercastSnapState = .invisible case large case tiny @@ -57,6 +65,9 @@ public enum AppleMapsSnapState: SnapState { case tiny } + public static let largeState: AppleMapsSnapState = .large + public static let invisibleState: AppleMapsSnapState = .invisible + case large case medium case tiny