-
Notifications
You must be signed in to change notification settings - Fork 3
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
[FIX/#300,#302] Domain 레이어 Paging3 제거 & FlowWithLifecycle 적용 #304
Changes from 19 commits
da4b141
24dff9c
c7934c8
5bc8ef4
eadfa38
f499543
52ce691
6fe8431
5d5fbe1
742187a
3e5d302
e3a1e6b
f650378
3592911
07f5a0b
585b215
1d71ee8
7c2692e
d201cba
527b1b0
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 |
---|---|---|
|
@@ -3,6 +3,8 @@ package com.el.yello.presentation.auth | |
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.viewModels | ||
import androidx.lifecycle.flowWithLifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import com.el.yello.R | ||
import com.el.yello.databinding.ActivitySignInBinding | ||
import com.el.yello.presentation.auth.SignInViewModel.Companion.FRIEND_LIST | ||
|
@@ -17,17 +19,19 @@ import com.example.ui.context.toast | |
import com.example.ui.view.UiState | ||
import com.example.ui.view.setOnSingleClickListener | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
@AndroidEntryPoint | ||
class SignInActivity : BindingActivity<ActivitySignInBinding>(R.layout.activity_sign_in) { | ||
|
||
private val viewModel by viewModels<SignInViewModel>() | ||
|
||
private var userKakaoId: Long = 0 | ||
private var userName: String = "" | ||
private var userGender: String = "" | ||
private var userEmail: String = "" | ||
private var userImage: String = "" | ||
private var userName: String = String() | ||
private var userGender: String = String() | ||
private var userEmail: String = String() | ||
private var userImage: String = String() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
@@ -52,21 +56,21 @@ class SignInActivity : BindingActivity<ActivitySignInBinding>(R.layout.activity_ | |
} | ||
|
||
private fun observeDeviceTokenError() { | ||
viewModel.getDeviceTokenError.observe(this) { | ||
toast(getString(R.string.sign_in_error_connection)) | ||
} | ||
viewModel.getDeviceTokenError.flowWithLifecycle(lifecycle).onEach { error -> | ||
if (error) toast(getString(R.string.sign_in_error_connection)) | ||
}.launchIn(lifecycleScope) | ||
} | ||
|
||
// 카카오통 앱 로그인에 실패한 경우 웹 로그인 시도 | ||
private fun observeAppLoginError() { | ||
viewModel.isAppLoginAvailable.observe(this) { available -> | ||
viewModel.isAppLoginAvailable.flowWithLifecycle(lifecycle).onEach { available -> | ||
if (!available) viewModel.startKakaoLogIn(this) | ||
} | ||
}.launchIn(lifecycleScope) | ||
Comment on lines
+59
to
+68
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. 코드 스타일마다 다를 것 같긴 하지만... 지금 SignIn에 상태 관련된 변수가 많아서 sealed class 하나로 합쳐서 분기 처리하면 더 깔끔할 수도 있지 않을까 하는 생각이 들었습니다! |
||
} | ||
|
||
// 서비스 토큰 교체 서버 통신 결과에 따라서 분기 처리 진행 | ||
private fun observeChangeTokenState() { | ||
viewModel.postChangeTokenState.observe(this) { state -> | ||
viewModel.postChangeTokenState.flowWithLifecycle(lifecycle).onEach { state -> | ||
when (state) { | ||
is UiState.Success -> { | ||
// 200(가입된 아이디): 온보딩 뷰 생략하고 바로 메인 화면으로 이동 위해 유저 정보 받기 | ||
|
@@ -78,21 +82,21 @@ class SignInActivity : BindingActivity<ActivitySignInBinding>(R.layout.activity_ | |
// 403, 404 : 온보딩 뷰로 이동 위해 카카오 유저 정보 얻기 | ||
viewModel.getKakaoInfo() | ||
} else { | ||
// 401 : 에러 발생 | ||
// 나머지 : 에러 발생 | ||
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. 83번째 줄에 |
||
toast(getString(R.string.sign_in_error_connection)) | ||
} | ||
} | ||
|
||
is UiState.Loading -> {} | ||
is UiState.Loading -> return@onEach | ||
|
||
is UiState.Empty -> {} | ||
is UiState.Empty -> return@onEach | ||
} | ||
} | ||
}.launchIn(lifecycleScope) | ||
} | ||
|
||
// Failure -> 카카오에 등록된 유저 정보 받아온 후 친구목록 동의 화면으로 이동 | ||
private fun observeKakaoUserInfoState() { | ||
viewModel.getKakaoInfoState.observe(this) { state -> | ||
viewModel.getKakaoInfoState.flowWithLifecycle(lifecycle).onEach { state -> | ||
when (state) { | ||
is UiState.Success -> { | ||
userKakaoId = state.data?.id ?: 0 | ||
|
@@ -103,19 +107,17 @@ class SignInActivity : BindingActivity<ActivitySignInBinding>(R.layout.activity_ | |
viewModel.checkFriendsListValid() | ||
} | ||
|
||
is UiState.Failure -> { | ||
yelloSnackbar(binding.root, getString(R.string.msg_error)) | ||
} | ||
is UiState.Failure -> yelloSnackbar(binding.root, getString(R.string.msg_error)) | ||
|
||
is UiState.Empty -> {} | ||
is UiState.Empty -> return@onEach | ||
|
||
is UiState.Loading -> {} | ||
is UiState.Loading -> return@onEach | ||
} | ||
} | ||
}.launchIn(lifecycleScope) | ||
} | ||
|
||
private fun observeFriendsListValidState() { | ||
viewModel.getKakaoValidState.observe(this) { state -> | ||
viewModel.getKakaoValidState.flowWithLifecycle(lifecycle).onEach { state -> | ||
when (state) { | ||
is UiState.Success -> { | ||
val friendScope = state.data.find { it.id == FRIEND_LIST } | ||
|
@@ -126,18 +128,18 @@ class SignInActivity : BindingActivity<ActivitySignInBinding>(R.layout.activity_ | |
} | ||
} | ||
|
||
is UiState.Failure -> { | ||
yelloSnackbar(binding.root, getString(R.string.msg_error)) | ||
} | ||
is UiState.Empty -> {} | ||
is UiState.Loading -> {} | ||
is UiState.Failure -> yelloSnackbar(binding.root, getString(R.string.msg_error)) | ||
|
||
is UiState.Empty -> return@onEach | ||
|
||
is UiState.Loading -> return@onEach | ||
} | ||
} | ||
}.launchIn(lifecycleScope) | ||
} | ||
|
||
// Success -> 서버에 등록된 유저 정보가 있는지 확인 후 메인 액티비티로 이동 | ||
private fun observeUserDataState() { | ||
viewModel.getUserProfileState.observe(this) { state -> | ||
viewModel.getUserProfileState.flowWithLifecycle(lifecycle).onEach { state -> | ||
when (state) { | ||
is UiState.Success -> { | ||
if (viewModel.getIsFirstLoginData()) { | ||
|
@@ -151,17 +153,21 @@ class SignInActivity : BindingActivity<ActivitySignInBinding>(R.layout.activity_ | |
} | ||
} | ||
|
||
is UiState.Failure -> { | ||
yelloSnackbar(binding.root, getString(R.string.msg_error)) | ||
} | ||
is UiState.Failure -> yelloSnackbar(binding.root, getString(R.string.msg_error)) | ||
|
||
is UiState.Empty -> { | ||
yelloSnackbar(binding.root, getString(R.string.msg_error)) | ||
} | ||
is UiState.Empty -> return@onEach | ||
|
||
is UiState.Loading -> {} | ||
is UiState.Loading -> return@onEach | ||
} | ||
}.launchIn(lifecycleScope) | ||
} | ||
|
||
private fun startMainActivity() { | ||
Intent(this, MainActivity::class.java).apply { | ||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
startActivity(this) | ||
} | ||
finish() | ||
} | ||
|
||
private fun startSocialSyncActivity() { | ||
|
@@ -191,14 +197,6 @@ class SignInActivity : BindingActivity<ActivitySignInBinding>(R.layout.activity_ | |
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
} | ||
|
||
private fun startMainActivity() { | ||
Intent(this, MainActivity::class.java).apply { | ||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
startActivity(this) | ||
} | ||
finish() | ||
} | ||
|
||
companion object { | ||
const val EXTRA_KAKAO_ID = "KAKAO_ID" | ||
const val EXTRA_EMAIL = "KAKAO_EMAIL" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
package com.el.yello.presentation.auth | ||
|
||
import android.content.Context | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.el.yello.presentation.auth.SignInActivity.Companion.CODE_NOT_SIGNED_IN | ||
import com.el.yello.presentation.auth.SignInActivity.Companion.CODE_NO_UUID | ||
import com.example.domain.entity.AuthTokenRequestModel | ||
import com.example.domain.entity.AuthTokenModel | ||
import com.example.domain.entity.AuthTokenRequestModel | ||
import com.example.domain.entity.ProfileUserModel | ||
import com.example.domain.repository.AuthRepository | ||
import com.example.domain.repository.OnboardingRepository | ||
import com.example.domain.repository.ProfileRepository | ||
|
@@ -21,6 +20,8 @@ import com.kakao.sdk.user.UserApiClient | |
import com.kakao.sdk.user.model.Scope | ||
import com.kakao.sdk.user.model.User | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import retrofit2.HttpException | ||
import javax.inject.Inject | ||
|
@@ -32,27 +33,27 @@ class SignInViewModel @Inject constructor( | |
private val profileRepository: ProfileRepository | ||
) : ViewModel() { | ||
|
||
private val _postChangeTokenState = MutableLiveData<UiState<AuthTokenModel?>>() | ||
val postChangeTokenState: LiveData<UiState<AuthTokenModel?>> = _postChangeTokenState | ||
private val _postChangeTokenState = MutableStateFlow<UiState<AuthTokenModel>>(UiState.Empty) | ||
val postChangeTokenState: StateFlow<UiState<AuthTokenModel?>> = _postChangeTokenState | ||
|
||
private val _getUserProfileState = MutableLiveData<UiState<Unit>>() | ||
val getUserProfileState: LiveData<UiState<Unit>> = _getUserProfileState | ||
private val _getUserProfileState = MutableStateFlow<UiState<ProfileUserModel>>(UiState.Empty) | ||
val getUserProfileState: StateFlow<UiState<ProfileUserModel>> = _getUserProfileState | ||
|
||
private val _getKakaoInfoState = MutableLiveData<UiState<User?>>() | ||
val getKakaoInfoState: LiveData<UiState<User?>> = _getKakaoInfoState | ||
private val _getKakaoInfoState = MutableStateFlow<UiState<User>>(UiState.Empty) | ||
val getKakaoInfoState: StateFlow<UiState<User?>> = _getKakaoInfoState | ||
|
||
private val _getKakaoValidState = MutableLiveData<UiState<List<Scope>>>() | ||
val getKakaoValidState: LiveData<UiState<List<Scope>>> = _getKakaoValidState | ||
private val _getKakaoValidState = MutableStateFlow<UiState<List<Scope>>>(UiState.Empty) | ||
val getKakaoValidState: StateFlow<UiState<List<Scope>>> = _getKakaoValidState | ||
|
||
private val serviceTermsList = listOf(THUMBNAIL, EMAIL, FRIEND_LIST, NAME, GENDER) | ||
|
||
private var deviceToken = String() | ||
|
||
private val _getDeviceTokenError = MutableLiveData<String>() | ||
val getDeviceTokenError: LiveData<String> = _getDeviceTokenError | ||
private val _getDeviceTokenError = MutableStateFlow(false) | ||
val getDeviceTokenError: StateFlow<Boolean> = _getDeviceTokenError | ||
Comment on lines
+52
to
+53
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. Boolean 값을 저장하는 변수인데 조금 더 명확하게 네이밍해주는 건 어떨까요?! 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. 와 이 링크 미쳐따 |
||
|
||
private val _isAppLoginAvailable = MutableLiveData<Boolean>() | ||
val isAppLoginAvailable: LiveData<Boolean> = _isAppLoginAvailable | ||
private val _isAppLoginAvailable = MutableStateFlow(true) | ||
val isAppLoginAvailable: StateFlow<Boolean> = _isAppLoginAvailable | ||
|
||
var isResigned = false | ||
private set | ||
|
@@ -82,10 +83,11 @@ class SignInViewModel @Inject constructor( | |
|
||
fun initLoginState() { | ||
_isAppLoginAvailable.value = true | ||
_getDeviceTokenError.value = false | ||
} | ||
|
||
fun startKakaoLogIn(context: Context) { | ||
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context) && isAppLoginAvailable.value == true) { | ||
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context) && isAppLoginAvailable.value) { | ||
UserApiClient.instance.loginWithKakaoTalk( | ||
context = context, | ||
callback = appLoginCallback, | ||
|
@@ -117,11 +119,14 @@ class SignInViewModel @Inject constructor( | |
|
||
fun checkFriendsListValid() { | ||
val scopes = mutableListOf(FRIEND_LIST) | ||
UserApiClient.instance.scopes(scopes) { scopeInfo, error-> | ||
_getKakaoValidState.value = UiState.Loading | ||
UserApiClient.instance.scopes(scopes) { scopeInfo, error -> | ||
if (error != null) { | ||
_getKakaoValidState.value = UiState.Failure(error.message.toString()) | ||
} else if (scopeInfo != null) { | ||
_getKakaoValidState.value = UiState.Success(scopeInfo.scopes ?: listOf()) | ||
} else { | ||
_getKakaoValidState.value = UiState.Failure(ERROR) | ||
} | ||
} | ||
} | ||
|
@@ -132,8 +137,8 @@ class SignInViewModel @Inject constructor( | |
social: String = KAKAO, | ||
deviceToken: String | ||
) { | ||
_postChangeTokenState.value = UiState.Loading | ||
viewModelScope.launch { | ||
_postChangeTokenState.value = UiState.Loading | ||
onboardingRepository.postTokenToServiceToken( | ||
AuthTokenRequestModel(accessToken, social, deviceToken), | ||
) | ||
|
@@ -159,15 +164,14 @@ class SignInViewModel @Inject constructor( | |
|
||
// 서버통신 - (가입되어 있는) 유저 정보 가져오기 | ||
fun getUserDataFromServer() { | ||
_getUserProfileState.value = UiState.Loading | ||
viewModelScope.launch { | ||
profileRepository.getUserData() | ||
.onSuccess { profile -> | ||
if (profile == null) { | ||
_getUserProfileState.value = UiState.Empty | ||
return@launch | ||
if (profile != null) { | ||
_getUserProfileState.value = UiState.Success(profile) | ||
authRepository.setYelloId(profile.yelloId) | ||
} | ||
_getUserProfileState.value = UiState.Success(Unit) | ||
authRepository.setYelloId(profile.yelloId) | ||
} | ||
.onFailure { t -> | ||
if (t is HttpException) { | ||
|
@@ -184,14 +188,12 @@ class SignInViewModel @Inject constructor( | |
deviceToken = task.result | ||
authRepository.setDeviceToken(deviceToken) | ||
} else { | ||
_getDeviceTokenError.value = task.result | ||
_getDeviceTokenError.value = true | ||
} | ||
} | ||
} | ||
|
||
fun getIsFirstLoginData(): Boolean { | ||
return authRepository.getIsFirstLoginData() | ||
} | ||
fun getIsFirstLoginData() = authRepository.getIsFirstLoginData() | ||
|
||
companion object { | ||
const val KAKAO = "KAKAO" | ||
|
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.
똑같이 빈 문자열로 초기화될 것 같은데 이렇게 바꾼 이유가 있나요?
별 이유 없다면 먼가
""
요게 더 직관적으로 빈 문자열이라고 이해될 것 같은 늑김...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.
코드에서 ""처럼 초록색 스트링값 안봤으면 해서 추출하는 느낌으로 전환해봤서요..!