Skip to content

Commit

Permalink
[FEATURE/#23] 토큰 여부에 따른 분기처리
Browse files Browse the repository at this point in the history
  • Loading branch information
chattymin committed Jul 30, 2024
1 parent f04dbee commit e08e84e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kkkk.presentation.main.onboarding.splash

import android.annotation.SuppressLint
import android.os.Bundle
import android.provider.Settings
import androidx.activity.viewModels
Expand All @@ -10,6 +11,7 @@ import com.kkkk.core.extension.navigateToScreenClear
import com.kkkk.core.extension.setNavigationBarColorFromResource
import com.kkkk.core.extension.setStatusBarColorFromResource
import com.kkkk.core.extension.toast
import com.kkkk.presentation.main.MainActivity
import com.kkkk.presentation.main.onboarding.onbarding.OnboardingActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
Expand All @@ -26,13 +28,29 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_spl

setStatusBarColor()
setNavigationBarColor()
observeUserState()
login()
observeStates()

viewModel.checkTokenState()
}

private fun setStatusBarColor() = setStatusBarColorFromResource(R.color.purple_50)
private fun setNavigationBarColor() = setNavigationBarColorFromResource(R.color.purple_50)

private fun observeStates() {
observeTokenState()
observeUserState()
}

private fun observeTokenState() {
viewModel.tokenState.flowWithLifecycle(lifecycle).onEach { state ->
if (state) {
navigateToScreenClear<MainActivity>()
} else {
login()
}
}.launchIn(lifecycleScope)
}

private fun observeUserState() {
viewModel.userState.flowWithLifecycle(lifecycle).onEach { state ->
if (state) {
Expand All @@ -47,6 +65,7 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_spl
viewModel.setAndroidId(getDeviceTag())
}

@SuppressLint("HardwareIds")
private fun getDeviceTag(): String =
Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.kkkk.domain.repository.AuthRepository
import com.kkkk.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
Expand All @@ -16,10 +17,21 @@ class SplashViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val userRepository: UserRepository,
) : ViewModel() {
private val _tokenState = MutableSharedFlow<Boolean>(replay = 1)
val tokenState: SharedFlow<Boolean>
get() = _tokenState

private val _userState = MutableSharedFlow<Boolean>()
val userState: SharedFlow<Boolean>
get() = _userState

fun checkTokenState() {
viewModelScope.launch {
delay(DELAY_TIME)
_tokenState.emit(userRepository.getAccessToken().isNotBlank())
}
}

fun setAndroidId(deviceTag: String) {
viewModelScope.launch {
authRepository.login(deviceTag)
Expand All @@ -30,4 +42,8 @@ class SplashViewModel @Inject constructor(
}.onFailure(Timber::e)
}
}

companion object {
private const val DELAY_TIME = 1000L
}
}

0 comments on commit e08e84e

Please sign in to comment.