Skip to content

Commit d6182e8

Browse files
authored
Consistent target index when reloading heights (#58)
1 parent 1b81af7 commit d6182e8

3 files changed

+15
-9
lines changed

Diff for: Sources/BottomSheetPresentationController.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class BottomSheetPresentationController: UIPresentationController {
4646
// MARK: - Private properties
4747

4848
private var contentHeights: [CGFloat]
49-
private let startTargetIndex: Int
49+
private var startTargetIndex: Int
5050
private let handleBackground: BottomSheetView.HandleBackground
5151
private let useSafeAreaInsets: Bool
5252
private let draggableHeight: CGFloat?
@@ -93,9 +93,12 @@ final class BottomSheetPresentationController: UIPresentationController {
9393
bottomSheetView?.reset()
9494
}
9595

96-
func reload(with contentHeights: [CGFloat]) {
96+
func reload(with contentHeights: [CGFloat], targetIndex: Int?) {
9797
self.contentHeights = contentHeights
98-
bottomSheetView?.reload(with: contentHeights)
98+
if let targetIndex = targetIndex {
99+
startTargetIndex = targetIndex
100+
}
101+
bottomSheetView?.reload(with: contentHeights, targetIndex: targetIndex)
99102
}
100103

101104
func hideDimView() {

Diff for: Sources/BottomSheetTransitioningDelegate.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import UIKit
66

77
public final class BottomSheetTransitioningDelegate: NSObject {
88
public private(set) var contentHeights: [CGFloat]
9-
private let startTargetIndex: Int
9+
private var startTargetIndex: Int
1010
private let handleBackground: BottomSheetView.HandleBackground
1111
private let draggableHeight: CGFloat?
1212
private let useSafeAreaInsets: Bool
@@ -62,8 +62,11 @@ public final class BottomSheetTransitioningDelegate: NSObject {
6262
}
6363

6464
public func reload(with contentHeights: [CGFloat]) {
65+
let previousHeight = self.contentHeights[safe: startTargetIndex] ?? 0
66+
let indexOfPreviousHeightInNewHeights = contentHeights.firstIndex(of: previousHeight) ?? 0
6567
self.contentHeights = contentHeights
66-
presentationController?.reload(with: contentHeights)
68+
startTargetIndex = indexOfPreviousHeightInNewHeights
69+
presentationController?.reload(with: contentHeights, targetIndex: startTargetIndex)
6770
}
6871

6972
public func hideBackgroundOverlay() {

Diff for: Sources/BottomSheetView.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ public final class BottomSheetView: UIView {
249249
}
250250
}
251251

252-
public func reload(with contentHeights: [CGFloat]) {
253-
let previousHeight = self.contentHeights[safe: currentTargetOffsetIndex] ?? 0
254-
let indexOfPreviousHeightInNewHeights = contentHeights.firstIndex(of: previousHeight) ?? 0
252+
public func reload(with contentHeights: [CGFloat], targetIndex: Int?) {
255253
self.contentHeights = contentHeights
256-
currentTargetOffsetIndex = indexOfPreviousHeightInNewHeights
254+
if let targetIndex = targetIndex {
255+
currentTargetOffsetIndex = targetIndex
256+
}
257257
reset()
258258
}
259259

0 commit comments

Comments
 (0)