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) } + } + } + } + } + } + } +}