diff --git a/Linphone/UI/Main/ContentView.swift b/Linphone/UI/Main/ContentView.swift index 3b3651504..0d34434ce 100644 --- a/Linphone/UI/Main/ContentView.swift +++ b/Linphone/UI/Main/ContentView.swift @@ -1425,7 +1425,7 @@ struct ContentView: View { .onChange(of: scenePhase) { newPhase in orientation = UIDevice.current.orientation if newPhase == .active { - conversationsListViewModel.computeChatRoomsList(filter: "") + conversationsListViewModel.computeChatRoomsList() } } } diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift index f7abc1b97..c0af89ce9 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationFragment.swift @@ -395,7 +395,6 @@ struct ConversationFragment: View { if index == 0 { displayFloatingButton = false conversationViewModel.markAsRead() - conversationsListViewModel.computeChatRoomsList(filter: "") } } .onDisappear { @@ -410,7 +409,6 @@ struct ConversationFragment: View { .listStyle(.plain) .onAppear { conversationViewModel.markAsRead() - conversationsListViewModel.computeChatRoomsList(filter: "") } if displayFloatingButton { diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationInfoFragment.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationInfoFragment.swift index 5fdcd0f5c..403ef31d7 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationInfoFragment.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationInfoFragment.swift @@ -646,7 +646,6 @@ struct ConversationInfoFragment: View { Button( action: { conversationViewModel.displayedConversation!.deleteChatRoom() - conversationsListViewModel.computeChatRoomsList(filter: "") conversationViewModel.displayedConversation = nil }, label: { diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift index 0bef649cd..c3473cafa 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift @@ -177,7 +177,6 @@ struct ConversationsListBottomSheet: View { Button { conversationsListViewModel.selectedConversation!.deleteChatRoom() - conversationsListViewModel.computeChatRoomsList(filter: "") if #available(iOS 16.0, *) { if idiom != .pad { diff --git a/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift b/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift index 8b45d6e47..7c362e3f1 100644 --- a/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift +++ b/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift @@ -40,29 +40,18 @@ class ConversationsListViewModel: ObservableObject { var currentFilter: String = "" init() { - computeChatRoomsList(filter: "") + computeChatRoomsList() addConversationDelegate() } - func computeChatRoomsList(filter: String) { + func computeChatRoomsList() { coreContext.doOnCoreQueue { core in if let account = core.defaultAccount { - //let chatRooms = account != nil ? account.chatRooms : core.chatRooms - - self.currentFilter = filter - DispatchQueue.main.async { - self.conversationsList = [] - } - - let conversationsListTmp = account.filterChatRooms(filter: filter) + let conversationsListTmp = account.filterChatRooms(filter: self.currentFilter) var conversationsTmp: [ConversationModel] = [] - var count = 0 conversationsListTmp.forEach { chatRoom in - if filter.isEmpty { - let model = ConversationModel(chatRoom: chatRoom) - conversationsTmp.append(model) - count += 1 - } + let model = ConversationModel(chatRoom: chatRoom) + conversationsTmp.append(model) } DispatchQueue.main.async { @@ -234,11 +223,9 @@ class ConversationsListViewModel: ObservableObject { if core.globalState == .On { switch state { case .Created: - print("") - //self.addChatRoom(chatRoom: chatroom) - //self.computeChatRoomsList(filter: "") + self.addChatRoom(chatRoom: chatroom) case .Deleted: - self.computeChatRoomsList(filter: "") + self.removeChatRoom(chatRoom: chatroom) default: break } @@ -293,30 +280,25 @@ class ConversationsListViewModel: ObservableObject { } } - /* - @WorkerThread - private fun removeChatRoom(chatRoom: ChatRoom) { - val currentList = conversations.value.orEmpty() - val identifier = chatRoom.identifier - val found = currentList.find { - it.chatRoom.identifier == identifier - } - if (found != null) { - val newList = arrayListOf() - newList.addAll(currentList) - newList.remove(found) - found.destroy() - Log.i("$TAG Removing chat room with identifier [$identifier] from list") - conversations.postValue(newList) + private func removeChatRoom(chatRoom: ChatRoom) { + let currentList = conversationsList + let identifier = chatRoom.identifier + let foundIndex = currentList.firstIndex(where: {$0.chatRoom.identifier == identifier}) + if foundIndex != nil { + var newList: [ConversationModel] = [] + newList.append(contentsOf: currentList) + newList.remove(at: foundIndex!) + Log.info("\(ConversationsListViewModel.TAG) Removing chat room with identifier \(identifier ?? "Identifier error") from list") + + DispatchQueue.main.async { + self.conversationsList = newList + } } else { - Log.w( - "$TAG Failed to find item in list matching deleted chat room identifier [$identifier]" + Log.warn( + "\(ConversationsListViewModel.TAG) Failed to find item in list matching deleted chat room identifier \(identifier ?? "Identifier error")" ) } - - showGreenToast(R.string.conversation_deleted_toast, R.drawable.chat_teardrop_text) } - */ func reorderChatRooms() { Log.info("[ConversationsListViewModel] Re-ordering conversations")