Skip to content

Commit 6acb6ac

Browse files
osanojninarg
andauthored
Try to keep height of bottomsheet when reloading with new contentHeights (#56)
* Try to keep height of bottomsheet when reloading with new contentHeights * Use safe subscript for arrays (#57) Co-authored-by: ninarg <[email protected]>
1 parent 6787c9c commit 6acb6ac

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

BottomSheet.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4DB2C6ED27230B6F00C3E559 /* Array+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DB2C6EC27230B6F00C3E559 /* Array+Extensions.swift */; };
1011
5D5A7BF1256BA2A900FD1820 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5D5A7BEF256BA2A900FD1820 /* Localizable.strings */; };
1112
5DAC3BF4256964C60052400F /* HandleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DAC3BF3256964C60052400F /* HandleView.swift */; };
1213
5DAC3BFB256966C50052400F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5DAC3BF9256966C50052400F /* Localizable.strings */; };
@@ -62,6 +63,7 @@
6263
/* End PBXCopyFilesBuildPhase section */
6364

6465
/* Begin PBXFileReference section */
66+
4DB2C6EC27230B6F00C3E559 /* Array+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+Extensions.swift"; sourceTree = "<group>"; };
6567
5D5A7BF0256BA2A900FD1820 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = Localizable.strings; sourceTree = "<group>"; };
6668
5DAC3BF3256964C60052400F /* HandleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HandleView.swift; sourceTree = "<group>"; };
6769
5DAC3BFA256966C50052400F /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = Localizable.strings; sourceTree = "<group>"; };
@@ -144,6 +146,7 @@
144146
isa = PBXGroup;
145147
children = (
146148
5DAC3C01256967B10052400F /* String+Localized.swift */,
149+
4DB2C6EC27230B6F00C3E559 /* Array+Extensions.swift */,
147150
);
148151
path = Extensions;
149152
sourceTree = "<group>";
@@ -384,6 +387,7 @@
384387
buildActionMask = 2147483647;
385388
files = (
386389
5DAC3C02256967B10052400F /* String+Localized.swift in Sources */,
390+
4DB2C6ED27230B6F00C3E559 /* Array+Extensions.swift in Sources */,
387391
CF1539FB2382F62A001687C1 /* BottomSheetPresentationController.swift in Sources */,
388392
CF46A383238593AD00FFCB9F /* BottomSheetCalculator.swift in Sources */,
389393
5DAC3BF4256964C60052400F /* HandleView.swift in Sources */,

Sources/BottomSheetPresentationController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ final class BottomSheetPresentationController: UIPresentationController {
133133
override var frameOfPresentedViewInContainerView: CGRect {
134134
guard let presentedView = presentedView else { return .zero }
135135
guard let containerView = containerView else { return .zero }
136+
guard let height = contentHeights[safe: startTargetIndex] else { return .zero }
136137

137138
let contentHeight = BottomSheetCalculator.contentHeight(
138139
for: presentedView,
139140
in: containerView,
140-
height: contentHeights[startTargetIndex],
141+
height: height,
141142
useSafeAreaInsets: useSafeAreaInsets
142143
)
143144

Sources/BottomSheetView.swift

+14-7
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ public final class BottomSheetView: UIView {
163163
/// - view: the container for the bottom sheet view
164164
/// - completion: a closure to be executed when the animation ends
165165
public func present(in superview: UIView, targetIndex: Int = 0, animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
166-
guard self.superview != superview else { return }
166+
guard
167+
self.superview != superview,
168+
let height = contentHeights[safe: targetIndex]
169+
else { return }
167170

168171
superview.addSubview(dimView)
169172
superview.addSubview(self)
@@ -174,7 +177,7 @@ public final class BottomSheetView: UIView {
174177
let startOffset = BottomSheetCalculator.offset(
175178
for: contentView,
176179
in: superview,
177-
height: contentHeights[targetIndex],
180+
height: height,
178181
useSafeAreaInsets: useSafeAreaInsets
179182
)
180183

@@ -240,12 +243,17 @@ public final class BottomSheetView: UIView {
240243
public func reset() {
241244
updateTargetOffsets()
242245
createTranslationTargets()
243-
animate(to: targetOffsets[currentTargetOffsetIndex])
246+
247+
if let targetOffset = targetOffsets[safe: currentTargetOffsetIndex] {
248+
animate(to: targetOffset)
249+
}
244250
}
245251

246252
public func reload(with contentHeights: [CGFloat]) {
253+
let previousHeight = self.contentHeights[safe: currentTargetOffsetIndex] ?? 0
254+
let indexOfPreviousHeightInNewHeights = contentHeights.firstIndex(of: previousHeight) ?? 0
247255
self.contentHeights = contentHeights
248-
currentTargetOffsetIndex = 0
256+
currentTargetOffsetIndex = indexOfPreviousHeightInNewHeights
249257
reset()
250258
}
251259

@@ -254,8 +262,7 @@ public final class BottomSheetView: UIView {
254262
/// - Parameters:
255263
/// - index: the index of the target height
256264
public func transition(to index: Int) {
257-
guard contentHeights.indices.contains(index) else {
258-
assertionFailure("Provided index is out of bounds of the array with target heights.")
265+
guard let height = contentHeights[safe: index] else {
259266
return
260267
}
261268

@@ -266,7 +273,7 @@ public final class BottomSheetView: UIView {
266273
let offset = BottomSheetCalculator.offset(
267274
for: contentView,
268275
in: superview,
269-
height: contentHeights[index],
276+
height: height,
270277
useSafeAreaInsets: useSafeAreaInsets
271278
)
272279

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Foundation
2+
3+
extension Array {
4+
// Returns an Optional that will be nil if index < count
5+
subscript(safe index: Int) -> Element? {
6+
return indices.contains(index) ? self[index] : .none
7+
}
8+
}

0 commit comments

Comments
 (0)