From 84174c84a4b78314d4d8595d24286c6d4500c1a0 Mon Sep 17 00:00:00 2001 From: StephaneMagne Date: Fri, 1 Sep 2023 10:58:50 -0700 Subject: [PATCH 1/2] Update animation extension. --- .../Classes/Public/Delegate/CollectionDataDelegate.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/LiveCollections/Classes/Public/Delegate/CollectionDataDelegate.swift b/Sources/LiveCollections/Classes/Public/Delegate/CollectionDataDelegate.swift index 464d666..4024036 100644 --- a/Sources/LiveCollections/Classes/Public/Delegate/CollectionDataDelegate.swift +++ b/Sources/LiveCollections/Classes/Public/Delegate/CollectionDataDelegate.swift @@ -108,7 +108,7 @@ public extension CollectionDataAnimationDelegate { return .preciseAnimations } - func animateAlongsideUpdate(with duration: TimeInterval) { } + func animateAlongsideUpdate(for state: CollectionDataAnimationState) { } } public extension CollectionSectionDataAnimationDelegate { From 98bbb41fe884a6bf7dc0015df0a2f0253a265c76 Mon Sep 17 00:00:00 2001 From: StephaneMagne Date: Fri, 1 Sep 2023 11:08:27 -0700 Subject: [PATCH 2/2] Update section animation logic. --- .../UIKit+DeltaUpdates.swift | 26 +++++++++---------- .../UIKit+SectionDeltaUpdates.swift | 10 +++---- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+DeltaUpdates.swift b/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+DeltaUpdates.swift index c5a03b5..a2d22dd 100644 --- a/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+DeltaUpdates.swift +++ b/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+DeltaUpdates.swift @@ -103,14 +103,13 @@ extension UITableView: DeltaUpdatableView { public func reloadSections(for sectionUpdates: [SectionUpdate]) { - performBatchUpdates({ [weak self] in + performBatchUpdates(.reload, delegates: sectionUpdates.uniqueAnimationDelegates, { [weak self] in sectionUpdates.forEach { sectionUpdate in sectionUpdate.update() - guard self != nil else { return } + guard let self else { return } let indexSet = IndexSet([sectionUpdate.section]) - reloadSections(indexSet, with: preferredReloadSectionAnimation(for: sectionUpdate.section)) + self.reloadSections(indexSet, with: self.preferredReloadSectionAnimation(for: sectionUpdate.section)) } - sectionUpdates.uniqueAnimationDelegates.forEach { $0.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration) } }, completion: { _ in sectionUpdates.forEach { $0.completion?() } }) @@ -202,11 +201,10 @@ extension UICollectionView: DeltaUpdatableView { let sections = sectionUpdates.map { $0.section } let indexSet = IndexSet(sections) - performBatchUpdates({ [weak self] in + performBatchUpdates(.reload, delegates: sectionUpdates.uniqueAnimationDelegates, { [weak self] in sectionUpdates.forEach { $0.update() } - guard self != nil else { return } - reloadSections(indexSet) - sectionUpdates.uniqueAnimationDelegates.forEach { $0.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration) } + guard let self else { return } + self.reloadSections(indexSet) }, completion: { _ in sectionUpdates.forEach { $0.completion?() } }) @@ -464,13 +462,13 @@ private extension Sequence where Element == SectionUpdate { // MARK: AnimationDelegate Helper -private extension UITableView { +extension UITableView { - func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) { + func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: () -> Void, completion: ((Bool) -> Void)? = nil) { delegates.forEach { $0.animateAlongsideUpdate(for: .immediatelyBefore(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) } performBatchUpdates { - updates?() + updates() delegates.forEach { $0.animateAlongsideUpdate(for: .during(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) } } completion: { value in completion?(value) @@ -481,13 +479,13 @@ private extension UITableView { } } -private extension UICollectionView { +extension UICollectionView { - func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) { + func performBatchUpdates(_ group: CollectionDataAnimationGroup, delegates: [CollectionDataAnimationDelegate], _ updates: () -> Void, completion: ((Bool) -> Void)? = nil) { delegates.forEach { $0.animateAlongsideUpdate(for: .immediatelyBefore(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) } performBatchUpdates { - updates?() + updates() delegates.forEach { $0.animateAlongsideUpdate(for: .during(group: group, duration: TimeInterval.standardCollectionAnimationDuration)) } } completion: { value in completion?(value) diff --git a/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+SectionDeltaUpdates.swift b/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+SectionDeltaUpdates.swift index 41c4211..ceed80b 100644 --- a/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+SectionDeltaUpdates.swift +++ b/Sources/LiveCollections/Classes/Public/UIKit & Foundatin Extensions/UIKit+SectionDeltaUpdates.swift @@ -100,7 +100,7 @@ extension UITableView: SectionDeltaUpdatableView { self.reloadRows(at: automaticReloadIndexPaths, with: self.preferredReloadRowAnimation) } - performBatchUpdates({ [weak self] in + performBatchUpdates(.insertDeleteMove, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak self] in updateData() guard let strongSelf = self else { return } guard strongSelf.isVisibleOnScreen else { @@ -108,13 +108,12 @@ extension UITableView: SectionDeltaUpdatableView { return } deleteMoveInsert() - delegate?.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration) }, completion: { [weak self] _ in guard let strongSelf = self else { completion?() return } - strongSelf.performBatchUpdates({ [weak weakSelf = strongSelf] in + strongSelf.performBatchUpdates(.reload, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak weakSelf = strongSelf] in guard let strongSelf = weakSelf else { return } guard strongSelf.isVisibleOnScreen else { strongSelf.reloadData() @@ -238,7 +237,7 @@ extension UICollectionView: SectionDeltaUpdatableView { } } - performBatchUpdates({ [weak self] in + performBatchUpdates(.insertDeleteMove, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak self] in updateData() guard let strongSelf = self else { return } guard strongSelf.isVisibleOnScreen else { @@ -250,13 +249,12 @@ extension UICollectionView: SectionDeltaUpdatableView { strongSelf.moveItem(at: indexPathPair.source as IndexPath, to: indexPathPair.target as IndexPath) } strongSelf.insertItems(at: itemIndexPathDelta.insertions as [IndexPath]) - delegate?.animateAlongsideUpdate(with: TimeInterval.standardCollectionAnimationDuration) }, completion: { [weak self] _ in guard let strongSelf = self else { completion?() return } - strongSelf.performBatchUpdates({ [weak weakSelf = strongSelf] in + strongSelf.performBatchUpdates(.reload, delegates: delegate.flatMap { [$0 as CollectionDataAnimationDelegate] } ?? [], { [weak weakSelf = strongSelf] in guard let strongSelf = weakSelf else { return } guard strongSelf.isVisibleOnScreen else { strongSelf.reloadData()