Skip to content

Commit 6911cc9

Browse files
committed
Refactor: 스플래시 화면 자동 로그인 분기 처리
- UserRole에 따라 약관 동의, 온보딩, 홈 화면으로 이동
1 parent aabecdf commit 6911cc9

3 files changed

Lines changed: 44 additions & 22 deletions

File tree

app/src/main/java/com/threegap/bitnagil/MainNavHost.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ fun MainNavHost(
3838
popUpTo<Route.Splash> { inclusive = true }
3939
}
4040
},
41+
navigateToTermsAgreement = {
42+
navigator.navController.navigate(Route.TermsAgreement) {
43+
popUpTo<Route.Splash> { inclusive = true }
44+
}
45+
},
46+
navigateToOnboarding = {
47+
navigator.navController.navigate(Route.OnBoarding()) {
48+
popUpTo<Route.Splash> { inclusive = true }
49+
}
50+
},
4151
navigateToHome = navigator::navigateToHomeAndClearStack,
4252
)
4353
}

presentation/src/main/java/com/threegap/bitnagil/presentation/splash/SplashScreen.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ import org.orbitmvi.orbit.compose.collectSideEffect
2929
@Composable
3030
fun SplashScreenContainer(
3131
navigateToIntro: () -> Unit,
32+
navigateToTermsAgreement: () -> Unit,
33+
navigateToOnboarding: () -> Unit,
3234
navigateToHome: () -> Unit,
3335
viewModel: SplashViewModel = hiltViewModel(),
3436
) {
3537
viewModel.collectSideEffect { sideEffect ->
3638
when (sideEffect) {
3739
is SplashSideEffect.NavigateToIntro -> navigateToIntro()
40+
is SplashSideEffect.NavigateToTermsAgreement -> navigateToTermsAgreement()
41+
is SplashSideEffect.NavigateToOnboarding -> navigateToOnboarding()
3842
is SplashSideEffect.NavigateToHome -> navigateToHome()
3943
}
4044
}

presentation/src/main/java/com/threegap/bitnagil/presentation/splash/SplashViewModel.kt

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package com.threegap.bitnagil.presentation.splash
22

33
import androidx.lifecycle.SavedStateHandle
44
import 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
67
import com.threegap.bitnagil.presentation.common.mviviewmodel.MviViewModel
78
import com.threegap.bitnagil.presentation.splash.model.SplashIntent
89
import com.threegap.bitnagil.presentation.splash.model.SplashSideEffect
@@ -15,26 +16,27 @@ import javax.inject.Inject
1516

1617
@HiltViewModel
1718
class 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

Comments
 (0)