-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#203 [refact] 로그인 리팩토링 #204
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package hous.release.android.presentation.login | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MediatorLiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.kakao.sdk.auth.model.OAuthToken | ||
import com.kakao.sdk.common.model.AuthErrorCause | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import hous.release.android.util.extension.Event | ||
import hous.release.android.util.KakaoLoginCallback | ||
import hous.release.domain.entity.SplashState | ||
import hous.release.domain.usecase.GetFcmTokenUseCase | ||
import hous.release.domain.usecase.InitHousTokenUseCase | ||
import hous.release.domain.usecase.InitTokenUseCase | ||
import hous.release.domain.usecase.PostForceLoginUseCase | ||
import hous.release.domain.usecase.PostLoginUseCase | ||
import hous.release.domain.usecase.SetSplashStateUseCase | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.launch | ||
import retrofit2.HttpException | ||
import timber.log.Timber | ||
|
@@ -24,74 +24,31 @@ import javax.inject.Inject | |
@HiltViewModel | ||
class LoginViewModel @Inject constructor( | ||
private val postLoginUseCase: PostLoginUseCase, | ||
private val initTokenUseCase: InitTokenUseCase, | ||
private val initHousTokenUseCase: InitHousTokenUseCase, | ||
private val getFcmTokenUseCase: GetFcmTokenUseCase, | ||
private val postForceLoginUseCase: PostForceLoginUseCase, | ||
private val setSplashStateUseCase: SetSplashStateUseCase | ||
) : ViewModel() { | ||
private val kakaoToken = MutableLiveData<String>() | ||
private val kakaoToken = MutableStateFlow("") | ||
private val fcmToken = MutableStateFlow("") | ||
|
||
private val fcmToken = MutableLiveData<String>() | ||
private val _isSignedUp = MutableSharedFlow<Boolean>() | ||
val isSignedUp = _isSignedUp.asSharedFlow() | ||
|
||
private val _isSuccessKakaoLogin = MutableLiveData<Event<Boolean>>() | ||
val isSuccessKakaoLogin: LiveData<Event<Boolean>> = _isSuccessKakaoLogin | ||
private val _isJoiningRoom = MutableSharedFlow<Boolean>() | ||
val isJoiningRoom = _isJoiningRoom.asSharedFlow() | ||
|
||
private val _isJoiningRoom = MutableLiveData<Boolean>() | ||
val isJoiningRoom: LiveData<Boolean> = _isJoiningRoom | ||
private val _isMultipleAccess = MutableStateFlow(false) | ||
val isMiultipleAccess = _isMultipleAccess.asStateFlow() | ||
Comment on lines
+43
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단발성 이벤트인 것 같은데 stateflow를 사용하는 이유가 있을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
다이얼로그에서 취소하고, 다시 강제로그인을 재요청할 수 있어서 상태 저장하려고 stateFlow 사용했숨다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오히려 위와 같은 상황이라면 더더욱 sharedFlow를 사용해야되지 않을까요? stateFlow 는 같은 값은 수집 안하는 특성이 있어서 오히려 다이얼로그가 안뜰 것 같은 생각이 듭니다..! 그리고 사용 방식도 sharedFlow랑 비슷해보여서 바꾸는 것이 나을 것 같네요. 위에 상황 한번 해보고 후기 알려줘연~ 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
아 같은 값 수집 안 하는 게 sharedFlow인 줄 알았구만요~ 오케이 |
||
|
||
private val _isUser = MutableLiveData<Boolean>() | ||
val isUser: LiveData<Boolean> = _isUser | ||
|
||
private val _isMultipleAccess = MutableLiveData<Boolean>() | ||
val isMultipleAccess: LiveData<Boolean> = _isMultipleAccess | ||
|
||
private val _isInitUserInfo = MediatorLiveData<Event<Boolean>>().apply { | ||
addSource(kakaoToken) { token -> | ||
value = Event(token.isNotBlank() && fcmToken.value != null) | ||
} | ||
addSource(fcmToken) { token -> | ||
value = Event(token.isNotBlank() && kakaoToken.value != null) | ||
} | ||
val isKakaoLogin = combine(kakaoToken, fcmToken) { kakaoToken, fcmToken -> | ||
kakaoToken.isNotBlank() && fcmToken.isNotBlank() | ||
} | ||
val isInitUserInfo: LiveData<Event<Boolean>> = _isInitUserInfo | ||
|
||
val kakaoLoginCallback: (OAuthToken?, Throwable?) -> Unit = { token, error -> | ||
if (error != null) { | ||
when { | ||
error.toString() == AuthErrorCause.AccessDenied.toString() -> { | ||
Timber.e(error, "접근이 거부 됨(동의 취소)") | ||
} | ||
error.toString() == AuthErrorCause.InvalidClient.toString() -> { | ||
Timber.e(error, "유효하지 않은 앱") | ||
} | ||
error.toString() == AuthErrorCause.InvalidGrant.toString() -> { | ||
Timber.e(error, "인증 수단이 유효하지 않아 인증할 수 없는 상태") | ||
} | ||
error.toString() == AuthErrorCause.InvalidRequest.toString() -> { | ||
Timber.e(error, "요청 파라미터 오류") | ||
} | ||
error.toString() == AuthErrorCause.InvalidScope.toString() -> { | ||
Timber.e(error, "유효하지 않은 scope ID") | ||
} | ||
error.toString() == AuthErrorCause.Misconfigured.toString() -> { | ||
Timber.e(error, "설정이 올바르지 않음(android key hash)") | ||
} | ||
error.toString() == AuthErrorCause.ServerError.toString() -> { | ||
Timber.e(error, "서버 내부 에러") | ||
} | ||
error.toString() == AuthErrorCause.Unauthorized.toString() -> { | ||
Timber.e(error, "앱이 요청 권한이 없음") | ||
} | ||
else -> { | ||
Timber.e(error, "기타 에러") | ||
} | ||
} | ||
} else if (token != null) { | ||
Timber.e("카카오 로그인 성공 ${token.accessToken}") | ||
kakaoToken.value = token.accessToken | ||
_isSuccessKakaoLogin.value = Event(true) | ||
} | ||
KakaoLoginCallback { accessToken -> | ||
kakaoToken.value = accessToken | ||
}.handleResult(token, error) | ||
} | ||
|
||
init { | ||
|
@@ -101,19 +58,12 @@ class LoginViewModel @Inject constructor( | |
fun postLogin() { | ||
viewModelScope.launch { | ||
postLoginUseCase( | ||
fcmToken = requireNotNull(fcmToken.value), | ||
socialType = "KAKAO", | ||
token = requireNotNull(kakaoToken.value) | ||
fcmToken = fcmToken.value, | ||
socialType = SOCIAL_TYPE, | ||
token = kakaoToken.value | ||
).onSuccess { response -> | ||
initTokenUseCase( | ||
fcmToken = requireNotNull(fcmToken.value), | ||
socialType = SOCIAL_TYPE, | ||
token = requireNotNull(kakaoToken.value) | ||
) | ||
initHousTokenUseCase( | ||
token = response.token | ||
) | ||
_isJoiningRoom.value = response.isJoiningRoom | ||
initHousTokenUseCase(token = response.token) | ||
_isJoiningRoom.emit(response.isJoiningRoom) | ||
setSplashStateUseCase( | ||
if (response.isJoiningRoom) SplashState.MAIN | ||
else SplashState.ENTER_ROOM | ||
|
@@ -122,12 +72,7 @@ class LoginViewModel @Inject constructor( | |
if (throwable is HttpException) { | ||
when (throwable.code()) { | ||
NOT_SIGN_UP -> { | ||
initTokenUseCase( | ||
fcmToken = requireNotNull(fcmToken.value), | ||
socialType = SOCIAL_TYPE, | ||
token = requireNotNull(kakaoToken.value) | ||
) | ||
_isUser.value = false | ||
_isSignedUp.emit(false) | ||
Timber.e(throwable.message) | ||
} | ||
ALREADY_LOGIN -> { | ||
|
@@ -142,22 +87,15 @@ class LoginViewModel @Inject constructor( | |
} | ||
} | ||
|
||
fun initIsPermitAccess() { | ||
fun postForceLogin() { | ||
viewModelScope.launch { | ||
postForceLoginUseCase( | ||
fcmToken = requireNotNull(fcmToken.value), | ||
socialType = "KAKAO", | ||
token = requireNotNull(kakaoToken.value) | ||
fcmToken = fcmToken.value, | ||
socialType = SOCIAL_TYPE, | ||
token = kakaoToken.value | ||
).onSuccess { response -> | ||
_isJoiningRoom.value = response.isJoiningRoom | ||
initTokenUseCase( | ||
fcmToken = requireNotNull(fcmToken.value), | ||
socialType = SOCIAL_TYPE, | ||
token = requireNotNull(kakaoToken.value) | ||
) | ||
initHousTokenUseCase( | ||
token = response.token | ||
) | ||
_isJoiningRoom.emit(response.isJoiningRoom) | ||
initHousTokenUseCase(token = response.token) | ||
setSplashStateUseCase( | ||
if (response.isJoiningRoom) SplashState.MAIN | ||
else SplashState.ENTER_ROOM | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KakaoLoginService
를 필드에서 주입받아 사용 중인데저희가 사용하는 구조랑 어색한 것 같습니다..!