diff --git a/Linphone/LinphoneApp.swift b/Linphone/LinphoneApp.swift index 6371e5bee..86403a04f 100644 --- a/Linphone/LinphoneApp.swift +++ b/Linphone/LinphoneApp.swift @@ -25,6 +25,11 @@ struct LinphoneApp: App { @ObservedObject private var coreContext = CoreContext.shared @ObservedObject private var sharedMainViewModel = SharedMainViewModel.shared + @State private var contactViewModel: ContactViewModel? + @State private var editContactViewModel: EditContactViewModel? + @State private var historyViewModel: HistoryViewModel? + @State private var historyListViewModel: HistoryListViewModel? + @State private var isActive = false var body: some Scene { @@ -34,16 +39,26 @@ struct LinphoneApp: App { WelcomeView() } else if coreContext.defaultAccount == nil || sharedMainViewModel.displayProfileMode { AssistantView() - } else if coreContext.defaultAccount != nil { + } else if coreContext.defaultAccount != nil + && contactViewModel != nil + && editContactViewModel != nil + && historyViewModel != nil + && historyListViewModel != nil { ContentView( - contactViewModel: ContactViewModel(), - editContactViewModel: EditContactViewModel(), - historyViewModel: HistoryViewModel(), - historyListViewModel: HistoryListViewModel() + contactViewModel: contactViewModel!, + editContactViewModel: editContactViewModel!, + historyViewModel: historyViewModel!, + historyListViewModel: historyListViewModel! ) } } else { SplashScreen(isActive: $isActive) + .onDisappear { + contactViewModel = ContactViewModel() + editContactViewModel = EditContactViewModel() + historyViewModel = HistoryViewModel() + historyListViewModel = HistoryListViewModel() + } } } } diff --git a/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift b/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift index 07cc6a2a5..139245706 100644 --- a/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift +++ b/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift @@ -73,8 +73,6 @@ struct HistoryContactFragment: View { Button { isMenuOpen = false - indexPage = 0 - if ContactsManager.shared.getFriendWithAddress( address: historyViewModel.displayedCall != nil && historyViewModel.displayedCall!.dir == .Outgoing ? historyViewModel.displayedCall!.toAddress! @@ -87,11 +85,13 @@ struct HistoryContactFragment: View { let friendIndex = MagicSearchSingleton.shared.lastSearch.firstIndex( where: {$0.friend!.addresses.contains(where: {$0.asStringUriOnly() == addressCall.asStringUriOnly()})}) if friendIndex != nil { - - withAnimation { + + withAnimation { historyViewModel.displayedCall = nil - contactViewModel.indexDisplayedFriend = friendIndex - } + indexPage = 0 + + contactViewModel.indexDisplayedFriend = friendIndex + } } } else { let addressCall = historyViewModel.displayedCall != nil && historyViewModel.displayedCall!.dir == .Outgoing @@ -100,10 +100,12 @@ struct HistoryContactFragment: View { withAnimation { historyViewModel.displayedCall = nil - isShowEditContactFragment.toggle() - editContactViewModel.sipAddresses.removeAll() - editContactViewModel.sipAddresses.append(String(addressCall.asStringUriOnly().dropFirst(4))) - editContactViewModel.sipAddresses.append("") + indexPage = 0 + + isShowEditContactFragment.toggle() + editContactViewModel.sipAddresses.removeAll() + editContactViewModel.sipAddresses.append(String(addressCall.asStringUriOnly().dropFirst(4))) + editContactViewModel.sipAddresses.append("") } } diff --git a/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift b/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift index 4758564a2..3251d2511 100644 --- a/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift +++ b/Linphone/UI/Main/History/ViewModel/HistoryListViewModel.swift @@ -26,9 +26,12 @@ class HistoryListViewModel: ObservableObject { @Published var callLogs: [CallLog] = [] var callLogsTmp: [CallLog] = [] + @Published private var coreDelegate: CoreDelegate? + var callLogsAddressToDelete = "" init() { + removeAllDelegate() computeCallLogsList() } @@ -46,6 +49,29 @@ class HistoryListViewModel: ObservableObject { self.callLogsTmp.append(log) } } + + DispatchQueue.main.async { + self.coreDelegate = CoreDelegateStub( + onCallLogUpdated: { (_: Core, _: CallLog) -> Void in + DispatchQueue.main.async { + let account = core.defaultAccount + let logs = account?.callLogs != nil ? account!.callLogs : core.callLogs + + self.callLogs.removeAll() + self.callLogsTmp.removeAll() + + logs.forEach { log in + self.callLogs.append(log) + self.callLogsTmp.append(log) + } + } + } + ) + if self.coreDelegate != nil { + core.addDelegate(delegate: self.coreDelegate!) + } + } + } } @@ -184,4 +210,14 @@ class HistoryListViewModel: ObservableObject { let indexTmp = self.callLogsTmp.firstIndex(where: {$0.callId == callLog.callId}) self.callLogsTmp.remove(at: indexTmp!) } + + func removeAllDelegate() { + coreContext.doOnCoreQueue { core in + if self.coreDelegate != nil { + core.removeDelegate(delegate: self.coreDelegate!) + self.coreDelegate = nil + } + } + } + }