Skip to content

Commit d20f6aa

Browse files
authored
Feature:auth fix auth module (#2393)
1 parent 43c5c6d commit d20f6aa

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

feature/auth/src/commonMain/kotlin/com/mifos/feature/auth/login/LoginViewModel.kt

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import com.mifos.core.network.model.PostAuthenticationResponse
2626
import kotlinx.coroutines.flow.MutableStateFlow
2727
import kotlinx.coroutines.flow.SharingStarted
2828
import kotlinx.coroutines.flow.StateFlow
29-
import kotlinx.coroutines.flow.asStateFlow
29+
import kotlinx.coroutines.flow.first
3030
import kotlinx.coroutines.flow.map
31+
import kotlinx.coroutines.flow.onStart
3132
import kotlinx.coroutines.flow.stateIn
3233
import kotlinx.coroutines.launch
3334

@@ -43,7 +44,23 @@ class LoginViewModel(
4344
) : ViewModel() {
4445

4546
private val _loginUiState = MutableStateFlow<LoginUiState>(LoginUiState.Empty)
46-
val loginUiState = _loginUiState.asStateFlow()
47+
val loginUiState = _loginUiState
48+
.onStart { checkLoginStatus() }
49+
.stateIn(
50+
viewModelScope,
51+
SharingStarted.WhileSubscribed(5000),
52+
LoginUiState.Empty,
53+
)
54+
private fun checkLoginStatus() {
55+
viewModelScope.launch {
56+
val user = prefManager.userData.first()
57+
if (user.isAuthenticated) {
58+
_loginUiState.value = LoginUiState.HomeActivityIntent
59+
} else {
60+
_loginUiState.value = LoginUiState.Empty
61+
}
62+
}
63+
}
4764

4865
private val passcode: StateFlow<String?> = prefManager.settingsInfo
4966
.map { it.passcode }
@@ -122,10 +139,12 @@ class LoginViewModel(
122139
)
123140
}
124141

125-
if (passcode.value != null) {
126-
_loginUiState.value = LoginUiState.HomeActivityIntent
127-
} else {
128-
_loginUiState.value = LoginUiState.HomeActivityIntent
129-
}
142+
_loginUiState.value = LoginUiState.HomeActivityIntent
143+
144+
// if (passcode.value != null) {
145+
// TODO() navigate to passcode screen
146+
// } else {
147+
// TODO() navigate to home screen
148+
// }
130149
}
131150
}

0 commit comments

Comments
 (0)