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](9b02804075/app/src/main/java/com/github/damontecres/wholphin/data/ServerRepository.kt (L117-L118)) the table is updated causing [current user to emit](9b02804075/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
This commit is contained in:
parent
9b02804075
commit
5bfbba193a
2 changed files with 16 additions and 8 deletions
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue