Skip to content

Commit fb4f611

Browse files
author
Stephane Magne
authored
Merge pull request #12 from scribd/stephane/update_extension
Update animation extension
2 parents 048f522 + 98bbb41 commit fb4f611

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

Sources/LiveCollections/Classes/Public/Delegate/CollectionDataDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public extension CollectionDataAnimationDelegate {
108108
return .preciseAnimations
109109
}
110110

111-
func animateAlongsideUpdate(with duration: TimeInterval) { }
111+
func animateAlongsideUpdate(for state: CollectionDataAnimationState) { }
112112
}
113113

114114
public extension CollectionSectionDataAnimationDelegate {

Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+DeltaUpdates.swift

+12-14
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,13 @@ extension UITableView: DeltaUpdatableView {
103103

104104
public func reloadSections(for sectionUpdates: [SectionUpdate]) {
105105

106-
performBatchUpdates({ [weak self] in
106+
performBatchUpdates(.reload, delegates: sectionUpdates.uniqueAnimationDelegates, { [weak self] in
107107
sectionUpdates.forEach { sectionUpdate in
108108
sectionUpdate.update()
109-
guard self != nil else { return }
109+
guard let self else { return }
110110
let indexSet = IndexSet([sectionUpdate.section])
111-
reloadSections(indexSet, with: preferredReloadSectionAnimation(for: sectionUpdate.section))
111+
self.reloadSections(indexSet, with: self.preferredReloadSectionAnimation(for: sectionUpdate.section))
112112
}
113-
sectionUpdates.uniqueAnimationDelegates.forEach { $0.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration) }
114113
}, completion: { _ in
115114
sectionUpdates.forEach { $0.completion?() }
116115
})
@@ -202,11 +201,10 @@ extension UICollectionView: DeltaUpdatableView {
202201
let sections = sectionUpdates.map { $0.section }
203202
let indexSet = IndexSet(sections)
204203

205-
performBatchUpdates({ [weak self] in
204+
performBatchUpdates(.reload, delegates: sectionUpdates.uniqueAnimationDelegates, { [weak self] in
206205
sectionUpdates.forEach { $0.update() }
207-
guard self != nil else { return }
208-
reloadSections(indexSet)
209-
sectionUpdates.uniqueAnimationDelegates.forEach { $0.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration) }
206+
guard let self else { return }
207+
self.reloadSections(indexSet)
210208
}, completion: { _ in
211209
sectionUpdates.forEach { $0.completion?() }
212210
})
@@ -464,13 +462,13 @@ private extension Sequence where Element == SectionUpdate {
464462

465463
// MARK: AnimationDelegate Helper
466464

467-
private extension UITableView {
465+
extension UITableView {
468466

469-
func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) {
467+
func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: () -> Void, completion: ((Bool) -> Void)? = nil) {
470468
delegates.forEach { $0.animateAlongsideUpdate(for: .immediatelyBefore(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) }
471469

472470
performBatchUpdates {
473-
updates?()
471+
updates()
474472
delegates.forEach { $0.animateAlongsideUpdate(for: .during(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) }
475473
} completion: { value in
476474
completion?(value)
@@ -481,13 +479,13 @@ private extension UITableView {
481479
}
482480
}
483481

484-
private extension UICollectionView {
482+
extension UICollectionView {
485483

486-
func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) {
484+
func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: () -> Void, completion: ((Bool) -> Void)? = nil) {
487485
delegates.forEach { $0.animateAlongsideUpdate(for: .immediatelyBefore(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) }
488486

489487
performBatchUpdates {
490-
updates?()
488+
updates()
491489
delegates.forEach { $0.animateAlongsideUpdate(for: .during(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) }
492490
} completion: { value in
493491
completion?(value)

Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+SectionDeltaUpdates.swift

+4-6
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,20 @@ extension UITableView: SectionDeltaUpdatableView {
100100
self.reloadRows(at: automaticReloadIndexPaths, with: self.preferredReloadRowAnimation)
101101
}
102102

103-
performBatchUpdates({ [weak self] in
103+
performBatchUpdates(.insertDeleteMove, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak self] in
104104
updateData()
105105
guard let strongSelf = self else { return }
106106
guard strongSelf.isVisibleOnScreen else {
107107
strongSelf.reloadData()
108108
return
109109
}
110110
deleteMoveInsert()
111-
delegate?.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration)
112111
}, completion: { [weak self] _ in
113112
guard let strongSelf = self else {
114113
completion?()
115114
return
116115
}
117-
strongSelf.performBatchUpdates({ [weak weakSelf = strongSelf] in
116+
strongSelf.performBatchUpdates(.reload, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak weakSelf = strongSelf] in
118117
guard let strongSelf = weakSelf else { return }
119118
guard strongSelf.isVisibleOnScreen else {
120119
strongSelf.reloadData()
@@ -238,7 +237,7 @@ extension UICollectionView: SectionDeltaUpdatableView {
238237
}
239238
}
240239

241-
performBatchUpdates({ [weak self] in
240+
performBatchUpdates(.insertDeleteMove, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak self] in
242241
updateData()
243242
guard let strongSelf = self else { return }
244243
guard strongSelf.isVisibleOnScreen else {
@@ -250,13 +249,12 @@ extension UICollectionView: SectionDeltaUpdatableView {
250249
strongSelf.moveItem(at: indexPathPair.source as IndexPath, to: indexPathPair.target as IndexPath)
251250
}
252251
strongSelf.insertItems(at: itemIndexPathDelta.insertions as [IndexPath])
253-
delegate?.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration)
254252
}, completion: { [weak self] _ in
255253
guard let strongSelf = self else {
256254
completion?()
257255
return
258256
}
259-
strongSelf.performBatchUpdates({ [weak weakSelf = strongSelf] in
257+
strongSelf.performBatchUpdates(.reload, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak weakSelf = strongSelf] in
260258
guard let strongSelf = weakSelf else { return }
261259
guard strongSelf.isVisibleOnScreen else {
262260
strongSelf.reloadData()

0 commit comments

Comments
 (0)