@@ -2,6 +2,8 @@ package com.threegap.bitnagil.presentation.login
22
33import android.util.Log
44import androidx.lifecycle.SavedStateHandle
5+ import androidx.lifecycle.viewModelScope
6+ import com.kakao.sdk.auth.model.OAuthToken
57import com.kakao.sdk.common.model.ClientError
68import com.kakao.sdk.common.model.ClientErrorCause
79import com.threegap.bitnagil.domain.auth.usecase.LoginUseCase
@@ -11,9 +13,8 @@ import com.threegap.bitnagil.presentation.login.model.LoginIntent
1113import com.threegap.bitnagil.presentation.login.model.LoginSideEffect
1214import com.threegap.bitnagil.presentation.login.model.LoginState
1315import dagger.hilt.android.lifecycle.HiltViewModel
16+ import kotlinx.coroutines.launch
1417import org.orbitmvi.orbit.syntax.simple.SimpleSyntax
15- import org.orbitmvi.orbit.syntax.simple.intent
16- import org.orbitmvi.orbit.syntax.simple.reduce
1718import javax.inject.Inject
1819
1920@HiltViewModel
@@ -38,66 +39,66 @@ class LoginViewModel @Inject constructor(
3839 null
3940 }
4041
41- is LoginIntent .OnKakaoLoginResult -> {
42- intent { handleKakaoLoginResult(intent) }
43- null
42+ is LoginIntent .SetLoading -> {
43+ state.copy(isLoading = intent.isLoading)
4444 }
45- }
4645
47- private suspend fun SimpleSyntax <LoginState , LoginSideEffect >.handleKakaoLoginResult (
48- intent : LoginIntent .OnKakaoLoginResult ,
49- ) {
50- reduce { state.copy(isLoading = true ) }
51-
52- when {
53- intent.token != null -> {
54- processBitnagilLogin(
55- socialAccessToken = intent.token.accessToken,
56- socialType = " KAKAO" ,
46+ is LoginIntent .LoginSuccess -> {
47+ sendSideEffect(
48+ if (intent.isGuest) {
49+ LoginSideEffect .NavigateToTermsOfService
50+ } else {
51+ LoginSideEffect .NavigateToHome
52+ },
53+ )
54+ state.copy(
55+ isGuest = intent.isGuest,
56+ isLoading = false ,
5757 )
5858 }
5959
60- intent.error is ClientError && intent.error.reason == ClientErrorCause . Cancelled -> {
61- Log .e( " KakaoLogin " , " 로그인 취소 " , intent.error )
62- reduce { state.copy(isLoading = false ) }
60+ is LoginIntent . KakaoTalkLoginCancel -> {
61+ sendSideEffect( LoginSideEffect . RequestKakaoAccountLogin )
62+ state.copy(isLoading = false )
6363 }
6464
65- intent.error != null -> {
66- Log .e(" KakaoLogin" , " 로그인 실패" , intent.error)
67- reduce { state.copy(isLoading = false ) }
65+ is LoginIntent .LoginFailure -> {
66+ state.copy(isLoading = false )
67+ }
68+ }
69+
70+ fun kakaoLogin (token : OAuthToken ? , error : Throwable ? ) {
71+ viewModelScope.launch {
72+ sendIntent(LoginIntent .SetLoading (true ))
73+ when {
74+ token != null -> {
75+ processKakaoLoginSuccess(token)
76+ }
77+
78+ error is ClientError && error.reason == ClientErrorCause .Cancelled -> {
79+ Log .e(" KakaoLogin" , " 카카오 로그인 취소" , error)
80+ sendIntent(LoginIntent .KakaoTalkLoginCancel )
81+ }
82+
83+ error != null -> {
84+ Log .e(" KakaoLogin" , " 카카오 로그인 실패" , error)
85+ sendIntent(LoginIntent .LoginFailure )
86+ }
6887 }
6988 }
7089 }
7190
72- private suspend fun SimpleSyntax <LoginState , LoginSideEffect >.processBitnagilLogin (
73- socialAccessToken : String ,
74- socialType : String ,
75- ) {
91+ private suspend fun processKakaoLoginSuccess (token : OAuthToken ) {
7692 loginUseCase(
77- socialAccessToken = socialAccessToken ,
78- socialType = socialType ,
93+ socialAccessToken = token.accessToken ,
94+ socialType = " KAKAO " ,
7995 ).fold(
8096 onSuccess = {
8197 val isGuest = it.role.isGuest()
82-
83- reduce {
84- state.copy(
85- isGuest = isGuest,
86- isLoading = false ,
87- )
88- }
89-
90- sendSideEffect(
91- if (isGuest) {
92- LoginSideEffect .NavigateToTermsOfService
93- } else {
94- LoginSideEffect .NavigateToHome
95- },
96- )
98+ sendIntent(LoginIntent .LoginSuccess (isGuest = isGuest))
9799 },
98100 onFailure = { e ->
99- reduce { state.copy(isLoading = false ) }
100-
101+ sendIntent(LoginIntent .LoginFailure )
101102 if (e is BitnagilError ) {
102103 Log .e(" Login" , " ${e.code} ${e.message} " )
103104 }
0 commit comments