ui: fix throttled chat ordering (#4645)

* ios: fix throttled chat ordering

* optimize

* account for added chats

* revert kotlin change

* dont pop chat that is already on top, unify with addChat

* android, desktop: fix chat ordering

* update

* clear

* fix ios

* refactor sorting
This commit is contained in:
Evgeny 2024-08-10 14:04:37 +01:00 committed by GitHub
parent 9ee74bd36e
commit d970470702
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 118 additions and 40 deletions

View file

@ -228,10 +228,17 @@ final class ChatModel: ObservableObject {
chats.firstIndex(where: { $0.id == id })
}
func addChat(_ chat: Chat, at position: Int = 0) {
withAnimation {
chats.insert(chat, at: position)
func addChat(_ chat: Chat) {
if chatId == nil {
withAnimation { addChat_(chat, at: 0) }
} else {
addChat_(chat, at: 0)
}
popChatCollector.throttlePopChat(chat.chatInfo.id, currentPosition: 0)
}
func addChat_(_ chat: Chat, at position: Int = 0) {
chats.insert(chat, at: position)
}
func updateChatInfo(_ cInfo: ChatInfo) {
@ -305,10 +312,11 @@ final class ChatModel: ObservableObject {
}
}
} else {
addChat(Chat(c), at: i)
addChat_(Chat(c), at: i)
}
}
NtfManager.shared.setNtfBadgeCount(totalUnreadCountForAllUsers())
popChatCollector.clear()
}
// func addGroup(_ group: SimpleXChat.Group) {
@ -335,7 +343,7 @@ final class ChatModel: ObservableObject {
if case .rcvNew = cItem.meta.itemStatus {
unreadCollector.changeUnreadCounter(cInfo.id, by: 1)
}
popChatCollector.addChat(cInfo.id)
popChatCollector.throttlePopChat(cInfo.id, currentPosition: i)
} else {
addChat(Chat(chatInfo: cInfo, chatItems: [cItem]))
}
@ -610,29 +618,61 @@ final class ChatModel: ObservableObject {
class PopChatCollector {
private let subject = PassthroughSubject<Void, Never>()
private var bag = Set<AnyCancellable>()
private var chatsToPop: [ChatId: Date] = [:]
private let popTsComparator = KeyPathComparator<Chat>(\.popTs, order: .reverse)
init() {
subject
.throttle(for: 2, scheduler: DispatchQueue.main, latest: true)
.sink {
let m = ChatModel.shared
if m.chatId == nil {
withAnimation {
m.chats = m.chats.sorted(using: KeyPathComparator(\.popTs, order: .reverse))
}
} else {
m.chats = m.chats.sorted(using: KeyPathComparator(\.popTs, order: .reverse))
}
}
.sink { self.popCollectedChats() }
.store(in: &bag)
}
func addChat(_ chatId: ChatId) {
if let index = ChatModel.shared.getChatIndex(chatId) {
ChatModel.shared.chats[index].popTs = CFAbsoluteTimeGetCurrent()
func throttlePopChat(_ chatId: ChatId, currentPosition: Int) {
let m = ChatModel.shared
if currentPosition > 0 && m.chatId == chatId {
m.chatToTop = chatId
}
if currentPosition > 0 || !chatsToPop.isEmpty {
chatsToPop[chatId] = Date.now
subject.send()
}
}
func clear() {
chatsToPop = [:]
}
func popCollectedChats() {
let m = ChatModel.shared
var ixs: IndexSet = []
var chs: [Chat] = []
// collect chats that received updates
for (chatId, popTs) in self.chatsToPop {
// Currently opened chat is excluded, removing it from the list would navigate out of it
// It will be popped to top later when user exits from the list.
if m.chatId != chatId, let i = m.getChatIndex(chatId) {
ixs.insert(i)
let ch = m.chats[i]
ch.popTs = popTs
chs.append(ch)
}
}
let removeInsert = {
m.chats.remove(atOffsets: ixs)
// sort chats by pop timestamp in descending order
m.chats.insert(contentsOf: chs.sorted(using: self.popTsComparator), at: 0)
}
if m.chatId == nil {
withAnimation { removeInsert() }
} else {
removeInsert()
}
self.chatsToPop = [:]
}
}
private func markChatItemRead_(_ i: Int) {
@ -729,6 +769,7 @@ final class ChatModel: ObservableObject {
func popChat(_ id: String) {
if let i = getChatIndex(id) {
// no animation here, for it not to look like it just moved when leaving the chat
popChat_(i)
}
}
@ -848,7 +889,7 @@ final class Chat: ObservableObject, Identifiable, ChatLike {
@Published var chatItems: [ChatItem]
@Published var chatStats: ChatStats
var created = Date.now
var popTs: CFAbsoluteTime?
fileprivate var popTs: Date?
init(_ cData: ChatData) {
self.chatInfo = cData.chatInfo