diff --git a/Linphone/Core/CoreContext.swift b/Linphone/Core/CoreContext.swift index 7c6dc5bae..c5b30fc74 100644 --- a/Linphone/Core/CoreContext.swift +++ b/Linphone/Core/CoreContext.swift @@ -371,6 +371,19 @@ class CoreContext: ObservableObject { } } + }, onDefaultAccountChanged: { (_: Core, account: Account?) in + Log.info("[CoreContext][onDefaultAccountChanged] Default account set to: \(account?.displayName() ?? "none")") + if let account = account { + DispatchQueue.main.async { + for accountModel in self.accounts { + accountModel.isDefaultAccount = accountModel.account == account + } + } + } + DispatchQueue.main.async { + NotificationCenter.default.post(name: NSNotification.Name("DefaultAccountChanged"), object: nil) + } + ContactsManager.shared.fetchContacts() }, onAccountAdded: { (_: Core, acc: Account) in self.forceRemotePushToMatchVoipPushSettings(account: acc) @@ -390,6 +403,7 @@ class CoreContext: ObservableObject { self.accounts = accountModels } }) + self.mCore.addDelegate(delegate: self.mCoreDelegate) self.mCore.autoIterateEnabled = true diff --git a/Linphone/UI/Assistant/Fragments/LoginFragment.swift b/Linphone/UI/Assistant/Fragments/LoginFragment.swift index a1f48084a..c494099cb 100644 --- a/Linphone/UI/Assistant/Fragments/LoginFragment.swift +++ b/Linphone/UI/Assistant/Fragments/LoginFragment.swift @@ -147,7 +147,6 @@ struct LoginFragment: View { .default_text_style(styleSize: 15) .disableAutocorrection(true) .autocapitalization(.none) - .disabled(coreContext.loggedIn) .frame(height: 25) .padding(.horizontal, 20) .padding(.vertical, 15) @@ -191,7 +190,6 @@ struct LoginFragment: View { .frame(width: 20, height: 20) }) } - .disabled(coreContext.loggedIn) .padding(.horizontal, 20) .padding(.vertical, 15) .cornerRadius(60) @@ -207,7 +205,7 @@ struct LoginFragment: View { self.accountLoginViewModel.login() coreContext.loggingInProgress = true }, label: { - Text(coreContext.loggedIn ? "manage_account_delete" : "assistant_account_login") + Text("assistant_account_login") .default_text_style_white_600(styleSize: 20) .frame(height: 35) .frame(maxWidth: .infinity) diff --git a/Linphone/UI/Assistant/Fragments/ThirdPartySipAccountLoginFragment.swift b/Linphone/UI/Assistant/Fragments/ThirdPartySipAccountLoginFragment.swift index 0aa077925..1ced5b2a7 100644 --- a/Linphone/UI/Assistant/Fragments/ThirdPartySipAccountLoginFragment.swift +++ b/Linphone/UI/Assistant/Fragments/ThirdPartySipAccountLoginFragment.swift @@ -88,7 +88,6 @@ struct ThirdPartySipAccountLoginFragment: View { .default_text_style(styleSize: 15) .disableAutocorrection(true) .autocapitalization(.none) - .disabled(coreContext.loggedIn) .frame(height: 25) .padding(.horizontal, 20) .padding(.vertical, 15) @@ -131,7 +130,6 @@ struct ThirdPartySipAccountLoginFragment: View { .frame(width: 20, height: 20) }) } - .disabled(coreContext.loggedIn) .padding(.horizontal, 20) .padding(.vertical, 15) .cornerRadius(60) @@ -150,7 +148,6 @@ struct ThirdPartySipAccountLoginFragment: View { .default_text_style(styleSize: 15) .disableAutocorrection(true) .autocapitalization(.none) - .disabled(coreContext.loggedIn) .frame(height: 25) .padding(.horizontal, 20) .padding(.vertical, 15) @@ -171,7 +168,6 @@ struct ThirdPartySipAccountLoginFragment: View { .default_text_style(styleSize: 15) .disableAutocorrection(true) .autocapitalization(.none) - .disabled(coreContext.loggedIn) .frame(height: 25) .padding(.horizontal, 20) .padding(.vertical, 15) @@ -221,7 +217,7 @@ struct ThirdPartySipAccountLoginFragment: View { Button(action: { self.accountLoginViewModel.login() }, label: { - Text(coreContext.loggedIn ? "manage_account_delete" : "assistant_account_login") + Text("assistant_account_login") .default_text_style_white_600(styleSize: 20) .frame(height: 35) .frame(maxWidth: .infinity) diff --git a/Linphone/UI/Main/ContentView.swift b/Linphone/UI/Main/ContentView.swift index 14898305d..327015892 100644 --- a/Linphone/UI/Main/ContentView.swift +++ b/Linphone/UI/Main/ContentView.swift @@ -355,8 +355,8 @@ struct ContentView: View { VStack(spacing: 0) { if searchIsActive == false { HStack { - if let accountModelIndex = accountProfileViewModel.accountModelIndex, - accountModelIndex < coreContext.accounts.count { + if let defaultAccountModelIndex = accountProfileViewModel.defaultAccountModelIndex, + defaultAccountModelIndex < coreContext.accounts.count { AsyncImage(url: imagePath) { image in switch image { case .empty: @@ -372,9 +372,9 @@ struct ContentView: View { imageTmp = image } case .failure: - if coreContext.accounts[accountModelIndex].avatarModel != nil { + if coreContext.accounts[defaultAccountModelIndex].avatarModel != nil { let tmpImage = contactsManager.textToImage( - firstName: coreContext.accounts[accountModelIndex].avatarModel!.name, + firstName: coreContext.accounts[defaultAccountModelIndex].avatarModel!.name, lastName: "") Image(uiImage: tmpImage) .resizable() @@ -398,14 +398,14 @@ struct ContentView: View { openMenu() } .onAppear { - let imagePathTmp = coreContext.accounts[accountModelIndex].getImagePath() + let imagePathTmp = coreContext.accounts[defaultAccountModelIndex].getImagePath() if !(imagePathTmp.lastPathComponent.isEmpty || imagePathTmp.lastPathComponent == "Error" || imagePathTmp.lastPathComponent == "ImageError.png") { imagePath = imagePathTmp } } - .onChange(of: coreContext.accounts[accountModelIndex].usernaneAvatar) { username in + .onChange(of: coreContext.accounts[defaultAccountModelIndex].usernaneAvatar) { username in if !username.isEmpty { - let imagePathTmp = coreContext.accounts[accountModelIndex].getImagePath() + let imagePathTmp = coreContext.accounts[defaultAccountModelIndex].getImagePath() if !(imagePathTmp.lastPathComponent.isEmpty || imagePathTmp.lastPathComponent == "Error" || imagePathTmp.lastPathComponent == "ImageError.png") { sharedMainViewModel.changeDefaultAvatar(defaultAvatarURL: imagePathTmp) imagePath = imagePathTmp @@ -413,8 +413,8 @@ struct ContentView: View { } } .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("ImageChanged"))) { _ in - if !coreContext.accounts[accountModelIndex].usernaneAvatar.isEmpty { - let imagePathTmp = coreContext.accounts[accountModelIndex].getImagePath() + if !coreContext.accounts[defaultAccountModelIndex].usernaneAvatar.isEmpty { + let imagePathTmp = coreContext.accounts[defaultAccountModelIndex].getImagePath() sharedMainViewModel.changeDefaultAvatar(defaultAvatarURL: imagePathTmp) imagePath = imagePathTmp } @@ -1368,6 +1368,31 @@ struct ContentView: View { .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("CoreStarted"))) { _ in accountProfileViewModel.setAvatarModel() } + .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("DefaultAccountChanged"))) { _ in + accountProfileViewModel.defaultAccountModelIndex = CoreContext.shared.accounts.firstIndex(where: {$0.isDefaultAccount}) + + withAnimation { + if self.sideMenuIsOpen { + self.sideMenuIsOpen = false + } + } + + if self.isShowLoginFragment { + self.isShowLoginFragment = false + } + + if conversationsListViewModel != nil { + conversationsListViewModel = ConversationsListViewModel() + } + + if historyListViewModel != nil { + historyListViewModel = HistoryListViewModel() + } + + if meetingsListViewModel != nil { + meetingsListViewModel = MeetingsListViewModel() + } + } .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name("PasswordUpdate")).compactMap { $0.userInfo?["address"] as? String }) { address in passwordUpdateAddress = address isShowUpdatePasswordPopup = true diff --git a/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift b/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift index 342925be1..7c268dd35 100644 --- a/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift +++ b/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift @@ -202,56 +202,72 @@ class ConversationsListViewModel: ObservableObject { func addConversationDelegate() { coreContext.doOnCoreQueue { core in - self.coreConversationDelegate = CoreDelegateStub(onMessagesReceived: { (_: Core, chatRoom: ChatRoom, _: [ChatMessage]) in - let model = ConversationModel(chatRoom: chatRoom) - let idTmp = LinphoneUtils.getChatRoomId(room: chatRoom) - let index = self.conversationsList.firstIndex(where: { $0.id == idTmp }) - DispatchQueue.main.async { - if index != nil { - self.conversationsList.remove(at: index!) - } - self.conversationsList.insert(model, at: 0) - } - self.updateUnreadMessagesCount() + self.coreConversationDelegate = CoreDelegateStub(onMessagesReceived: { (core: Core, chatRoom: ChatRoom, _: [ChatMessage]) in + if let defaultAddress = core.defaultAccount?.contactAddress, + let localAddress = chatRoom.localAddress, + defaultAddress.weakEqual(address2: localAddress) { + let model = ConversationModel(chatRoom: chatRoom) + let idTmp = LinphoneUtils.getChatRoomId(room: chatRoom) + let index = self.conversationsList.firstIndex(where: { $0.id == idTmp }) + DispatchQueue.main.async { + 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 idTmp = LinphoneUtils.getChatRoomId(room: chatRoom) - let index = self.conversationsList.firstIndex(where: { $0.id == idTmp }) - if index != nil { - self.conversationsList[index!].chatMessageRemoveDelegate() - } - DispatchQueue.main.async { - if index != nil { - self.conversationsList.remove(at: index!) - } - self.conversationsList.insert(model, at: 0) - } - self.updateUnreadMessagesCount() + if let defaultAddress = core.defaultAccount?.contactAddress, + let localAddress = chatRoom.localAddress, + defaultAddress.weakEqual(address2: localAddress) { + let model = ConversationModel(chatRoom: chatRoom) + let idTmp = LinphoneUtils.getChatRoomId(room: chatRoom) + let index = self.conversationsList.firstIndex(where: { $0.id == idTmp }) + if index != nil { + self.conversationsList[index!].chatMessageRemoveDelegate() + } + DispatchQueue.main.async { + 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 idTmp = LinphoneUtils.getChatRoomId(room: chatRoom) - let index = self.conversationsList.firstIndex(where: { $0.id == idTmp }) - DispatchQueue.main.async { - if index != nil { - self.conversationsList.remove(at: index!) - self.conversationsList.insert(model, at: index!) - } else { - self.conversationsList.insert(model, at: 0) - } - } - self.updateUnreadMessagesCount() + if let defaultAddress = core.defaultAccount?.contactAddress, + let localAddress = chatRoom.localAddress, + defaultAddress.weakEqual(address2: localAddress) { + let model = ConversationModel(chatRoom: chatRoom) + let idTmp = LinphoneUtils.getChatRoomId(room: chatRoom) + let index = self.conversationsList.firstIndex(where: { $0.id == idTmp }) + DispatchQueue.main.async { + if index != nil { + self.conversationsList.remove(at: index!) + self.conversationsList.insert(model, at: index!) + } else { + self.conversationsList.insert(model, at: 0) + } + } + self.updateUnreadMessagesCount() + } }, onChatRoomStateChanged: { (core: Core, chatroom: ChatRoom, state: ChatRoom.State) in // Log.info("[ConversationsListViewModel] Conversation [${LinphoneUtils.getChatRoomId(chatRoom)}] state changed [$state]") - if core.globalState == .On { - switch state { - case .Created: - self.addChatRoom(chatRoom: chatroom) - case .Deleted: - self.removeChatRoom(chatRoom: chatroom) - default: - break - } - } + if let defaultAddress = core.defaultAccount?.contactAddress, + let localAddress = chatroom.localAddress, + defaultAddress.weakEqual(address2: localAddress) { + if core.globalState == .On { + switch state { + case .Created: + self.addChatRoom(chatRoom: chatroom) + case .Deleted: + self.removeChatRoom(chatRoom: chatroom) + default: + break + } + } + } }) core.addDelegate(delegate: self.coreConversationDelegate!) } diff --git a/Linphone/UI/Main/Fragments/SideMenu.swift b/Linphone/UI/Main/Fragments/SideMenu.swift index 2e0e1941a..f984aa5b1 100644 --- a/Linphone/UI/Main/Fragments/SideMenu.swift +++ b/Linphone/UI/Main/Fragments/SideMenu.swift @@ -73,7 +73,7 @@ struct SideMenu: View { ForEach(0..