fix(multi-account): preserve existing account when adding new one

- ensureCurrentAccountInStorage(): saves current account metadata to
  encrypted storage before switching to new account via AddAccountDialog
- AddAccountDialog calls ensureCurrentAccountInStorage() before each
  login method to prevent losing the previous account
- LoginScreen onLoginSuccess now calls refreshAccountList() so the
  switcher dropdown picks up the initial account immediately

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
nrobi144
2026-04-28 14:16:47 +03:00
parent b1098368a1
commit 573b478bbc
3 changed files with 35 additions and 1 deletions
@@ -708,6 +708,10 @@ fun App(
if (current?.signerType is com.vitorpamplona.amethyst.commons.model.account.SignerType.Remote) {
accountManager.startHeartbeat(scope)
}
// Refresh account list for switcher
scope.launch(Dispatchers.IO) {
accountManager.refreshAccountList()
}
},
)
}
@@ -637,6 +637,17 @@ class AccountManager internal constructor(
refreshAccountList()
}
/**
* Ensures the currently logged-in account is persisted in multi-account storage.
* Call before switching to a new account to avoid losing the current one.
*/
suspend fun ensureCurrentAccountInStorage() {
val current = currentAccount() ?: return
val info = AccountInfo(npub = current.npub, signerType = current.signerType)
accountStorage.saveAccount(info)
accountStorage.setCurrentAccount(current.npub)
}
suspend fun removeAccountFromStorage(npub: String) {
val current = currentAccount()
accountStorage.deleteAccount(npub)
@@ -88,6 +88,12 @@ fun AddAccountDialog(
) {
LoginCard(
onLogin = { keyInput ->
// Save current account before switching
scope.launch {
withContext(Dispatchers.IO) {
accountManager.ensureCurrentAccountInStorage()
}
}
accountManager.loginWithKey(keyInput).map {
scope.launch {
withContext(Dispatchers.IO) { accountManager.saveCurrentAccount() }
@@ -96,18 +102,31 @@ fun AddAccountDialog(
}
},
onGenerateNew = {
accountManager.generateNewAccount()
scope.launch {
withContext(Dispatchers.IO) {
accountManager.ensureCurrentAccountInStorage()
}
accountManager.generateNewAccount()
withContext(Dispatchers.IO) { accountManager.saveCurrentAccount() }
onAccountAdded()
}
},
onLoginBunker = { bunkerUri ->
scope.launch {
withContext(Dispatchers.IO) {
accountManager.ensureCurrentAccountInStorage()
}
}
accountManager.loginWithBunker(bunkerUri).map {
onAccountAdded()
}
},
onLoginNostrConnect = { onUriGenerated ->
scope.launch {
withContext(Dispatchers.IO) {
accountManager.ensureCurrentAccountInStorage()
}
}
accountManager.loginWithNostrConnect(onUriGenerated).map {
onAccountAdded()
}