diff --git a/Linphone/UI/Main/ContentView.swift b/Linphone/UI/Main/ContentView.swift index e08eaf48c..5b49be882 100644 --- a/Linphone/UI/Main/ContentView.swift +++ b/Linphone/UI/Main/ContentView.swift @@ -250,12 +250,12 @@ struct ContentView: View { .frame(height: geometry.size.height/4) ZStack { - if let historyListVM = historyListViewModel, historyListVM.missedCallsCount > 0 { + if SharedMainViewModel.shared.missedCallsCount > 0 { VStack { HStack { Text( - historyListVM.missedCallsCount < 99 - ? String(historyListVM.missedCallsCount) + SharedMainViewModel.shared.missedCallsCount < 99 + ? String(SharedMainViewModel.shared.missedCallsCount) : "99+" ) .foregroundStyle(.white) @@ -275,8 +275,8 @@ struct ContentView: View { sharedMainViewModel.displayedFriend = nil sharedMainViewModel.displayedConversation = nil sharedMainViewModel.displayedMeeting = nil - if let historyListVM = historyListViewModel, historyListVM.missedCallsCount > 0 { - historyListVM.resetMissedCallsCount() + if SharedMainViewModel.shared.missedCallsCount > 0 { + SharedMainViewModel.shared.resetMissedCallsCount() } }, label: { VStack { @@ -300,12 +300,12 @@ struct ContentView: View { if !CorePreferences.disableChatFeature { ZStack { - if let contactsListVM = conversationsListViewModel, contactsListVM.unreadMessages > 0 { + if SharedMainViewModel.shared.unreadMessages > 0 { VStack { HStack { Text( - contactsListVM.unreadMessages < 99 - ? String(contactsListVM.unreadMessages) + SharedMainViewModel.shared.unreadMessages < 99 + ? String(SharedMainViewModel.shared.unreadMessages) : "99+" ) .foregroundStyle(.white) @@ -797,12 +797,12 @@ struct ContentView: View { Spacer() ZStack { - if let historyListVM = historyListViewModel, historyListVM.missedCallsCount > 0 { + if SharedMainViewModel.shared.missedCallsCount > 0 { VStack { HStack { Text( - historyListVM.missedCallsCount < 99 - ? String(historyListVM.missedCallsCount) + SharedMainViewModel.shared.missedCallsCount < 99 + ? String(SharedMainViewModel.shared.missedCallsCount) : "99+" ) .foregroundStyle(.white) @@ -822,8 +822,8 @@ struct ContentView: View { sharedMainViewModel.displayedFriend = nil sharedMainViewModel.displayedConversation = nil sharedMainViewModel.displayedMeeting = nil - if let historyListVM = historyListViewModel, historyListVM.missedCallsCount > 0 { - historyListVM.resetMissedCallsCount() + if SharedMainViewModel.shared.missedCallsCount > 0 { + SharedMainViewModel.shared.resetMissedCallsCount() } }, label: { VStack { @@ -849,12 +849,12 @@ struct ContentView: View { Spacer() ZStack { - if let conversationsListVM = conversationsListViewModel, conversationsListVM.unreadMessages > 0 { + if SharedMainViewModel.shared.unreadMessages > 0 { VStack { HStack { Text( - conversationsListVM.unreadMessages < 99 - ? String(conversationsListVM.unreadMessages) + SharedMainViewModel.shared.unreadMessages < 99 + ? String(SharedMainViewModel.shared.unreadMessages) : "99+" ) .foregroundStyle(.white) diff --git a/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift b/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift index e8b5cc040..88bea7896 100644 --- a/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift +++ b/Linphone/UI/Main/Conversations/Fragments/ConversationsListBottomSheet.swift @@ -58,7 +58,7 @@ struct ConversationsListBottomSheet: View { if selectedConversation.unreadMessagesCount > 0 { Button { conversationsListViewModel.markAsReadSelectedConversation() - conversationsListViewModel.updateUnreadMessagesCount() + SharedMainViewModel.shared.updateUnreadMessagesCount() 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 476197e42..2d1771a39 100644 --- a/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift +++ b/Linphone/UI/Main/Conversations/ViewModel/ConversationsListViewModel.swift @@ -35,8 +35,6 @@ class ConversationsListViewModel: ObservableObject { @Published var conversationsList: [ConversationModel] = [] - @Published var unreadMessages: Int = 0 - var selectedConversation: ConversationModel? var currentFilter: String = "" @@ -60,7 +58,7 @@ class ConversationsListViewModel: ObservableObject { self.conversationsList = conversationsTmp } - self.updateUnreadMessagesCount() + SharedMainViewModel.shared.updateUnreadMessagesCount() } } } @@ -199,7 +197,7 @@ class ConversationsListViewModel: ObservableObject { } self.conversationsList.insert(model, at: 0) } - self.updateUnreadMessagesCount() + SharedMainViewModel.shared.updateUnreadMessagesCount() } }, onMessageSent: { (_: Core, chatRoom: ChatRoom, _: ChatMessage) in if let defaultAddress = core.defaultAccount?.contactAddress, @@ -218,7 +216,7 @@ class ConversationsListViewModel: ObservableObject { } self.conversationsList.insert(model, at: 0) } - self.updateUnreadMessagesCount() + SharedMainViewModel.shared.updateUnreadMessagesCount() } }, onChatRoomRead: { (_: Core, chatRoom: ChatRoom) in if let defaultAddress = core.defaultAccount?.contactAddress, @@ -236,7 +234,7 @@ class ConversationsListViewModel: ObservableObject { self.conversationsList.insert(model, at: 0) } } - self.updateUnreadMessagesCount() + SharedMainViewModel.shared.updateUnreadMessagesCount() } }, onChatRoomStateChanged: { (core: Core, chatroom: ChatRoom, state: ChatRoom.State) in // Log.info("[ConversationsListViewModel] Conversation [${LinphoneUtils.getChatRoomId(chatRoom)}] state changed [$state]") @@ -333,26 +331,7 @@ class ConversationsListViewModel: ObservableObject { self.conversationsList = sortedList.sorted { $0.lastUpdateTime > $1.lastUpdateTime } } - updateUnreadMessagesCount() - } - - func updateUnreadMessagesCount() { - coreContext.doOnCoreQueue { core in - let account = core.defaultAccount - if account != nil { - let count = account?.unreadChatMessageCount != nil ? account!.unreadChatMessageCount : core.unreadChatMessageCount - - DispatchQueue.main.async { - self.unreadMessages = count - UIApplication.shared.applicationIconBadgeNumber = count - } - } else { - DispatchQueue.main.async { - self.unreadMessages = 0 - UIApplication.shared.applicationIconBadgeNumber = 0 - } - } - } + SharedMainViewModel.shared.updateUnreadMessagesCount() } func getContentTextMessage(message: ChatMessage, completion: @escaping (String) -> Void) { diff --git a/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift b/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift index d0f483317..90aff417b 100644 --- a/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift +++ b/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift @@ -31,8 +31,6 @@ class HistoryListViewModel: ObservableObject { var callLogsAddressToDelete = "" var callLogCoreDelegate: CoreDelegate? - @Published var missedCallsCount: Int = 0 - @Published var selectedCall: HistoryModel? @Published var displayedConversation: ConversationModel? @@ -41,7 +39,7 @@ class HistoryListViewModel: ObservableObject { init() { computeCallLogsList() - updateMissedCallsCount() + SharedMainViewModel.shared.updateMissedCallsCount() } func computeCallLogsList() { @@ -87,45 +85,12 @@ class HistoryListViewModel: ObservableObject { self.callLogsTmp = callLogsTmpBis } - self.updateMissedCallsCount() + SharedMainViewModel.shared.updateMissedCallsCount() }) core.addDelegate(delegate: self.callLogCoreDelegate!) } } - func resetMissedCallsCount() { - coreContext.doOnCoreQueue { core in - let account = core.defaultAccount - if account != nil { - account?.resetMissedCallsCount() - DispatchQueue.main.async { - self.missedCallsCount = 0 - } - } else { - DispatchQueue.main.async { - self.missedCallsCount = 0 - } - } - } - } - - func updateMissedCallsCount() { - coreContext.doOnCoreQueue { core in - let account = core.defaultAccount - if account != nil { - let count = account?.missedCallsCount != nil ? account!.missedCallsCount : core.missedCallsCount - - DispatchQueue.main.async { - self.missedCallsCount = count - } - } else { - DispatchQueue.main.async { - self.missedCallsCount = 0 - } - } - } - } - func getCallIconResId(callStatus: Call.Status, isOutgoing: Bool) -> String { switch callStatus { case Call.Status.Missed: diff --git a/Linphone/UI/Main/Viewmodel/AccountModel.swift b/Linphone/UI/Main/Viewmodel/AccountModel.swift index 4667da3b9..e1b48c446 100644 --- a/Linphone/UI/Main/Viewmodel/AccountModel.swift +++ b/Linphone/UI/Main/Viewmodel/AccountModel.swift @@ -157,6 +157,8 @@ class AccountModel: ObservableObject { private func computeNotificationsCount() { let count = account.unreadChatMessageCount + account.missedCallsCount + SharedMainViewModel.shared.updateMissedCallsCount() + SharedMainViewModel.shared.updateUnreadMessagesCount() DispatchQueue.main.async { [self] in notificationsCount = count } diff --git a/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift b/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift index 4c2c41b80..9e28d345a 100644 --- a/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift +++ b/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift @@ -42,6 +42,9 @@ class SharedMainViewModel: ObservableObject { @Published var fileUrlsToShare: [String] = [] @Published var operationInProgress = false + + @Published var unreadMessages: Int = 0 + @Published var missedCallsCount: Int = 0 let welcomeViewKey = "welcome_view" let generalTermsKey = "general_terms" @@ -85,6 +88,9 @@ class SharedMainViewModel: ObservableObject { defaultAvatar = defaultAvatarTmp } } + + updateMissedCallsCount() + updateUnreadMessagesCount() } func changeWelcomeView() { @@ -153,4 +159,56 @@ class SharedMainViewModel: ObservableObject { } } } + + func resetMissedCallsCount() { + CoreContext.shared.doOnCoreQueue { core in + let account = core.defaultAccount + if account != nil { + account?.resetMissedCallsCount() + DispatchQueue.main.async { + self.missedCallsCount = 0 + } + } else { + DispatchQueue.main.async { + self.missedCallsCount = 0 + } + } + } + } + + func updateMissedCallsCount() { + CoreContext.shared.doOnCoreQueue { core in + let account = core.defaultAccount + if account != nil { + let count = account?.missedCallsCount != nil ? account!.missedCallsCount : core.missedCallsCount + + DispatchQueue.main.async { + self.missedCallsCount = count + } + } else { + DispatchQueue.main.async { + self.missedCallsCount = 0 + } + } + } + } + + func updateUnreadMessagesCount() { + CoreContext.shared.doOnCoreQueue { core in + let account = core.defaultAccount + if account != nil { + let count = account?.unreadChatMessageCount != nil ? account!.unreadChatMessageCount : core.unreadChatMessageCount + + DispatchQueue.main.async { + self.unreadMessages = count + UIApplication.shared.applicationIconBadgeNumber = count + } + } else { + DispatchQueue.main.async { + self.unreadMessages = 0 + UIApplication.shared.applicationIconBadgeNumber = 0 + } + } + } + } }