From e21b8a0029a516a29128da70ea1f11462a3015ec Mon Sep 17 00:00:00 2001 From: yangsooplus Date: Tue, 2 Jan 2024 14:50:09 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Splash=EB=A5=BC=20=EB=B3=B4=EC=97=AC?= =?UTF-8?q?=EC=A3=BC=EA=B3=A0=20=EB=82=9C=20=EB=92=A4,=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=9D=BC=20=ED=99=94=EB=A9=B4=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../loginsignup/social/KakaoLoginHelper.kt | 2 +- .../susu/feature/navigator/MainActivity.kt | 37 +++++++++++-------- .../susu/feature/navigator/MainNavigator.kt | 3 -- .../com/susu/feature/navigator/MainScreen.kt | 5 +-- .../navigator/initialization/InitialRoute.kt | 2 +- .../navigator/initialization/MainContract.kt | 8 ++-- .../navigator/initialization/MainViewModel.kt | 29 ++++++++++++++- 7 files changed, 57 insertions(+), 29 deletions(-) diff --git a/feature/loginsignup/src/main/java/com/susu/feature/loginsignup/social/KakaoLoginHelper.kt b/feature/loginsignup/src/main/java/com/susu/feature/loginsignup/social/KakaoLoginHelper.kt index a541e466..1a33abd1 100644 --- a/feature/loginsignup/src/main/java/com/susu/feature/loginsignup/social/KakaoLoginHelper.kt +++ b/feature/loginsignup/src/main/java/com/susu/feature/loginsignup/social/KakaoLoginHelper.kt @@ -9,7 +9,7 @@ import com.kakao.sdk.common.model.ClientErrorCause import com.kakao.sdk.user.UserApiClient import timber.log.Timber -internal object KakaoLoginHelper { +object KakaoLoginHelper { fun login( context: Context, diff --git a/feature/navigator/src/main/java/com/susu/feature/navigator/MainActivity.kt b/feature/navigator/src/main/java/com/susu/feature/navigator/MainActivity.kt index 22afb869..0f1f079c 100644 --- a/feature/navigator/src/main/java/com/susu/feature/navigator/MainActivity.kt +++ b/feature/navigator/src/main/java/com/susu/feature/navigator/MainActivity.kt @@ -20,10 +20,12 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import com.susu.core.designsystem.theme.SusuTheme +import com.susu.feature.loginsignup.navigation.LoginSignupRoute +import com.susu.feature.navigator.initialization.InitialRoute import com.susu.feature.navigator.initialization.MainContract import com.susu.feature.navigator.initialization.MainViewModel +import com.susu.feature.received.navigation.ReceivedRoute import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @AndroidEntryPoint @@ -35,7 +37,7 @@ class MainActivity : ComponentActivity() { val splashScreen = installSplashScreen() super.onCreate(savedInstanceState) - var uiState: MainContract.MainState by mutableStateOf(MainContract.MainState.Loading) + var uiState: MainContract.MainState by mutableStateOf(MainContract.MainState()) // Update the uiState lifecycleScope.launch { @@ -46,26 +48,29 @@ class MainActivity : ComponentActivity() { } } - splashScreen.setKeepOnScreenCondition { - when (uiState) { - MainContract.MainState.Loading -> true - is MainContract.MainState.Initialized -> false - } - } + splashScreen.setKeepOnScreenCondition { uiState.isLoading } WindowCompat.setDecorFitsSystemWindows(window, false) setContent { SusuTheme { - MainScreen( - modifier = Modifier - .fillMaxSize() - .windowInsetsPadding( - WindowInsets.systemBars.only( - WindowInsetsSides.Vertical, + if (uiState.isLoading.not()) { + MainScreen( + modifier = Modifier + .fillMaxSize() + .windowInsetsPadding( + WindowInsets.systemBars.only( + WindowInsetsSides.Vertical, + ), ), - ), - ) + startDestination = when (uiState.initialRoute) { + InitialRoute.SIGNUP_VOTE -> LoginSignupRoute.Parent.route + InitialRoute.LOGIN -> LoginSignupRoute.Parent.route + InitialRoute.RECEIVED -> ReceivedRoute.route + InitialRoute.NONE -> LoginSignupRoute.Parent.route + }, + ) + } } } } diff --git a/feature/navigator/src/main/java/com/susu/feature/navigator/MainNavigator.kt b/feature/navigator/src/main/java/com/susu/feature/navigator/MainNavigator.kt index d7a54282..ab742b73 100644 --- a/feature/navigator/src/main/java/com/susu/feature/navigator/MainNavigator.kt +++ b/feature/navigator/src/main/java/com/susu/feature/navigator/MainNavigator.kt @@ -9,7 +9,6 @@ import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController import androidx.navigation.navOptions import com.susu.feature.community.navigation.navigateCommunity -import com.susu.feature.loginsignup.navigation.LoginSignupRoute import com.susu.feature.mypage.navigation.navigateMyPage import com.susu.feature.received.navigation.navigateReceived import com.susu.feature.sent.navigation.SentRoute @@ -23,8 +22,6 @@ internal class MainNavigator( @Composable get() = navController .currentBackStackEntryAsState().value?.destination - val startDestination = LoginSignupRoute.Parent.route - val currentTab: MainNavigationTab? @Composable get() = currentDestination ?.route diff --git a/feature/navigator/src/main/java/com/susu/feature/navigator/MainScreen.kt b/feature/navigator/src/main/java/com/susu/feature/navigator/MainScreen.kt index 6a7fa5bb..25c86334 100644 --- a/feature/navigator/src/main/java/com/susu/feature/navigator/MainScreen.kt +++ b/feature/navigator/src/main/java/com/susu/feature/navigator/MainScreen.kt @@ -7,8 +7,6 @@ import androidx.compose.animation.slideIn import androidx.compose.animation.slideOut import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.IntOffset @@ -27,6 +25,7 @@ import kotlinx.collections.immutable.toImmutableList @Composable internal fun MainScreen( modifier: Modifier = Modifier, + startDestination: String, navigator: MainNavigator = rememberMainNavigator(), ) { Scaffold( @@ -34,7 +33,7 @@ internal fun MainScreen( content = { innerPadding -> NavHost( navController = navigator.navController, - startDestination = navigator.startDestination, + startDestination = startDestination, ) { loginSignupNavGraph(navigator.navController) diff --git a/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/InitialRoute.kt b/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/InitialRoute.kt index 99c32429..1e3abd51 100644 --- a/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/InitialRoute.kt +++ b/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/InitialRoute.kt @@ -1,5 +1,5 @@ package com.susu.feature.navigator.initialization enum class InitialRoute { - SIGNUP_VOTE, LOGIN, RECEIVED + SIGNUP_VOTE, LOGIN, RECEIVED, NONE } diff --git a/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainContract.kt b/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainContract.kt index 61c6662b..c21df4f3 100644 --- a/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainContract.kt +++ b/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainContract.kt @@ -5,8 +5,8 @@ import com.susu.core.ui.base.UiState sealed interface MainContract { object MainEffect : SideEffect - sealed class MainState : UiState { - data object Loading : MainState() - data class Initialized(val initialRoute: InitialRoute) : MainState() - } + data class MainState( + val isLoading: Boolean = true, + val initialRoute: InitialRoute = InitialRoute.NONE + ) : UiState } diff --git a/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainViewModel.kt b/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainViewModel.kt index 2cad8a43..6fe6131e 100644 --- a/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainViewModel.kt +++ b/feature/navigator/src/main/java/com/susu/feature/navigator/initialization/MainViewModel.kt @@ -1,8 +1,35 @@ package com.susu.feature.navigator.initialization +import androidx.lifecycle.viewModelScope import com.susu.core.ui.base.BaseViewModel +import com.susu.domain.usecase.LoginUseCase +import com.susu.feature.loginsignup.social.KakaoLoginHelper import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel -class MainViewModel @Inject constructor() : BaseViewModel(MainContract.MainState.Loading) +class MainViewModel @Inject constructor( + private val loginUseCase: LoginUseCase, +) : BaseViewModel(MainContract.MainState()) { + init { + if (!KakaoLoginHelper.hasKakaoLoginHistory()) { + intent { copy(isLoading = false, initialRoute = InitialRoute.SIGNUP_VOTE) } + } else { + KakaoLoginHelper.getAccessToken { oauthAccessToken -> + if (oauthAccessToken == null) { + intent { copy(isLoading = false, initialRoute = InitialRoute.LOGIN) } + } else { + viewModelScope.launch { + loginUseCase(oauthAccessToken = oauthAccessToken) + .onSuccess { + intent { copy(isLoading = false, initialRoute = InitialRoute.RECEIVED) } + }.onFailure { + intent { copy(isLoading = false, initialRoute = InitialRoute.LOGIN) } + } + } + } + } + } + } +}