From c6b2618fd648cb3b849cf1aa88b24639624fcac3 Mon Sep 17 00:00:00 2001 From: nrobi144 Date: Tue, 28 Apr 2026 14:23:15 +0300 Subject: [PATCH] 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) --- .../vitorpamplona/amethyst/desktop/Main.kt | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt index 1b665d88e..8e5fa1c2b 100644 --- a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt @@ -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(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 -> {} } }