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