Skip to content

Commit

Permalink
feat: Splash를 보여주고 난 뒤, 유저 데이터에 따라 화면 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsooplus committed Jan 2, 2024
1 parent 5f5b40e commit e21b8a0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
},
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,14 +25,15 @@ import kotlinx.collections.immutable.toImmutableList
@Composable
internal fun MainScreen(
modifier: Modifier = Modifier,
startDestination: String,
navigator: MainNavigator = rememberMainNavigator(),
) {
Scaffold(
modifier = modifier,
content = { innerPadding ->
NavHost(
navController = navigator.navController,
startDestination = navigator.startDestination,
startDestination = startDestination,
) {
loginSignupNavGraph(navigator.navController)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.susu.feature.navigator.initialization

enum class InitialRoute {
SIGNUP_VOTE, LOGIN, RECEIVED
SIGNUP_VOTE, LOGIN, RECEIVED, NONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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, MainContract.MainEffect>(MainContract.MainState.Loading)
class MainViewModel @Inject constructor(
private val loginUseCase: LoginUseCase,
) : BaseViewModel<MainContract.MainState, MainContract.MainEffect>(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) }
}
}
}
}
}
}
}

0 comments on commit e21b8a0

Please sign in to comment.