forked from mirrors/linphone-iphone
Refactor computeChatRoomsList for better performance
This commit is contained in:
parent
46f5f21216
commit
4641ef680c
3 changed files with 79 additions and 10 deletions
|
|
@ -1355,7 +1355,7 @@ struct ContentView: View {
|
|||
}
|
||||
}
|
||||
.onReceive(pub) { _ in
|
||||
conversationsListViewModel.computeChatRoomsList(filter: "")
|
||||
conversationsListViewModel.updateChatRoomsList()
|
||||
historyListViewModel.refreshHistoryAvatarModel()
|
||||
}
|
||||
}
|
||||
|
|
@ -1381,6 +1381,7 @@ struct ContentView: View {
|
|||
CoreContext.shared.enteredForeground = newPhase == .active
|
||||
orientation = UIDevice.current.orientation
|
||||
if newPhase == .active {
|
||||
conversationsListViewModel.computeChatRoomsList(filter: "")
|
||||
accountProfileViewModel.setAvatarModel()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class ConversationModel: ObservableObject, Identifiable {
|
|||
private var coreContext = CoreContext.shared
|
||||
private var contactsManager = ContactsManager.shared
|
||||
|
||||
let chatRoom: ChatRoom
|
||||
var chatRoom: ChatRoom
|
||||
let isDisabledBecauseNotSecured: Bool = false
|
||||
|
||||
static let TAG = "[Conversation Model]"
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ class ConversationsListViewModel: ObservableObject {
|
|||
var selectedConversation: ConversationModel?
|
||||
|
||||
init() {
|
||||
computeChatRoomsList(filter: "")
|
||||
addConversationDelegate()
|
||||
}
|
||||
|
||||
|
|
@ -46,16 +47,22 @@ class ConversationsListViewModel: ObservableObject {
|
|||
let chatRooms = account != nil ? account!.chatRooms : core.chatRooms
|
||||
|
||||
self.conversationsListTmp = []
|
||||
DispatchQueue.main.async {
|
||||
self.conversationsList = []
|
||||
}
|
||||
|
||||
chatRooms.forEach { chatRoom in
|
||||
if filter.isEmpty {
|
||||
let model = ConversationModel(chatRoom: chatRoom)
|
||||
self.conversationsListTmp.append(model)
|
||||
DispatchQueue.main.async {
|
||||
self.conversationsList.append(model)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.conversationsList = self.conversationsListTmp
|
||||
//self.conversationsList = self.conversationsListTmp
|
||||
NotificationCenter.default.post(name: NSNotification.Name("ChatRoomsComputed"), object: nil)
|
||||
}
|
||||
|
||||
|
|
@ -63,32 +70,93 @@ class ConversationsListViewModel: ObservableObject {
|
|||
}
|
||||
}
|
||||
|
||||
func updateChatRoomsList() {
|
||||
CoreContext.shared.doOnCoreQueue { _ in
|
||||
if !self.conversationsListTmp.isEmpty {
|
||||
self.contactsManager.avatarListModel.forEach { contactAvatarModel in
|
||||
self.conversationsListTmp.forEach { conversationModel in
|
||||
if conversationModel.participantsAddress.contains(contactAvatarModel.address) {
|
||||
if conversationModel.isGroup && conversationModel.participantsAddress.count > 1 {
|
||||
let lastMessage = conversationModel.chatRoom.lastMessageInHistory
|
||||
if lastMessage != nil && lastMessage!.fromAddress != nil && lastMessage!.fromAddress!.asStringUriOnly().contains(contactAvatarModel.address) {
|
||||
var fromAddressFriend = lastMessage!.fromAddress != nil
|
||||
? self.contactsManager.getFriendWithAddress(address: lastMessage!.fromAddress)?.name ?? nil
|
||||
: nil
|
||||
|
||||
if !lastMessage!.isOutgoing && lastMessage!.chatRoom != nil && !lastMessage!.chatRoom!.hasCapability(mask: ChatRoom.Capabilities.OneToOne.rawValue) {
|
||||
if fromAddressFriend == nil {
|
||||
if lastMessage!.fromAddress!.displayName != nil {
|
||||
fromAddressFriend = lastMessage!.fromAddress!.displayName! + ": "
|
||||
} else if lastMessage!.fromAddress!.username != nil {
|
||||
fromAddressFriend = lastMessage!.fromAddress!.username! + ": "
|
||||
} else {
|
||||
fromAddressFriend = String(lastMessage!.fromAddress!.asStringUriOnly().dropFirst(4)) + ": "
|
||||
}
|
||||
} else {
|
||||
fromAddressFriend! += ": "
|
||||
}
|
||||
|
||||
} else {
|
||||
fromAddressFriend = nil
|
||||
}
|
||||
let lastMessageTextTmp = (fromAddressFriend ?? "")
|
||||
+ (lastMessage!.contents.first(where: {$0.isText == true})?.utf8Text ?? (lastMessage!.contents.first(where: {$0.isFile == true || $0.isFileTransfer == true})?.name ?? ""))
|
||||
|
||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom })
|
||||
|
||||
DispatchQueue.main.async {
|
||||
conversationModel.lastMessageText = lastMessageTextTmp
|
||||
if index != nil {
|
||||
self.conversationsList[index!].lastMessageText = lastMessageTextTmp
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if !conversationModel.isGroup && conversationModel.participantsAddress.first != nil && conversationModel.participantsAddress.first!.contains(contactAvatarModel.address) {
|
||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === conversationModel.chatRoom })
|
||||
DispatchQueue.main.async {
|
||||
conversationModel.avatarModel = contactAvatarModel
|
||||
conversationModel.subject = contactAvatarModel.name
|
||||
if index != nil {
|
||||
self.conversationsList[index!].avatarModel = contactAvatarModel
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func addConversationDelegate() {
|
||||
coreContext.doOnCoreQueue { core in
|
||||
self.coreConversationDelegate = CoreDelegateStub(onMessagesReceived: { (_: Core, chatRoom: ChatRoom, _: [ChatMessage]) in
|
||||
let model = ConversationModel(chatRoom: chatRoom)
|
||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === chatRoom })
|
||||
DispatchQueue.main.async {
|
||||
if let index = self.conversationsList.firstIndex(where: { $0.chatRoom === chatRoom }) {
|
||||
self.conversationsList.remove(at: index)
|
||||
if index != nil {
|
||||
self.conversationsList.remove(at: index!)
|
||||
}
|
||||
self.conversationsList.insert(model, at: 0)
|
||||
}
|
||||
self.updateUnreadMessagesCount()
|
||||
}, onMessageSent: { (_: Core, chatRoom: ChatRoom, _: ChatMessage) in
|
||||
let model = ConversationModel(chatRoom: chatRoom)
|
||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === chatRoom })
|
||||
DispatchQueue.main.async {
|
||||
if let index = self.conversationsList.firstIndex(where: { $0.chatRoom === chatRoom }) {
|
||||
self.conversationsList.remove(at: index)
|
||||
if index != nil {
|
||||
self.conversationsList.remove(at: index!)
|
||||
}
|
||||
self.conversationsList.insert(model, at: 0)
|
||||
}
|
||||
self.updateUnreadMessagesCount()
|
||||
}, onChatRoomRead: { (_: Core, chatRoom: ChatRoom) in
|
||||
let model = ConversationModel(chatRoom: chatRoom)
|
||||
let index = self.conversationsList.firstIndex(where: { $0.chatRoom === chatRoom })
|
||||
DispatchQueue.main.async {
|
||||
if let index = self.conversationsList.firstIndex(where: { $0.chatRoom === chatRoom }) {
|
||||
self.conversationsList.remove(at: index)
|
||||
self.conversationsList.insert(model, at: index)
|
||||
if index != nil {
|
||||
self.conversationsList.remove(at: index!)
|
||||
self.conversationsList.insert(model, at: index!)
|
||||
} else {
|
||||
self.conversationsList.insert(model, at: 0)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue