Refactoring of addChatRoom and removeChatRoom functions

This commit is contained in:
benoit.martins 2025-05-02 14:54:44 +02:00
parent 713526ac57
commit 7d6b2d8e0b
5 changed files with 23 additions and 45 deletions

View file

@ -1425,7 +1425,7 @@ struct ContentView: View {
.onChange(of: scenePhase) { newPhase in
orientation = UIDevice.current.orientation
if newPhase == .active {
conversationsListViewModel.computeChatRoomsList(filter: "")
conversationsListViewModel.computeChatRoomsList()
}
}
}

View file

@ -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 {

View file

@ -646,7 +646,6 @@ struct ConversationInfoFragment: View {
Button(
action: {
conversationViewModel.displayedConversation!.deleteChatRoom()
conversationsListViewModel.computeChatRoomsList(filter: "")
conversationViewModel.displayedConversation = nil
},
label: {

View file

@ -177,7 +177,6 @@ struct ConversationsListBottomSheet: View {
Button {
conversationsListViewModel.selectedConversation!.deleteChatRoom()
conversationsListViewModel.computeChatRoomsList(filter: "")
if #available(iOS 16.0, *) {
if idiom != .pad {

View file

@ -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<ConversationModel>()
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")