ios: fix search (#5715)

This commit is contained in:
Stanislav Dmitrenko 2025-03-06 18:28:26 +07:00 committed by GitHub
parent a3a27b250c
commit a0560a5ad0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -494,7 +494,7 @@ struct ChatView: View {
Button ("Cancel") {
closeSearch()
Task { await loadChat(chat: chat) }
searchTextChanged("")
}
}
.padding(.horizontal)
@ -585,23 +585,8 @@ struct ChatView: View {
.padding(.vertical, -100)
.onTapGesture { hideKeyboard() }
.onChange(of: searchText) { s in
guard showSearch else { return }
Task {
await loadChat(chat: chat, search: s)
mergedItems.boxedValue = MergedItems.create(im.reversedChatItems, revealedItems, im.chatState)
await MainActor.run {
scrollView.updateItems(mergedItems.boxedValue.items)
}
if !s.isEmpty {
scrollView.scrollToBottom()
} else if let index = scrollView.listState.items.lastIndex(where: { $0.hasUnread() }) {
// scroll to the top unread item
scrollView.scrollToItem(index)
loadLastItems($loadingMoreItems, loadingBottomItems: $loadingBottomItems, chat)
} else {
scrollView.scrollToBottom()
loadLastItems($loadingMoreItems, loadingBottomItems: $loadingBottomItems, chat)
}
if showSearch {
searchTextChanged(s)
}
}
.onChange(of: im.itemAdded) { added in
@ -652,6 +637,26 @@ struct ChatView: View {
}
}
private func searchTextChanged(_ s: String) {
Task {
await loadChat(chat: chat, search: s)
mergedItems.boxedValue = MergedItems.create(im.reversedChatItems, revealedItems, im.chatState)
await MainActor.run {
scrollView.updateItems(mergedItems.boxedValue.items)
}
if !s.isEmpty {
scrollView.scrollToBottom()
} else if let index = scrollView.listState.items.lastIndex(where: { $0.hasUnread() }) {
// scroll to the top unread item
scrollView.scrollToItem(index)
loadLastItems($loadingMoreItems, loadingBottomItems: $loadingBottomItems, chat)
} else {
scrollView.scrollToBottom()
loadLastItems($loadingMoreItems, loadingBottomItems: $loadingBottomItems, chat)
}
}
}
class FloatingButtonModel: ObservableObject {
@Published var unreadAbove: Int = 0
@Published var unreadBelow: Int = 0