Skip to content

Commit 5e2dd39

Browse files
committed
Add animated option when perform batchUpdate
1 parent c84ace3 commit 5e2dd39

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

Sources/DataSources/ASCollectionNode+Rx/ASCollectionNode+SectionViewType.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ extension ASCollectionNode: SectionedViewType {
4646
}
4747

4848
public func performBatchUpdates<S: SectionModelType>(_ changes: Changeset<S>, animationConfiguration: AnimationConfiguration) {
49-
self.performBatch(animated: false, updates: {
49+
self.performBatch(animated: true, updates: {
50+
_performBatchUpdates(self, changes: changes, animationConfiguration: animationConfiguration)
51+
}, completion: nil)
52+
}
53+
54+
public func performBatchUpdates<S: SectionModelType>(_ changes: Changeset<S>, animated: Bool, animationConfiguration: AnimationConfiguration) {
55+
self.performBatch(animated: animated, updates: {
5056
_performBatchUpdates(self, changes: changes, animationConfiguration: animationConfiguration)
5157
}, completion: nil)
5258
}

Sources/DataSources/ASCollectionNode+Rx/RxASCollectionAnimatedDataSource.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ open class RxASCollectionAnimatedDataSource<S: AnimatableSectionModelType>: ASCo
1616

1717
public typealias Element = [S]
1818
public var animationConfiguration = AnimationConfiguration()
19+
public var animated: Bool = true
1920

2021
var dataSet = false
2122

@@ -30,27 +31,21 @@ open class RxASCollectionAnimatedDataSource<S: AnimatableSectionModelType>: ASCo
3031
#endif
3132
if !self.dataSet {
3233
self.dataSet = true
33-
DispatchQueue.main.async {
34-
dataSource.setSections(newSections)
35-
collectionNode.reloadData()
36-
}
34+
dataSource.setSections(newSections)
35+
collectionNode.reloadData()
3736
} else {
3837
let oldSections = dataSource.sectionModels
3938
do {
4039
let differences = try differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
41-
DispatchQueue.main.async {
4240

43-
for difference in differences {
44-
dataSource.setSections(difference.finalSections)
45-
collectionNode.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
46-
}
41+
for difference in differences {
42+
dataSource.setSections(difference.finalSections)
43+
collectionNode.performBatchUpdates(difference, animated: self.animated, animationConfiguration: self.animationConfiguration)
4744
}
4845
} catch {
4946
rxDebugFatalError("\(error)")
50-
DispatchQueue.main.async {
51-
self.setSections(newSections)
52-
collectionNode.reloadData()
53-
}
47+
self.setSections(newSections)
48+
collectionNode.reloadData()
5449
}
5550
}
5651
}.on(observedEvent)

Sources/DataSources/ASTableNode+Rx/ASTableNode+SectionedViewType.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ extension ASTableNode: SectionedViewType {
4545
}
4646

4747
public func performBatchUpdates<S: SectionModelType>(_ changes: Changeset<S>, animationConfiguration: AnimationConfiguration) {
48-
self.performBatch(animated: false, updates: {
48+
self.performBatch(animated: true, updates: {
49+
_performBatchUpdates(self, changes: changes, animationConfiguration: animationConfiguration)
50+
}, completion: nil)
51+
}
52+
53+
public func performBatchUpdates<S: SectionModelType>(_ changes: Changeset<S>, animated: Bool, animationConfiguration: AnimationConfiguration) {
54+
self.performBatch(animated: animated, updates: {
4955
_performBatchUpdates(self, changes: changes, animationConfiguration: animationConfiguration)
5056
}, completion: nil)
5157
}

Sources/DataSources/ASTableNode+Rx/RxASTableAnimatedDataSource.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ open class RxASTableAnimatedDataSource<S: AnimatableSectionModelType>: ASTableSe
1616

1717
public typealias Element = [S]
1818
public var animationConfiguration = AnimationConfiguration()
19-
19+
public var animated: Bool = true
20+
2021
var dataSet = false
2122

2223
public override init() {
@@ -30,29 +31,22 @@ open class RxASTableAnimatedDataSource<S: AnimatableSectionModelType>: ASTableSe
3031
#endif
3132
if !self.dataSet {
3233
self.dataSet = true
33-
DispatchQueue.main.async {
34-
dataSource.setSections(newSections)
35-
tableNode.reloadData()
36-
}
34+
dataSource.setSections(newSections)
35+
tableNode.reloadData()
3736
} else {
3837
let oldSections = dataSource.sectionModels
3938
do {
4039
let differences = try differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
41-
DispatchQueue.main.async {
42-
for difference in differences {
43-
dataSource.setSections(difference.finalSections)
44-
tableNode.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
45-
}
46-
tableNode.waitUntilAllUpdatesAreCommitted()
40+
for difference in differences {
41+
dataSource.setSections(difference.finalSections)
42+
tableNode.performBatchUpdates(difference, animated: self.animated, animationConfiguration: self.animationConfiguration)
4743
}
4844
} catch {
4945
rxDebugFatalError("\(error)")
50-
DispatchQueue.main.async {
51-
self.setSections(newSections)
52-
tableNode.reloadData()
53-
}
46+
self.setSections(newSections)
47+
tableNode.reloadData()
5448
}
5549
}
56-
}.on(observedEvent)
50+
}.on(observedEvent)
5751
}
5852
}

Sources/DataSources/SectionedViewType+BatchUpdate.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ func indexSet(_ values: [Int]) -> IndexSet {
2020
func _performBatchUpdates<V: SectionedViewType, S: SectionModelType>(_ view: V, changes: Changeset<S>, animationConfiguration: AnimationConfiguration) {
2121
typealias I = S.Item
2222

23-
view.deleteSections(changes.deletedSections, animationStyle: .none)
23+
view.deleteSections(changes.deletedSections, animationStyle: .fade)
2424
// Updated sections doesn't mean reload entire section, somebody needs to update the section view manually
2525
// otherwise all cells will be reloaded for nothing.
2626
//view.reloadSections(changes.updatedSections, animationStyle: rowAnimation)
27-
view.insertSections(changes.insertedSections, animationStyle: .none)
27+
view.insertSections(changes.insertedSections, animationStyle: .fade)
2828
for (from, to) in changes.movedSections {
2929
view.moveSection(from, to: to)
3030
}
3131

3232
view.deleteItemsAtIndexPaths(
3333
changes.deletedItems.map { IndexPath(item: $0.itemIndex, section: $0.sectionIndex) },
34-
animationStyle: .none
34+
animationStyle: .fade
3535
)
3636
view.insertItemsAtIndexPaths(
3737
changes.insertedItems.map { IndexPath(item: $0.itemIndex, section: $0.sectionIndex) },
38-
animationStyle: .none
38+
animationStyle: .fade
3939
)
4040
view.reloadItemsAtIndexPaths(
4141
changes.updatedItems.map { IndexPath(item: $0.itemIndex, section: $0.sectionIndex) },

0 commit comments

Comments
 (0)