Fix removing account when registration fails

This commit is contained in:
Benoit Martins 2025-06-24 11:32:09 +02:00
parent decb2ef753
commit 8db22b6b52
3 changed files with 63 additions and 39 deletions

View file

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

View file

@ -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)
}
}
}
}
}

View file

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