From 84d64c8163fd0149ced079a7b610acfc52e83c6f Mon Sep 17 00:00:00 2001 From: Sangho Kim Date: Tue, 13 Aug 2024 20:53:19 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT/#68]=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=ED=86=A0=ED=81=B0=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/auth/login/LoginActivity.kt | 24 ++++++++----------- .../presentation/auth/login/LoginViewModel.kt | 15 ++++++------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/presentation/src/main/java/co/orange/presentation/auth/login/LoginActivity.kt b/presentation/src/main/java/co/orange/presentation/auth/login/LoginActivity.kt index 4ea953b3..b2d6d67c 100644 --- a/presentation/src/main/java/co/orange/presentation/auth/login/LoginActivity.kt +++ b/presentation/src/main/java/co/orange/presentation/auth/login/LoginActivity.kt @@ -9,7 +9,6 @@ import co.orange.core.base.BaseActivity import co.orange.core.extension.setOnSingleClickListener import co.orange.core.extension.stringOf import co.orange.core.extension.toast -import co.orange.core.state.UiState import co.orange.presentation.auth.phone.PhoneActivity import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.flow.distinctUntilChanged @@ -27,7 +26,7 @@ class LoginActivity : BaseActivity(R.layout.activity_login initLoginBtnListener() observeAppLoginAvailable() - observeChangeTokenState() + observeChangeTokenResult() } private fun initLoginBtnListener() { @@ -42,19 +41,16 @@ class LoginActivity : BaseActivity(R.layout.activity_login }.launchIn(lifecycleScope) } - private fun observeChangeTokenState() { - viewModel.changeTokenState.flowWithLifecycle(lifecycle).distinctUntilChanged() - .onEach { state -> - when (state) { - is UiState.Success -> { - Intent(this, PhoneActivity::class.java).apply { - startActivity(this) - } - finish() + private fun observeChangeTokenResult() { + viewModel.changeTokenResult.flowWithLifecycle(lifecycle).distinctUntilChanged() + .onEach { isSuccess -> + if (isSuccess) { + Intent(this, PhoneActivity::class.java).apply { + startActivity(this) } - - is UiState.Failure -> toast(stringOf(R.string.error_msg)) - else -> return@onEach + finish() + } else { + toast(stringOf(R.string.error_msg)) } }.launchIn(lifecycleScope) } diff --git a/presentation/src/main/java/co/orange/presentation/auth/login/LoginViewModel.kt b/presentation/src/main/java/co/orange/presentation/auth/login/LoginViewModel.kt index 91b4de22..a90b84fe 100644 --- a/presentation/src/main/java/co/orange/presentation/auth/login/LoginViewModel.kt +++ b/presentation/src/main/java/co/orange/presentation/auth/login/LoginViewModel.kt @@ -3,7 +3,7 @@ package co.orange.presentation.auth.login import android.content.Context import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import co.orange.core.state.UiState +import co.orange.domain.entity.request.AuthRequestModel import co.orange.domain.repository.AuthRepository import co.orange.domain.repository.UserRepository import com.kakao.sdk.auth.model.OAuthToken @@ -11,7 +11,9 @@ import com.kakao.sdk.common.model.ClientError import com.kakao.sdk.common.model.ClientErrorCause import com.kakao.sdk.user.UserApiClient import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import javax.inject.Inject @@ -26,8 +28,8 @@ class LoginViewModel private val _isAppLoginAvailable = MutableStateFlow(true) val isAppLoginAvailable: StateFlow = _isAppLoginAvailable - private val _changeTokenState = MutableStateFlow>(UiState.Empty) - val changeTokenState: StateFlow> = _changeTokenState + private val _changeTokenResult = MutableSharedFlow() + val changeTokenResult: SharedFlow = _changeTokenResult private var appLoginCallback: (OAuthToken?, Throwable?) -> Unit = { token, error -> if (error != null) { @@ -64,13 +66,12 @@ class LoginViewModel authRepository.postOauthDataToGetToken(AuthRequestModel(accessToken, KAKAO)) .onSuccess { with(userRepository) { - setTokens(it.accessToken, it.refreshToken) - setUserRole(it.userRoleString) + setTokens(it.accesstoken, it.refreshtoken) } - _changeTokenState.value = UiState.Success(it.userRoleString) + _changeTokenResult.emit(true) } .onFailure { - _changeTokenState.value = UiState.Failure(it.message.toString()) + _changeTokenResult.emit(false) } } }