Skip to content

Commit 11aef3e

Browse files
authored
Support ignoring scroll while tracking (#14)
1 parent 66ce09e commit 11aef3e

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

Sources/DynamicList/DynamicList.swift

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ public enum Selection<Data: Hashable> {
88

99
public struct DynamicList<Section: Hashable, Item: Hashable>: UIViewRepresentable {
1010

11+
public struct ScrollTarget {
12+
public let item: Item
13+
public let position: UICollectionView.ScrollPosition
14+
public let animated: Bool
15+
public let skipsWhileTracking: Bool
16+
17+
public init(
18+
item: Item,
19+
position: UICollectionView.ScrollPosition = .centeredVertically,
20+
skipsWhileTracking: Bool = false,
21+
animated: Bool
22+
) {
23+
self.item = item
24+
self.position = position
25+
self.animated = animated
26+
self.skipsWhileTracking = skipsWhileTracking
27+
}
28+
}
29+
1130
private var selection: Binding<Selection<Item>?>?
1231

1332
public typealias SelectionAction = DynamicListView<Section, Item>.SelectionAction
@@ -26,7 +45,7 @@ public struct DynamicList<Section: Hashable, Item: Hashable>: UIViewRepresentabl
2645
private let contentInsetAdjustmentBehavior: UIScrollView.ContentInsetAdjustmentBehavior
2746
private let cellStates: [Item: CellState]
2847

29-
private var scrollingTarget: Item?
48+
private var scrollingTarget: ScrollTarget?
3049

3150
public init(
3251
snapshot: NSDiffableDataSourceSnapshot<Section, Item>,
@@ -48,7 +67,7 @@ public struct DynamicList<Section: Hashable, Item: Hashable>: UIViewRepresentabl
4867
self.cellStates = cellStates
4968
}
5069

51-
public func scrolling(to item: Item?) -> Self {
70+
public func scrolling(to item: ScrollTarget?) -> Self {
5271
var modified = self
5372
modified.scrollingTarget = item
5473
return modified
@@ -75,10 +94,12 @@ public struct DynamicList<Section: Hashable, Item: Hashable>: UIViewRepresentabl
7594
listView.setContents(snapshot: snapshot)
7695

7796
if let scrollingTarget {
97+
7898
listView.scroll(
79-
to: scrollingTarget,
80-
at: .centeredVertically,
81-
animated: false
99+
to: scrollingTarget.item,
100+
at: scrollingTarget.position,
101+
skipsWhileTracking: scrollingTarget.skipsWhileTracking,
102+
animated: scrollingTarget.animated
82103
)
83104
}
84105

@@ -117,12 +138,12 @@ public struct DynamicList<Section: Hashable, Item: Hashable>: UIViewRepresentabl
117138

118139
if let scrollingTarget {
119140
listView.scroll(
120-
to: scrollingTarget,
121-
at: .centeredVertically,
122-
animated: true
141+
to: scrollingTarget.item,
142+
at: scrollingTarget.position,
143+
skipsWhileTracking: scrollingTarget.skipsWhileTracking,
144+
animated: scrollingTarget.animated
123145
)
124146
}
125-
126147
}
127148

128149
public func selectionHandler(

Sources/DynamicList/DynamicListView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,17 @@ public final class DynamicListView<Section: Hashable, Data: Hashable>: UIView,
546546
public func scroll(
547547
to data: Data,
548548
at scrollPosition: UICollectionView.ScrollPosition,
549+
skipsWhileTracking: Bool = false,
549550
animated: Bool
550551
) {
551552
guard let indexPath = dataSource.indexPath(for: data) else {
552553
return
553554
}
554555

556+
if skipsWhileTracking == true, _collectionView.isTracking == true {
557+
return
558+
}
559+
555560
_collectionView.scrollToItem(at: indexPath, at: scrollPosition, animated: animated)
556561
}
557562

0 commit comments

Comments
 (0)