diff --git a/apps/ios/Shared/Views/Chat/ReverseList.swift b/apps/ios/Shared/Views/Chat/ReverseList.swift index e5220cab93..2befab8634 100644 --- a/apps/ios/Shared/Views/Chat/ReverseList.swift +++ b/apps/ios/Shared/Views/Chat/ReverseList.swift @@ -63,11 +63,31 @@ struct ReverseList: UIViewControllerRepresentable { // so it's better to just wait until dragging ends if !mergedItems.splits.isEmpty && controller.tableView.isDragging/* && !controller.tableView.isDecelerating*/ { controller.runBlockOnEndDecelerating = { - controller.update(mergedItems.items, mergedItems.snapshot) + Task { + let prevSnapshot = controller.dataSource.snapshot() + if mergedItems.items == prevSnapshot.itemIdentifiers { + logger.debug("LALAL SAME ITEMS, not rebuilding the tableview") + // update counters because they are static, unbound to specific chat and will become outdated if a new empty chat was open after non-empty one with unread messages + controller.updateFloatingButtons.send() + return + } + await MainActor.run { + controller.update(mergedItems.items, mergedItems.snapshot, prevSnapshot) + } + } } } else { controller.runBlockOnEndDecelerating = nil - controller.update(mergedItems.items, mergedItems.snapshot) + Task { + let prevSnapshot = controller.dataSource.snapshot() + if mergedItems.items == prevSnapshot.itemIdentifiers { + logger.debug("LALAL SAME ITEMS, not rebuilding the tableview") + // update counters because they are static, unbound to specific chat and will become outdated if a new empty chat was open after non-empty one with unread messages + controller.updateFloatingButtons.send() + return + } + controller.update(mergedItems.items, mergedItems.snapshot, prevSnapshot) + } } } } @@ -75,8 +95,8 @@ struct ReverseList: UIViewControllerRepresentable { /// Controller, which hosts SwiftUI cells public class Controller: UITableViewController { var representer: ReverseList - private var dataSource: UITableViewDiffableDataSource! - private let updateFloatingButtons = PassthroughSubject() + var dataSource: UITableViewDiffableDataSource! + let updateFloatingButtons = PassthroughSubject() private var bag = Set() var runBlockOnEndDecelerating: (() -> Void)? = nil @@ -256,22 +276,18 @@ struct ReverseList: UIViewControllerRepresentable { } } - func update(_ items: [MergedItem], _ snapshot: NSDiffableDataSourceSnapshot) { - logger.debug("LALAL STEP 0 \(items.count)") - let prevSnapshot = dataSource.snapshot() - logger.debug("LALAL STEP 1") + func update( + _ items: [MergedItem], + _ snapshot: NSDiffableDataSourceSnapshot, + _ prevSnapshot: NSDiffableDataSourceSnapshot + ) { + logger.debug("LALAL STEP 1 \(items.count)") let wasCount = prevSnapshot.numberOfItems let willBeCount = items.count let c = 1 let insertedSeveralNewestItems = wasCount != 0 && willBeCount - wasCount == c && prevSnapshot.itemIdentifiers.first!.hashValue == items[c].hashValue logger.debug("LALAL STEP 2") logger.debug("LALAL WAS \(wasCount) will be \(items.count)") - if items == prevSnapshot.itemIdentifiers { - logger.debug("LALAL SAME ITEMS, not rebuilding the tableview") - // update counters because they are static, unbound to specific chat and will become outdated if a new empty chat was open after non-empty one with unread messages - updateFloatingButtons.send() - return - } // var snapshot = NSDiffableDataSourceSnapshot() // snapshot.appendSections([.main]) // snapshot.appendItems(items)