@@ -2,7 +2,8 @@ package com.threegap.bitnagil.presentation.splash
22
33import androidx.lifecycle.SavedStateHandle
44import androidx.lifecycle.viewModelScope
5- import com.threegap.bitnagil.domain.auth.usecase.HasTokenUseCase
5+ import com.threegap.bitnagil.domain.auth.model.UserRole
6+ import com.threegap.bitnagil.domain.auth.usecase.AutoLoginUseCase
67import com.threegap.bitnagil.presentation.common.mviviewmodel.MviViewModel
78import com.threegap.bitnagil.presentation.splash.model.SplashIntent
89import com.threegap.bitnagil.presentation.splash.model.SplashSideEffect
@@ -15,26 +16,27 @@ import javax.inject.Inject
1516
1617@HiltViewModel
1718class SplashViewModel @Inject constructor(
18- private val savedStateHandle : SavedStateHandle ,
19- private val hasTokenUseCase : HasTokenUseCase ,
19+ savedStateHandle : SavedStateHandle ,
20+ private val autoLoginUseCase : AutoLoginUseCase ,
2021) : MviViewModel<SplashState, SplashSideEffect, SplashIntent>(
2122 initState = SplashState (),
2223 savedStateHandle = savedStateHandle,
2324) {
2425
25- private var hasToken: Boolean? = null
26-
2726 init {
28- checkTokenStatus ()
27+ performAutoLogin ()
2928 }
3029
3130 override suspend fun SimpleSyntax <SplashState , SplashSideEffect >.reduceState (
3231 intent : SplashIntent ,
3332 state : SplashState ,
3433 ): SplashState ? =
3534 when (intent) {
36- is SplashIntent .SetTokenChecked -> {
37- state.copy(isTokenChecked = intent.hasToken != null )
35+ is SplashIntent .SetUserRole -> {
36+ state.copy(
37+ userRole = intent.userRole,
38+ isAutoLoginCompleted = true ,
39+ )
3840 }
3941
4042 is SplashIntent .NavigateToIntro -> {
@@ -46,34 +48,40 @@ class SplashViewModel @Inject constructor(
4648 sendSideEffect(SplashSideEffect .NavigateToHome )
4749 null
4850 }
51+
52+ is SplashIntent .NavigateToTermsAgreement -> {
53+ sendSideEffect(SplashSideEffect .NavigateToTermsAgreement )
54+ null
55+ }
56+
57+ is SplashIntent .NavigateToOnboarding -> {
58+ sendSideEffect(SplashSideEffect .NavigateToOnboarding )
59+ null
60+ }
4961 }
5062
51- private fun checkTokenStatus () {
63+ private fun performAutoLogin () {
5264 viewModelScope.launch {
53- try {
54- hasToken = hasTokenUseCase()
55- sendIntent(SplashIntent .SetTokenChecked (hasToken))
56- } catch (e: Exception ) {
57- hasToken = false
58- sendIntent(SplashIntent .SetTokenChecked (false ))
59- }
65+ val userRole = autoLoginUseCase()
66+ sendIntent(SplashIntent .SetUserRole (userRole))
6067 }
6168 }
6269
6370 fun onAnimationCompleted () {
64- val tokenResult = hasToken
65- if (tokenResult == null ) {
71+ val splashState = container.stateFlow.value
72+ if (! splashState.isAutoLoginCompleted ) {
6673 viewModelScope.launch {
6774 delay(100 )
6875 onAnimationCompleted()
6976 }
7077 return
7178 }
7279
73- if (tokenResult) {
74- sendIntent(SplashIntent .NavigateToHome )
75- } else {
76- sendIntent(SplashIntent .NavigateToIntro )
80+ when (splashState.userRole) {
81+ UserRole .GUEST -> sendIntent(SplashIntent .NavigateToTermsAgreement )
82+ UserRole .USER -> sendIntent(SplashIntent .NavigateToHome )
83+ UserRole .ONBOARDING -> sendIntent(SplashIntent .NavigateToOnboarding )
84+ else -> sendIntent(SplashIntent .NavigateToIntro )
7785 }
7886 }
7987}
0 commit comments