fix scroll to bottom, on insert

This commit is contained in:
Levitating Pineapple
2024-08-16 21:48:41 +03:00
parent 299766a758
commit d76b321919
3 changed files with 22 additions and 15 deletions
-7
View File
@@ -50,9 +50,6 @@ class ItemsModel: ObservableObject {
var reversedChatItems: [ChatItem] = [] {
willSet { publisher.send() }
}
var itemAdded = false {
willSet { publisher.send() }
}
// Publishes directly to `objectWillChange` publisher,
// this will cause reversedChatItems to be rendered without throttling
@@ -456,7 +453,6 @@ final class ChatModel: ObservableObject {
ci.meta.itemStatus = status
}
im.reversedChatItems.insert(ci, at: hasLiveDummy ? 1 : 0)
im.itemAdded = true
}
return true
}
@@ -551,7 +547,6 @@ final class ChatModel: ObservableObject {
let cItem = ChatItem.liveDummy(chatInfo.chatType)
withAnimation {
im.reversedChatItems.insert(cItem, at: 0)
im.itemAdded = true
}
return cItem
}
@@ -915,7 +910,6 @@ final class ChatModel: ObservableObject {
// TODO these thresholds account for the fact that items are still "visible" while
// covered by compose area, they should be replaced with the actual height in pixels below the screen.
isNearBottom: totalBelow < 15,
isReallyNearBottom: totalBelow < 2,
unreadBelow: unreadBelow
)
}
@@ -942,7 +936,6 @@ struct NTFContactRequest {
struct UnreadChatItemCounts: Equatable {
var isNearBottom: Bool
var isReallyNearBottom: Bool
var unreadBelow: Int
}
@@ -472,7 +472,6 @@ struct ChatView: View {
init() {
unreadChatItemCounts = UnreadChatItemCounts(
isNearBottom: true,
isReallyNearBottom: true,
unreadBelow: 0
)
events
+22 -7
View File
@@ -163,17 +163,31 @@ struct ReverseList<Item: Identifiable & Hashable & Sendable, Content: View>: UIV
animated: animated
)
} else {
tableView.setContentOffset(
CGPoint(x: .zero, y: -InvertedTableView.inset),
animated: animated
)
scrollToBottom(animated: animated)
}
Task { representer.scrollState = .atDestination }
}
var itemId: Item.ID?
var retainedItems: [Item]?
var updateInProgress = false
private func scrollToBottom(animated: Bool) {
tableView.setContentOffset(
CGPoint(x: .zero, y: -InvertedTableView.inset),
animated: animated
)
}
private var isNearBottom: Bool {
let adjustedOffset = tableView.contentOffset.y + InvertedTableView.inset
return adjustedOffset > 0 && adjustedOffset < 500
}
private var isFarFromBottom: Bool {
let adjustedOffset = tableView.contentOffset.y + InvertedTableView.inset
return adjustedOffset > 1000
}
private var itemId: Item.ID?
private var retainedItems: [Item]?
private var updateInProgress = false
func update(items: [Item]) {
if updateInProgress {
@@ -190,6 +204,7 @@ struct ReverseList<Item: Identifiable & Hashable & Sendable, Content: View>: UIV
// Added items animated by sliding from bottom (.top)
self._update(items: items, animated: true) {
self.updateInProgress = false
if self.isNearBottom { self.scrollToBottom(animated: true) }
// Process update, which might have arrived before completion
if let items = self.retainedItems { self.update(items: items) }
}