From 8db22b6b52ff62361fa45b381c81b39e923da19e Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Tue, 24 Jun 2025 11:32:09 +0200 Subject: [PATCH] Fix removing account when registration fails --- Linphone/Core/CoreContext.swift | 7 -- Linphone/LinphoneApp.swift | 73 +++++++++++-------- .../Viewmodel/AccountLoginViewModel.swift | 22 ++++++ 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/Linphone/Core/CoreContext.swift b/Linphone/Core/CoreContext.swift index 5ff3617d4..8e1232ad2 100644 --- a/Linphone/Core/CoreContext.swift +++ b/Linphone/Core/CoreContext.swift @@ -328,13 +328,6 @@ class CoreContext: ObservableObject { let clonedParams = params?.clone() clonedParams?.registerEnabled = false account.params = clonedParams - - if let authInfo = account.findAuthInfo() { - core.removeAuthInfo(info: authInfo) - } - - Log.warn("Registration failed for account \(account.displayName()), deleting it from core") - core.removeAccount(account: account) } default: break diff --git a/Linphone/LinphoneApp.swift b/Linphone/LinphoneApp.swift index 9d673bbdb..3b475c3cd 100644 --- a/Linphone/LinphoneApp.swift +++ b/Linphone/LinphoneApp.swift @@ -179,13 +179,25 @@ struct RootView: View { var body: some View { Group { if coreContext.coreHasStartedOnce { - MainViewSwitcher( - coreContext: coreContext, - navigationManager: navigationManager, - sharedMainViewModel: sharedMainViewModel, - pendingURL: $pendingURL, - appDelegate: appDelegate - ) + if showWelcome { + ZStack { + WelcomeView() + ToastView().zIndex(3) + } + } else if showAssistant { + ZStack { + AssistantView() + ToastView().zIndex(3) + } + } else { + MainViewSwitcher( + coreContext: coreContext, + navigationManager: navigationManager, + sharedMainViewModel: sharedMainViewModel, + pendingURL: $pendingURL, + appDelegate: appDelegate + ) + } } else { SplashScreen() } @@ -198,6 +210,16 @@ struct RootView: View { } } } + + + var showWelcome: Bool { + !sharedMainViewModel.welcomeViewDisplayed + } + + var showAssistant: Bool { + (coreContext.coreIsStarted && coreContext.accounts.isEmpty) + || sharedMainViewModel.displayProfileMode + } } struct MainViewSwitcher: View { @@ -209,9 +231,9 @@ struct MainViewSwitcher: View { var body: some View { ZStack { - selectedMainView() - if coreContext.coreIsStarted { + selectedMainView() + VStack {} // Force trigger .onAppear .onAppear { if let url = pendingURL { @@ -225,29 +247,16 @@ struct MainViewSwitcher: View { @ViewBuilder func selectedMainView() -> some View { - if !sharedMainViewModel.welcomeViewDisplayed { - ZStack { - WelcomeView() - ToastView().zIndex(3) - } - } else if (coreContext.coreIsStarted && coreContext.accounts.isEmpty) - || sharedMainViewModel.displayProfileMode { - ZStack { - AssistantView() - ToastView().zIndex(3) - } - } else { - ContentView() - .onAppear { - appDelegate.coreContext = coreContext - appDelegate.navigationManager = navigationManager - - if let callId = appDelegate.launchNotificationCallId, - let peerAddr = appDelegate.launchNotificationPeerAddr, - let localAddr = appDelegate.launchNotificationLocalAddr { - navigationManager.openChatRoom(callId: callId, peerAddr: peerAddr, localAddr: localAddr) - } + ContentView() + .onAppear { + appDelegate.coreContext = coreContext + appDelegate.navigationManager = navigationManager + + if let callId = appDelegate.launchNotificationCallId, + let peerAddr = appDelegate.launchNotificationPeerAddr, + let localAddr = appDelegate.launchNotificationLocalAddr { + navigationManager.openChatRoom(callId: callId, peerAddr: peerAddr, localAddr: localAddr) } - } + } } } diff --git a/Linphone/UI/Assistant/Viewmodel/AccountLoginViewModel.swift b/Linphone/UI/Assistant/Viewmodel/AccountLoginViewModel.swift index 988bd9dc7..630f19b4b 100644 --- a/Linphone/UI/Assistant/Viewmodel/AccountLoginViewModel.swift +++ b/Linphone/UI/Assistant/Viewmodel/AccountLoginViewModel.swift @@ -30,6 +30,8 @@ class AccountLoginViewModel: ObservableObject { @Published var displayName: String = "" @Published var transportType: String = "TLS" + private var mCoreDelegate: CoreDelegate! + init() {} func login() { @@ -118,6 +120,26 @@ class AccountLoginViewModel: ObservableObject { accountParams.internationalPrefixIsoCountryCode = "FRA" accountParams.useInternationalPrefixForCallsAndChats = true + self.mCoreDelegate = CoreDelegateStub(onAccountRegistrationStateChanged: { (core: Core, account: Account, state: RegistrationState, message: String) in + + Log.info("New registration state is \(state) for user id " + + "\( String(describing: account.params?.identityAddress?.asString())) = \(message)\n") + + switch state { + case .Failed: // If registration failed, remove account from core + if let authInfo = account.findAuthInfo() { + core.removeAuthInfo(info: authInfo) + } + + Log.warn("Registration failed for account \(account.displayName()), deleting it from core") + core.removeAccount(account: account) + default: + break + } + }) + + self.coreContext.mCore.addDelegate(delegate: self.mCoreDelegate) + // Now that our AccountParams is configured, we can create the Account object let account = try core.createAccount(params: accountParams)