handle retained items

This commit is contained in:
Levitating Pineapple
2024-08-16 21:21:34 +03:00
parent 86fd8cf5a1
commit 299766a758
2 changed files with 19 additions and 11 deletions
+8 -8
View File
@@ -432,14 +432,14 @@ struct ChatView: View {
.onChange(of: im.reversedChatItems) { _ in
floatingButtonModel.chatItemsChanged()
}
.onChange(of: im.itemAdded) { added in
if added {
im.itemAdded = false
if floatingButtonModel.unreadChatItemCounts.isReallyNearBottom {
scrollModel.scrollToBottom()
}
}
}
// .onChange(of: im.itemAdded) { added in
// if added {
// im.itemAdded = false
// if floatingButtonModel.unreadChatItemCounts.isReallyNearBottom {
// scrollModel.scrollToBottom()
// }
// }
// }
}
}
+11 -3
View File
@@ -171,19 +171,27 @@ struct ReverseList<Item: Identifiable & Hashable & Sendable, Content: View>: UIV
Task { representer.scrollState = .atDestination }
}
var updateInProgress = false
var itemId: Item.ID?
var retainedItems: [Item]?
var updateInProgress = false
func update(items: [Item]) {
if updateInProgress { return }
if let itemId,
if updateInProgress {
retainedItems = items
return
}
if let itemId,
let i = items.firstIndex(where: { $0.id == itemId }),
i > 0 {
updateInProgress = true
// Update existing items without animation
_update(items: Array(items[i...]), animated: false) {
DispatchQueue.main.async {
// Added items animated by sliding from bottom (.top)
self._update(items: items, animated: true) {
self.updateInProgress = false
// Process update, which might have arrived before completion
if let items = self.retainedItems { self.update(items: items) }
}
}
}