fix(profile): prevent seeing unlogged screen even if logged
This commit is contained in:
parent
ba6f573b20
commit
f8433211f5
@ -2,7 +2,11 @@ package com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository
|
|||||||
|
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.core.preferences.KeyStoreKeys
|
import com.github.diegoberaldin.raccoonforlemmy.core.preferences.KeyStoreKeys
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.core.preferences.TemporaryKeyStore
|
import com.github.diegoberaldin.raccoonforlemmy.core.preferences.TemporaryKeyStore
|
||||||
|
import kotlinx.coroutines.FlowPreview
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.debounce
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
|
||||||
internal class DefaultIdentityRepository(
|
internal class DefaultIdentityRepository(
|
||||||
private val keyStore: TemporaryKeyStore,
|
private val keyStore: TemporaryKeyStore,
|
||||||
@ -10,9 +14,13 @@ internal class DefaultIdentityRepository(
|
|||||||
|
|
||||||
override val authToken = MutableStateFlow<String?>(null)
|
override val authToken = MutableStateFlow<String?>(null)
|
||||||
|
|
||||||
|
@OptIn(FlowPreview::class)
|
||||||
|
override val isLogged: Flow<Boolean?> = authToken.debounce(100).map {
|
||||||
|
it?.isNotEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val previousToken = keyStore[KeyStoreKeys.AuthToken, ""]
|
val previousToken = keyStore[KeyStoreKeys.AuthToken, ""].takeIf { it.isNotEmpty() }
|
||||||
.takeIf { it.isNotEmpty() }
|
|
||||||
authToken.value = previousToken
|
authToken.value = previousToken
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository
|
package com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
|
||||||
interface IdentityRepository {
|
interface IdentityRepository {
|
||||||
|
|
||||||
val authToken: StateFlow<String?>
|
val authToken: StateFlow<String?>
|
||||||
|
val isLogged: Flow<Boolean?>
|
||||||
|
|
||||||
fun storeToken(value: String)
|
fun storeToken(value: String)
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepo
|
|||||||
import com.github.diegoberaldin.raccoonforlemmy.feature.inbox.InboxCoordinator
|
import com.github.diegoberaldin.raccoonforlemmy.feature.inbox.InboxCoordinator
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.IO
|
import kotlinx.coroutines.IO
|
||||||
import kotlinx.coroutines.flow.debounce
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -24,8 +23,8 @@ class InboxViewModel(
|
|||||||
override fun onStarted() {
|
override fun onStarted() {
|
||||||
mvi.onStarted()
|
mvi.onStarted()
|
||||||
mvi.scope?.launch {
|
mvi.scope?.launch {
|
||||||
identityRepository.authToken.debounce(250).onEach { auth ->
|
identityRepository.isLogged.onEach { logged ->
|
||||||
mvi.updateState { it.copy(isLogged = !auth.isNullOrEmpty()) }
|
mvi.updateState { it.copy(isLogged = logged) }
|
||||||
}.launchIn(this)
|
}.launchIn(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,13 +94,17 @@ internal object ProfileContentScreen : Tab {
|
|||||||
.padding(it),
|
.padding(it),
|
||||||
contentAlignment = Alignment.Center,
|
contentAlignment = Alignment.Center,
|
||||||
) {
|
) {
|
||||||
val screens = listOf(
|
|
||||||
ProfileNotLoggedScreen,
|
|
||||||
ProfileLoggedScreen,
|
|
||||||
)
|
|
||||||
// wait until logging status is determined
|
// wait until logging status is determined
|
||||||
if (uiState.logged != null) {
|
val logged = uiState.logged
|
||||||
TabNavigator(ProfileNotLoggedScreen) {
|
if (logged != null) {
|
||||||
|
val screens = remember {
|
||||||
|
listOf(
|
||||||
|
ProfileNotLoggedScreen,
|
||||||
|
ProfileLoggedScreen,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val root = if (logged) screens[1] else screens[0]
|
||||||
|
TabNavigator(root) {
|
||||||
CurrentScreen()
|
CurrentScreen()
|
||||||
val navigator = LocalTabNavigator.current
|
val navigator = LocalTabNavigator.current
|
||||||
LaunchedEffect(model) {
|
LaunchedEffect(model) {
|
||||||
|
@ -11,7 +11,6 @@ import com.github.diegoberaldin.raccoonforlemmy.core.preferences.KeyStoreKeys
|
|||||||
import com.github.diegoberaldin.raccoonforlemmy.core.preferences.TemporaryKeyStore
|
import com.github.diegoberaldin.raccoonforlemmy.core.preferences.TemporaryKeyStore
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.flow.debounce
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -30,8 +29,8 @@ class ProfileContentViewModel(
|
|||||||
mvi.onStarted()
|
mvi.onStarted()
|
||||||
|
|
||||||
mvi.scope?.launch {
|
mvi.scope?.launch {
|
||||||
identityRepository.authToken.debounce(250).onEach { token ->
|
identityRepository.isLogged.onEach { logged ->
|
||||||
mvi.updateState { it.copy(logged = !token.isNullOrEmpty()) }
|
mvi.updateState { it.copy(logged = logged) }
|
||||||
}.launchIn(this)
|
}.launchIn(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.UserRepo
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.IO
|
import kotlinx.coroutines.IO
|
||||||
import kotlinx.coroutines.flow.debounce
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -26,8 +25,9 @@ class MainViewModel(
|
|||||||
|
|
||||||
mvi.scope?.launch(Dispatchers.IO) {
|
mvi.scope?.launch(Dispatchers.IO) {
|
||||||
launch {
|
launch {
|
||||||
identityRepository.authToken.debounce(250).onEach { auth ->
|
identityRepository.isLogged.onEach { logged ->
|
||||||
val unreadCount = if (!auth.isNullOrEmpty()) {
|
val unreadCount = if (logged == true) {
|
||||||
|
val auth = identityRepository.authToken.value
|
||||||
val mentionCount =
|
val mentionCount =
|
||||||
userRepository.getMentions(auth, page = 1, limit = 50).count()
|
userRepository.getMentions(auth, page = 1, limit = 50).count()
|
||||||
val replyCount =
|
val replyCount =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user