From 5bfbba193aa017c3e44f53e9ffea10ed5b4baa51 Mon Sep 17 00:00:00 2001 From: Damontecres Date: Sun, 11 Oct 2026 09:59:50 -0400 Subject: [PATCH] Fix issue with home reloading on user switch (#2064) ## Description When switching users, the current user's home page would start a reload during the switch. This PR fixes that by comparing the user switch. The underlying cause is that Room's flow is blunt and emits whenever the table updates. So [during the user switch](https://github.com/damontecres/Wholphin/blob/9b028040754acc58253b414c2c33a3ef4ca92d00/app/src/main/java/com/github/damontecres/wholphin/data/ServerRepository.kt#L117-L118) the table is updated causing [current user to emit](https://github.com/damontecres/Wholphin/blob/9b028040754acc58253b414c2c33a3ef4ca92d00/app/src/main/java/com/github/damontecres/wholphin/data/ServerRepository.kt#L71) and the current user flow is updated causing the new user to emit. ### Related issues N/A ### Testing Emulator, switching users and switching locales Can observe that `UserSwitchListener` only trigger once per user switch instead of twice ## Screenshots N/A ## AI or LLM usage None --- .../wholphin/data/ServerRepository.kt | 5 ++++- .../wholphin/services/UserSwitchListener.kt | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/damontecres/wholphin/data/ServerRepository.kt b/app/src/main/java/com/github/damontecres/wholphin/data/ServerRepository.kt index f3cd888e..5deba4c3 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/data/ServerRepository.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/data/ServerRepository.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map @@ -68,7 +69,9 @@ class ServerRepository _current .flatMapLatest { user -> if (user?.user != null) { - serverDao.getUserFlow(user.user.serverId, user.user.id) + serverDao + .getUserFlow(user.user.serverId, user.user.id) + .distinctUntilChanged() } else { flow { emit(null) } } diff --git a/app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt b/app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt index 536dfb40..06115982 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/services/UserSwitchListener.kt @@ -15,6 +15,8 @@ import com.github.damontecres.wholphin.ui.launchIO import com.github.damontecres.wholphin.util.WholphinDispatchers import dagger.hilt.android.qualifiers.ActivityContext import dagger.hilt.android.scopes.ActivityScoped +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.update import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.withContext @@ -38,14 +40,17 @@ class UserSwitchListener init { context as AppCompatActivity context.lifecycleScope.launchDefault { - serverRepository.currentUserFlow.collect { user -> - Timber.d("New user") - seerrServerRepository.clear() - homeSettingsService.currentSettings.update { HomePageResolvedSettings.EMPTY } - if (user != null) { - switchUser(user) + serverRepository.currentUserFlow + .distinctUntilChanged { old, new -> + old?.id == new?.id && old?.uiLanguage == new?.uiLanguage + }.collectLatest { user -> + Timber.d("UserSwitch: %s", user?.id) + seerrServerRepository.clear() + homeSettingsService.currentSettings.update { HomePageResolvedSettings.EMPTY } + if (user != null) { + switchUser(user) + } } - } } }