Skip to content

Commit

Permalink
Bottom sheet improvements (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
almazrafi committed Jun 15, 2023
1 parent 792c7b0 commit 15765bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal final class BottomSheetDetentionController {

private var cachedDetentValues: [BottomSheetDetentKey: CGFloat?] = [:]

private var cachedSmallestDetentValueIgnoringKeyboard: CGFloat?
private var cachedSmallestDetentValue: CGFloat?
private var cachedLargestDetentValue: CGFloat?
private var cachedCurrentDetentValue: CGFloat?
Expand All @@ -13,6 +14,7 @@ internal final class BottomSheetDetentionController {

internal var keyboardHeight: CGFloat = .zero {
didSet {
cachedSmallestDetentValue = nil
cachedLargestDetentValue = nil
cachedCurrentDetentValue = nil
}
Expand Down Expand Up @@ -71,16 +73,31 @@ internal final class BottomSheetDetentionController {
return resolveDetentValue(detent: detent)
}

internal func resolveSmallestDetentValueIgnoringKeyboard() -> CGFloat {
if let cachedSmallestDetentValueIgnoringKeyboard {
return cachedSmallestDetentValueIgnoringKeyboard
}

let smallestDetentValue = detents
.compactMap(resolveDetentValue(detent:))
.min() ?? .zero

cachedSmallestDetentValueIgnoringKeyboard = smallestDetentValue

return smallestDetentValue
}

internal func resolveSmallestDetentValue() -> CGFloat {
if let cachedSmallestDetentValue {
return cachedSmallestDetentValue
}

let smallestDetentValue = detents
.compactMap(resolveDetentValue(detent:))
.min() ?? .zero
.min()
.map { min($0 + keyboardHeight, maximumDetentValue) } ?? .zero

self.cachedSmallestDetentValue = smallestDetentValue
cachedSmallestDetentValue = smallestDetentValue

return smallestDetentValue
}
Expand All @@ -95,7 +112,7 @@ internal final class BottomSheetDetentionController {
.max()
.map { min($0 + keyboardHeight, maximumDetentValue) } ?? .zero

self.cachedLargestDetentValue = largestDetentValue
cachedLargestDetentValue = largestDetentValue

return largestDetentValue
}
Expand All @@ -116,7 +133,7 @@ internal final class BottomSheetDetentionController {
.map { min($0 + self.keyboardHeight, self.maximumDetentValue) }
.min() ?? .zero

self.cachedCurrentDetentValue = currentDetentValue
cachedCurrentDetentValue = currentDetentValue

return currentDetentValue
}
Expand Down Expand Up @@ -159,6 +176,7 @@ internal final class BottomSheetDetentionController {
internal func invalidateDetents() {
cachedDetentValues = [:]

cachedSmallestDetentValueIgnoringKeyboard = nil
cachedSmallestDetentValue = nil
cachedLargestDetentValue = nil
cachedCurrentDetentValue = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ internal final class BottomSheetDismissingInteraction: BottomSheetInteraction {

let smallestDetentValue = presentationController
.detention
.resolveSmallestDetentValue()
.resolveSmallestDetentValueIgnoringKeyboard()

if gesturePredictedValue > 0.5 * smallestDetentValue {
presentationController.transition.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal final class BottomSheetPresentedInteraction: BottomSheetInteraction {

let smallestDetentValue = presentationController
.detention
.resolveSmallestDetentValue()
.resolveSmallestDetentValueIgnoringKeyboard()

if gestureValue < smallestDetentValue - .leastNonzeroMagnitude {
let smallestDetentDelta = smallestDetentValue - currentDetentValue
Expand All @@ -89,7 +89,7 @@ internal final class BottomSheetPresentedInteraction: BottomSheetInteraction {

let smallestDetentValue = presentationController
.detention
.resolveSmallestDetentValue()
.resolveSmallestDetentValueIgnoringKeyboard()

let dismissalThreshold = isGestureFinished
? smallestDetentValue * 0.5
Expand Down

0 comments on commit 15765bd

Please sign in to comment.