-
Notifications
You must be signed in to change notification settings - Fork 0
[Modify] 스플래시에서 uiState와 uiEvent를 활용해서 분기처리하기 #284
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
Changes from 4 commits
aadce8e
9d653e0
6d2d257
40c44c8
0b3eba8
520fefd
babbf1c
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.eatssu.android.domain.usecase.auth | ||
|
|
||
| import com.eatssu.android.data.dto.response.BaseResponse | ||
| import com.eatssu.android.domain.repository.UserRepository | ||
| import kotlinx.coroutines.flow.Flow | ||
| import javax.inject.Inject | ||
|
|
||
| class GetIsAccessTokenValidUseCase @Inject constructor( | ||
| private val userRepository: UserRepository, | ||
| ) { | ||
| suspend operator fun invoke(): Flow<BaseResponse<Boolean>> = | ||
| userRepository.checkUserNameValidation("qkqh") //todo api 만들어지면 수정 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,12 @@ | ||
| package com.eatssu.android.presentation.login | ||
|
|
||
| import android.os.Bundle | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import androidx.activity.viewModels | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.eatssu.android.R | ||
| import com.eatssu.android.databinding.ActivityIntroBinding | ||
| import com.eatssu.android.presentation.main.MainActivity | ||
| import com.eatssu.android.presentation.util.showToast | ||
| import com.eatssu.android.presentation.util.startActivity | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
| import kotlinx.coroutines.flow.collectLatest | ||
|
|
@@ -17,36 +16,42 @@ import kotlinx.coroutines.launch | |
| class IntroActivity : AppCompatActivity() { | ||
|
|
||
| private val introViewModel: IntroViewModel by viewModels() | ||
| private lateinit var binding: ActivityIntroBinding | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_intro) | ||
|
|
||
| // 일정 시간 지연 이후 실행하기 위한 코드 | ||
| Handler(Looper.getMainLooper()).postDelayed({ | ||
|
|
||
| introViewModel.autoLogin() | ||
| binding = ActivityIntroBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| lifecycleScope.launch { | ||
| introViewModel.uiState.collectLatest { state -> | ||
| when (state) { | ||
| is IntroUiState.Loading -> { | ||
| // 할게 없는뎅? 그냥 뷰 보여주기 | ||
| } | ||
|
|
||
| lifecycleScope.launch { | ||
| introViewModel.uiState.collectLatest { | ||
| if (it.isAutoLogined) { | ||
| is IntroUiState.Success -> { | ||
| // 메인 액티비티로 이동 | ||
| startActivity<MainActivity>() | ||
|
|
||
| // 이전 키를 눌렀을 때 스플래스 스크린 화면으로 이동을 방지하기 위해 | ||
| // 이동한 다음 사용안함으로 finish 처리 | ||
| finish() | ||
| } else { | ||
| startActivity<LoginActivity>() | ||
| } | ||
|
|
||
| // 이전 키를 눌렀을 때 스플래스 스크린 화면으로 이동을 방지하기 위해 | ||
| // 이동한 다음 사용안함으로 finish 처리 | ||
| is IntroUiState.NoValidToken -> { | ||
| // 로그인 액티비티로 이동 | ||
| startActivity<LoginActivity>() | ||
|
||
| finish() | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| }, 2000) // 시간 2초 이후 실행 | ||
|
|
||
| introViewModel.uiEvent.collectLatest { event -> | ||
| when (event) { | ||
| is IntroUiEvent.ShowToast -> { | ||
| // 에러 메시지 표시 | ||
| showToast(event.error) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,41 +3,74 @@ package com.eatssu.android.presentation.login | |
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import com.eatssu.android.domain.usecase.auth.GetAccessTokenUseCase | ||
| import com.eatssu.android.domain.usecase.auth.GetIsAccessTokenValidUseCase | ||
| 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.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| @HiltViewModel | ||
| class IntroViewModel @Inject constructor( | ||
| private val getAccessTokenUseCase: GetAccessTokenUseCase | ||
| private val getAccessTokenUseCase: GetAccessTokenUseCase, | ||
| private val getIsAccessTokenValidUseCase: GetIsAccessTokenValidUseCase | ||
| ) : ViewModel() { | ||
|
|
||
| private val _uiState: MutableStateFlow<IntroState> = MutableStateFlow(IntroState()) | ||
| val uiState: StateFlow<IntroState> = _uiState.asStateFlow() | ||
| private val _uiState: MutableStateFlow<IntroUiState> = MutableStateFlow(IntroUiState.Loading) | ||
| val uiState: StateFlow<IntroUiState> = _uiState.asStateFlow() | ||
|
|
||
| private val _uiEvent = MutableSharedFlow<IntroUiEvent>() | ||
| val uiEvent: SharedFlow<IntroUiEvent> = _uiEvent | ||
|
|
||
| init { | ||
| autoLogin() | ||
| } | ||
|
|
||
| fun autoLogin() { | ||
| private fun autoLogin() { | ||
| viewModelScope.launch { | ||
| if (getAccessTokenUseCase().isEmpty()) { | ||
| _uiState.update { it.copy(isAutoLogined = false) } | ||
| } else { | ||
| _uiState.update { it.copy(isAutoLogined = true) } | ||
| _uiState.value = IntroUiState.Loading | ||
|
|
||
| try { | ||
| // 토큰 존재 여부 확인 | ||
| if (getAccessTokenUseCase().isEmpty()) { | ||
| _uiState.value = IntroUiState.NoValidToken | ||
| _uiEvent.emit(IntroUiEvent.ShowToast("로그인이 필요합니다")) | ||
| return@launch | ||
| } | ||
|
|
||
| checkValid() | ||
|
|
||
| } catch (e: Exception) { | ||
| _uiState.value = IntroUiState.NoValidToken | ||
| _uiEvent.emit(IntroUiEvent.ShowToast("오류가 발생했습니다: ${e.message}")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun checkValid() { | ||
| viewModelScope.launch { | ||
| getIsAccessTokenValidUseCase() | ||
| .collect { | ||
| if (it.result == true) { //토큰이 있고 유효함 | ||
| _uiState.value = IntroUiState.Success | ||
| } else { //토큰이 있어도 유효하지 않음 | ||
| _uiState.value = IntroUiState.NoValidToken | ||
| _uiEvent.emit(IntroUiEvent.ShowToast("로그인이 필요합니다")) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sealed class IntroUiState { | ||
| object Loading : IntroUiState() | ||
| object Success : IntroUiState() | ||
| object NoValidToken : IntroUiState() | ||
|
||
| } | ||
|
|
||
| data class IntroState( | ||
| var toastMessage: String = "", | ||
| var loading: Boolean = true, | ||
| var error: Boolean = false, | ||
| var isAutoLogined: Boolean = false, | ||
| ) | ||
| sealed class IntroUiEvent { | ||
| data class ShowToast(val error: String) : IntroUiEvent() | ||
| } | ||
|
||
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.
The use-case currently calls 'checkUserNameValidation', which does not align with token validation. Update the repository method to reflect token validation once the proper API is available.