From d5090c3df2be48e6eed4b75fd653df4cff47ac51 Mon Sep 17 00:00:00 2001 From: Levitating Pineapple Date: Sat, 17 Aug 2024 22:45:36 +0300 Subject: [PATCH] animate row removal --- apps/ios/Shared/Views/Chat/ReverseList.swift | 26 +++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/apps/ios/Shared/Views/Chat/ReverseList.swift b/apps/ios/Shared/Views/Chat/ReverseList.swift index 8ecde8fe6c..344b2e3691 100644 --- a/apps/ios/Shared/Views/Chat/ReverseList.swift +++ b/apps/ios/Shared/Views/Chat/ReverseList.swift @@ -207,12 +207,16 @@ struct ReverseList: UIV i > 0 { updateInProgress = true // Update existing items without animation - _update(items: Array(items[i...]), animated: false) { + _update(items: Array(items[i...])) { DispatchQueue.main.async { // Added items animated by sliding from bottom (.top) - self._update(items: items, animated: self.isAtBottom) { + self._update( + items: items, + animated: self.isAtBottom, + rowAnimation: self.isAtBottom ? .top : .none + ) { self.updateInProgress = false - if self.isNearBottom { self.scrollToBottom(animated: true) } + if self.isNearBottom { self.scrollToBottom(animated: false) } // Process update, which might have arrived before completion if let items = self.retainedItems { self.retainedItems = nil @@ -222,17 +226,27 @@ struct ReverseList: UIV } } } else { - _update(items: items, animated: false) + let wasItemsRemoved = !items.isEmpty && items.count < itemCount + _update( + items: items, + animated: wasItemsRemoved, + rowAnimation: wasItemsRemoved ? .fade : .none + ) } itemId = items.first?.id itemCount = items.count } - private func _update(items: [Item], animated: Bool, completion: (() -> Void)? = nil) { + private func _update( + items: [Item], + animated: Bool = false, + rowAnimation: UITableView.RowAnimation = .none, + completion: (() -> Void)? = nil + ) { var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([.main]) snapshot.appendItems(items) - dataSource.defaultRowAnimation = animated ? .top : .none + dataSource.defaultRowAnimation = rowAnimation dataSource.apply(snapshot, animatingDifferences: animated, completion: completion) } }