fix(multi-account): clear subscriptions and cache on account switch

When switching accounts, the feed showed stale data from the previous
account because subscriptions and LocalCache weren't cleared.

Now tracks previousAccountPubKey and when it changes:
1. Clears all relay subscriptions (subscriptionsCoordinator.clear())
2. Clears local event cache (localCache.clear())
3. Restarts subscriptions coordinator

Feed composables then resubscribe with the new account's pubkey
via their existing rememberSubscription(account, ...) keys.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
nrobi144
2026-04-28 14:23:15 +03:00
parent 0fb7c659f2
commit c6b2618fd6
@@ -648,11 +648,28 @@ fun App(
).also { it.startCleanupLoop() }
}
// Clear cache and subscriptions on logout
// Clear cache and subscriptions on logout or account switch
var previousAccountPubKey by remember { mutableStateOf<String?>(null) }
LaunchedEffect(accountState) {
if (accountState is AccountState.LoggedOut) {
subscriptionsCoordinator.clear()
localCache.clear()
when (val state = accountState) {
is AccountState.LoggedOut -> {
subscriptionsCoordinator.clear()
localCache.clear()
previousAccountPubKey = null
}
is AccountState.LoggedIn -> {
val currentPubKey = state.pubKeyHex
if (previousAccountPubKey != null && previousAccountPubKey != currentPubKey) {
// Account switched — clear old data so new feed loads fresh
subscriptionsCoordinator.clear()
localCache.clear()
subscriptionsCoordinator.start()
}
previousAccountPubKey = currentPubKey
}
is AccountState.ConnectingRelays -> {}
}
}