Skip to content

Commit

Permalink
Making compatible with Swift 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Jul 22, 2020
1 parent 8f8ce87 commit 0d20940
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Sources/Snap/SnapCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct SnapPointCalculator<State: SnapState> {
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)
}
}

Expand All @@ -26,6 +26,8 @@ struct SnapPointCalculator<State: SnapState> {
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
Expand All @@ -44,9 +46,9 @@ struct SnapPointCalculator<State: SnapState> {
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!
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Snap/SnapDrawer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct SnapDrawer<StateType: SnapState, Background : View, Content: View>
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
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/Snap/SnapState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0d20940

Please sign in to comment.