|
1 | 1 | package com.threegap.bitnagil.presentation.login |
2 | 2 |
|
3 | 3 | import android.util.Log |
4 | | -import androidx.lifecycle.SavedStateHandle |
5 | | -import androidx.lifecycle.viewModelScope |
| 4 | +import androidx.lifecycle.ViewModel |
6 | 5 | import com.kakao.sdk.auth.model.OAuthToken |
7 | 6 | import com.threegap.bitnagil.domain.auth.usecase.LoginUseCase |
8 | | -import com.threegap.bitnagil.presentation.common.mviviewmodel.MviViewModel |
9 | | -import com.threegap.bitnagil.presentation.login.model.LoginIntent |
10 | 7 | import com.threegap.bitnagil.presentation.login.model.LoginSideEffect |
11 | 8 | import com.threegap.bitnagil.presentation.login.model.LoginState |
12 | 9 | import dagger.hilt.android.lifecycle.HiltViewModel |
13 | | -import kotlinx.coroutines.launch |
14 | | -import org.orbitmvi.orbit.syntax.Syntax |
| 10 | +import org.orbitmvi.orbit.Container |
| 11 | +import org.orbitmvi.orbit.ContainerHost |
| 12 | +import org.orbitmvi.orbit.viewmodel.container |
15 | 13 | import javax.inject.Inject |
16 | 14 |
|
17 | 15 | @HiltViewModel |
18 | 16 | class LoginViewModel @Inject constructor( |
19 | | - savedStateHandle: SavedStateHandle, |
20 | 17 | private val loginUseCase: LoginUseCase, |
21 | | -) : MviViewModel<LoginState, LoginSideEffect, LoginIntent>( |
22 | | - initState = LoginState(), |
23 | | - savedStateHandle = savedStateHandle, |
24 | | -) { |
25 | | - override suspend fun Syntax<LoginState, LoginSideEffect>.reduceState( |
26 | | - intent: LoginIntent, |
27 | | - state: LoginState, |
28 | | - ): LoginState? = |
29 | | - when (intent) { |
30 | | - is LoginIntent.SetLoading -> { |
31 | | - state.copy(isLoading = intent.isLoading) |
32 | | - } |
| 18 | +) : ContainerHost<LoginState, LoginSideEffect>, ViewModel() { |
33 | 19 |
|
34 | | - is LoginIntent.LoginSuccess -> { |
35 | | - sendSideEffect( |
36 | | - if (intent.isGuest) { |
37 | | - LoginSideEffect.NavigateToTermsAgreement |
38 | | - } else { |
39 | | - LoginSideEffect.NavigateToHome |
40 | | - }, |
41 | | - ) |
42 | | - state.copy( |
43 | | - isGuest = intent.isGuest, |
44 | | - isLoading = false, |
45 | | - ) |
46 | | - } |
47 | | - |
48 | | - is LoginIntent.KakaoTalkLoginCancel -> { |
49 | | - state.copy(isLoading = false) |
50 | | - } |
51 | | - |
52 | | - is LoginIntent.LoginFailure -> { |
53 | | - state.copy(isLoading = false) |
54 | | - } |
55 | | - } |
| 20 | + override val container: Container<LoginState, LoginSideEffect> = container(initialState = LoginState.INIT) |
56 | 21 |
|
57 | 22 | fun kakaoLogin(token: OAuthToken?, error: Throwable?) { |
58 | | - viewModelScope.launch { |
59 | | - sendIntent(LoginIntent.SetLoading(true)) |
| 23 | + intent { |
| 24 | + reduce { state.copy(isLoading = true) } |
60 | 25 | when { |
61 | | - token != null -> { |
62 | | - processKakaoLoginSuccess(token) |
63 | | - } |
| 26 | + token != null -> processKakaoLoginSuccess(token) |
64 | 27 |
|
65 | 28 | error != null -> { |
| 29 | + reduce { state.copy(isLoading = false) } |
66 | 30 | Log.e("KakaoLogin", "카카오 로그인 실패", error) |
67 | | - sendIntent(LoginIntent.LoginFailure) |
68 | 31 | } |
69 | 32 | } |
70 | 33 | } |
71 | 34 | } |
72 | 35 |
|
73 | 36 | private suspend fun processKakaoLoginSuccess(token: OAuthToken) { |
74 | | - loginUseCase( |
75 | | - socialAccessToken = token.accessToken, |
76 | | - socialType = "KAKAO", |
77 | | - ).fold( |
78 | | - onSuccess = { |
79 | | - val isGuest = it.role.isGuest() |
80 | | - sendIntent(LoginIntent.LoginSuccess(isGuest = isGuest)) |
81 | | - }, |
82 | | - onFailure = { e -> |
83 | | - sendIntent(LoginIntent.LoginFailure) |
84 | | - Log.e("Login", "${e.message}") |
85 | | - }, |
86 | | - ) |
| 37 | + subIntent { |
| 38 | + loginUseCase(socialAccessToken = token.accessToken, socialType = KAKAO).fold( |
| 39 | + onSuccess = { |
| 40 | + val isGuest = it.role.isGuest() |
| 41 | + if (isGuest) { |
| 42 | + postSideEffect(LoginSideEffect.NavigateToTermsAgreement) |
| 43 | + } else { |
| 44 | + postSideEffect(LoginSideEffect.NavigateToHome) |
| 45 | + } |
| 46 | + reduce { state.copy(isLoading = false) } |
| 47 | + }, |
| 48 | + onFailure = { e -> |
| 49 | + reduce { state.copy(isLoading = false) } |
| 50 | + Log.e("Login", "${e.message}") |
| 51 | + }, |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + companion object { |
| 57 | + private const val KAKAO = "KAKAO" |
87 | 58 | } |
88 | 59 | } |
0 commit comments