|
1 | 1 | package com.threegap.bitnagil.presentation.splash |
2 | 2 |
|
3 | | -import androidx.lifecycle.SavedStateHandle |
| 3 | +import androidx.lifecycle.ViewModel |
4 | 4 | import androidx.lifecycle.viewModelScope |
5 | 5 | import com.threegap.bitnagil.domain.auth.model.UserRole |
6 | 6 | import com.threegap.bitnagil.domain.auth.usecase.AutoLoginUseCase |
7 | 7 | import com.threegap.bitnagil.domain.version.usecase.CheckUpdateRequirementUseCase |
8 | | -import com.threegap.bitnagil.presentation.common.mviviewmodel.MviViewModel |
9 | | -import com.threegap.bitnagil.presentation.splash.model.SplashIntent |
10 | 8 | import com.threegap.bitnagil.presentation.splash.model.SplashSideEffect |
11 | 9 | import com.threegap.bitnagil.presentation.splash.model.SplashState |
12 | 10 | import dagger.hilt.android.lifecycle.HiltViewModel |
13 | 11 | import kotlinx.coroutines.delay |
14 | 12 | import kotlinx.coroutines.launch |
15 | 13 | import kotlinx.coroutines.withTimeoutOrNull |
16 | | -import org.orbitmvi.orbit.syntax.Syntax |
| 14 | +import org.orbitmvi.orbit.Container |
| 15 | +import org.orbitmvi.orbit.ContainerHost |
| 16 | +import org.orbitmvi.orbit.viewmodel.container |
17 | 17 | import javax.inject.Inject |
18 | 18 |
|
19 | 19 | @HiltViewModel |
20 | 20 | class SplashViewModel @Inject constructor( |
21 | | - savedStateHandle: SavedStateHandle, |
22 | 21 | private val checkUpdateRequirementUseCase: CheckUpdateRequirementUseCase, |
23 | 22 | private val autoLoginUseCase: AutoLoginUseCase, |
24 | | -) : MviViewModel<SplashState, SplashSideEffect, SplashIntent>( |
25 | | - initState = SplashState(), |
26 | | - savedStateHandle = savedStateHandle, |
27 | | -) { |
| 23 | +) : ContainerHost<SplashState, SplashSideEffect>, ViewModel() { |
| 24 | + |
| 25 | + override val container: Container<SplashState, SplashSideEffect> = container(initialState = SplashState.INIT) |
28 | 26 |
|
29 | 27 | init { |
30 | 28 | performForceUpdateCheck() |
31 | 29 | } |
32 | 30 |
|
33 | | - override suspend fun Syntax<SplashState, SplashSideEffect>.reduceState( |
34 | | - intent: SplashIntent, |
35 | | - state: SplashState, |
36 | | - ): SplashState? = |
37 | | - when (intent) { |
38 | | - is SplashIntent.SetUserRole -> { |
39 | | - state.copy( |
40 | | - userRole = intent.userRole, |
41 | | - isAutoLoginCompleted = true, |
42 | | - ) |
43 | | - } |
44 | | - |
45 | | - is SplashIntent.SetForceUpdateResult -> { |
46 | | - state.copy( |
47 | | - forceUpdateRequired = intent.isRequired, |
48 | | - isForceUpdateCheckCompleted = true, |
49 | | - ) |
50 | | - } |
51 | | - |
52 | | - is SplashIntent.NavigateToLogin -> { |
53 | | - sendSideEffect(SplashSideEffect.NavigateToLogin) |
54 | | - null |
55 | | - } |
56 | | - |
57 | | - is SplashIntent.NavigateToHome -> { |
58 | | - sendSideEffect(SplashSideEffect.NavigateToHome) |
59 | | - null |
60 | | - } |
61 | | - |
62 | | - is SplashIntent.NavigateToTermsAgreement -> { |
63 | | - sendSideEffect(SplashSideEffect.NavigateToTermsAgreement) |
64 | | - null |
65 | | - } |
66 | | - |
67 | | - is SplashIntent.NavigateToOnboarding -> { |
68 | | - sendSideEffect(SplashSideEffect.NavigateToOnboarding) |
69 | | - null |
70 | | - } |
71 | | - } |
72 | | - |
73 | 31 | private fun performForceUpdateCheck() { |
74 | | - viewModelScope.launch { |
| 32 | + intent { |
75 | 33 | val isUpdateRequired = withTimeoutOrNull(5000) { |
76 | 34 | checkUpdateRequirementUseCase().getOrElse { false } |
77 | 35 | } ?: false |
| 36 | + reduce { state.copy(forceUpdateRequired = isUpdateRequired, isForceUpdateCheckCompleted = true) } |
78 | 37 |
|
79 | | - sendIntent(SplashIntent.SetForceUpdateResult(isUpdateRequired)) |
80 | | - |
81 | | - if (!isUpdateRequired) { |
82 | | - performAutoLogin() |
83 | | - } |
| 38 | + if (!isUpdateRequired) { performAutoLogin() } |
84 | 39 | } |
85 | 40 | } |
86 | 41 |
|
87 | 42 | private fun performAutoLogin() { |
88 | | - viewModelScope.launch { |
| 43 | + intent { |
89 | 44 | try { |
90 | 45 | val userRole = withTimeoutOrNull(5000) { |
91 | 46 | autoLoginUseCase() |
92 | 47 | } |
93 | | - sendIntent(SplashIntent.SetUserRole(userRole)) |
| 48 | + reduce { state.copy(userRole = userRole, isAutoLoginCompleted = true) } |
94 | 49 | } catch (e: Exception) { |
95 | | - sendIntent(SplashIntent.SetUserRole(null)) |
| 50 | + reduce { state.copy(userRole = null, isAutoLoginCompleted = true) } |
96 | 51 | } |
97 | 52 | } |
98 | 53 | } |
99 | 54 |
|
100 | 55 | fun onAnimationCompleted() { |
101 | | - val splashState = container.stateFlow.value |
102 | | - |
103 | | - if (splashState.forceUpdateRequired) return |
104 | | - |
105 | | - if (!splashState.isAutoLoginCompleted) { |
106 | | - viewModelScope.launch { |
107 | | - delay(100) |
108 | | - onAnimationCompleted() |
| 56 | + intent { |
| 57 | + if (state.forceUpdateRequired) return@intent |
| 58 | + if (!state.isAutoLoginCompleted) { |
| 59 | + viewModelScope.launch { |
| 60 | + delay(100) |
| 61 | + onAnimationCompleted() |
| 62 | + } |
| 63 | + return@intent |
109 | 64 | } |
110 | | - return |
111 | | - } |
112 | 65 |
|
113 | | - when (splashState.userRole) { |
114 | | - UserRole.GUEST -> sendIntent(SplashIntent.NavigateToTermsAgreement) |
115 | | - UserRole.USER -> sendIntent(SplashIntent.NavigateToHome) |
116 | | - UserRole.ONBOARDING -> sendIntent(SplashIntent.NavigateToOnboarding) |
117 | | - else -> sendIntent(SplashIntent.NavigateToLogin) |
| 66 | + when (state.userRole) { |
| 67 | + UserRole.GUEST -> postSideEffect(SplashSideEffect.NavigateToTermsAgreement) |
| 68 | + UserRole.USER -> postSideEffect(SplashSideEffect.NavigateToHome) |
| 69 | + UserRole.ONBOARDING -> postSideEffect(SplashSideEffect.NavigateToOnboarding) |
| 70 | + else -> postSideEffect(SplashSideEffect.NavigateToLogin) |
| 71 | + } |
118 | 72 | } |
119 | 73 | } |
120 | 74 | } |
0 commit comments