Skip to content

Commit fe99bbb

Browse files
committed
Refactor: 로그인 Intent 분리
LoginIntent를 세분화하여 각 상황에 맞는 Intent를 사용하도록 수정
1 parent c1ad2d4 commit fe99bbb

3 files changed

Lines changed: 52 additions & 53 deletions

File tree

presentation/src/main/java/com/threegap/bitnagil/presentation/login/LoginScreen.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ fun LoginScreenContainer(
3131
when (sideEffect) {
3232
is LoginSideEffect.RequestKakaoTalkLogin -> {
3333
client.loginWithKakaoTalk(context) { token, error ->
34-
viewModel.sendIntent(LoginIntent.OnKakaoLoginResult(token, error))
34+
viewModel.kakaoLogin(token, error)
3535
}
3636
}
3737

3838
is LoginSideEffect.RequestKakaoAccountLogin -> {
3939
client.loginWithKakaoAccount(context) { token, error ->
40-
viewModel.sendIntent(LoginIntent.OnKakaoLoginResult(token, error))
40+
viewModel.kakaoLogin(token, error)
4141
}
4242
}
4343

presentation/src/main/java/com/threegap/bitnagil/presentation/login/LoginViewModel.kt

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.threegap.bitnagil.presentation.login
22

33
import android.util.Log
44
import androidx.lifecycle.SavedStateHandle
5+
import androidx.lifecycle.viewModelScope
6+
import com.kakao.sdk.auth.model.OAuthToken
57
import com.kakao.sdk.common.model.ClientError
68
import com.kakao.sdk.common.model.ClientErrorCause
79
import com.threegap.bitnagil.domain.auth.usecase.LoginUseCase
@@ -11,9 +13,8 @@ import com.threegap.bitnagil.presentation.login.model.LoginIntent
1113
import com.threegap.bitnagil.presentation.login.model.LoginSideEffect
1214
import com.threegap.bitnagil.presentation.login.model.LoginState
1315
import dagger.hilt.android.lifecycle.HiltViewModel
16+
import kotlinx.coroutines.launch
1417
import org.orbitmvi.orbit.syntax.simple.SimpleSyntax
15-
import org.orbitmvi.orbit.syntax.simple.intent
16-
import org.orbitmvi.orbit.syntax.simple.reduce
1718
import 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
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.threegap.bitnagil.presentation.login.model
22

3-
import com.kakao.sdk.auth.model.OAuthToken
43
import com.threegap.bitnagil.presentation.common.mviviewmodel.MviIntent
54

65
sealed class LoginIntent : MviIntent {
76
data class OnKakaoLoginClick(val onKakaoTalkLoginAvailable: Boolean) : LoginIntent()
8-
9-
data class OnKakaoLoginResult(
10-
val token: OAuthToken?,
11-
val error: Throwable?,
12-
) : LoginIntent()
7+
data class SetLoading(val isLoading: Boolean) : LoginIntent()
8+
data class LoginSuccess(val isGuest: Boolean) : LoginIntent()
9+
data object KakaoTalkLoginCancel : LoginIntent()
10+
data object LoginFailure : LoginIntent()
1311
}

0 commit comments

Comments
 (0)