From 50661742ef41efbef43f716ebd350bbc178e1c17 Mon Sep 17 00:00:00 2001 From: Ray Jang Date: Mon, 29 Jan 2024 00:02:34 +0900 Subject: [PATCH] [Refactoring] Splash Screen Migration --- app/src/main/res/values/strings.xml | 4 - .../ui/main/splash/SplashDestination.kt | 19 ++++- .../ui/main/splash/SplashIntent.kt | 3 + .../ui/main/splash/SplashModel.kt | 5 ++ .../ui/main/splash/SplashScreen.kt | 81 ++++++++++--------- .../ui/main/splash/SplashViewModel.kt | 4 + presentation/src/main/res/values/strings.xml | 2 + 7 files changed, 73 insertions(+), 45 deletions(-) delete mode 100644 app/src/main/res/values/strings.xml create mode 100644 presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashIntent.kt create mode 100644 presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashModel.kt diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml deleted file mode 100644 index 2d26097d..00000000 --- a/app/src/main/res/values/strings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - 장부 관리 앱 - diff --git a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashDestination.kt b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashDestination.kt index 14758bdf..4535962a 100644 --- a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashDestination.kt +++ b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashDestination.kt @@ -1,6 +1,10 @@ package ac.dnd.bookkeeping.android.presentation.ui.main.splash +import ac.dnd.bookkeeping.android.presentation.common.util.ErrorObserver import ac.dnd.bookkeeping.android.presentation.ui.main.ApplicationState +import androidx.compose.runtime.getValue +import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavGraphBuilder import androidx.navigation.compose.composable import androidx.navigation.navArgument @@ -16,8 +20,21 @@ fun NavGraphBuilder.splashDestination( } ) ) { + + val viewModel: SplashViewModel = hiltViewModel() + + val model: SplashModel = let { + val state by viewModel.state.collectAsStateWithLifecycle() + SplashModel(state = state) + } + + ErrorObserver(viewModel) SplashScreen( - appState = appState + appState = appState, + model = model, + event = viewModel.event, + intent = viewModel::onIntent, + handler = viewModel.handler ) } } diff --git a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashIntent.kt b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashIntent.kt new file mode 100644 index 00000000..5bfaaa68 --- /dev/null +++ b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashIntent.kt @@ -0,0 +1,3 @@ +package ac.dnd.bookkeeping.android.presentation.ui.main.splash + +sealed interface SplashIntent diff --git a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashModel.kt b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashModel.kt new file mode 100644 index 00000000..68dbe434 --- /dev/null +++ b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashModel.kt @@ -0,0 +1,5 @@ +package ac.dnd.bookkeeping.android.presentation.ui.main.splash + +data class SplashModel( + val state: SplashState +) diff --git a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashScreen.kt b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashScreen.kt index b9e4f7b9..a1457a33 100644 --- a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashScreen.kt +++ b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashScreen.kt @@ -1,12 +1,14 @@ package ac.dnd.bookkeeping.android.presentation.ui.main.splash import ac.dnd.bookkeeping.android.presentation.R -import ac.dnd.bookkeeping.android.presentation.common.util.ErrorObserver import ac.dnd.bookkeeping.android.presentation.common.util.LaunchedEffectWithLifecycle +import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow +import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.eventObserve import ac.dnd.bookkeeping.android.presentation.ui.main.ApplicationState import ac.dnd.bookkeeping.android.presentation.ui.main.home.HomeConstant import ac.dnd.bookkeeping.android.presentation.ui.main.login.LoginConstant +import ac.dnd.bookkeeping.android.presentation.ui.main.rememberApplicationState import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -14,54 +16,24 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.size import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.hilt.navigation.compose.hiltViewModel -import androidx.lifecycle.compose.collectAsStateWithLifecycle +import kotlinx.coroutines.CoroutineExceptionHandler @Composable fun SplashScreen( appState: ApplicationState, - viewModel: SplashViewModel = hiltViewModel() + model: SplashModel, + event: EventFlow, + intent: (SplashIntent) -> Unit, + handler: CoroutineExceptionHandler ) { - val state by viewModel.state.collectAsStateWithLifecycle() - - Observer( - appState = appState, - viewModel = viewModel - ) - - Column( - modifier = Modifier - .fillMaxSize(), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center - ) { - Image( - modifier = Modifier.size(100.dp), - painter = painterResource(id = R.drawable.ic_launcher), - contentDescription = "" - ) - Text( - text = "Splash Screen", - fontSize = 20.sp, - color = Color.Black - ) - } -} - -@Composable -private fun Observer( - appState: ApplicationState, - viewModel: SplashViewModel -) { - ErrorObserver(viewModel) - fun navigateToLogin() { appState.navController.navigate(LoginConstant.ROUTE) { popUpTo(SplashConstant.ROUTE) { @@ -94,11 +66,40 @@ private fun Observer( } } - LaunchedEffectWithLifecycle(viewModel.event, viewModel.handler) { - viewModel.event.eventObserve { event -> + Column( + modifier = Modifier.fillMaxSize(), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Image( + modifier = Modifier.size(100.dp), + painter = painterResource(id = R.drawable.ic_launcher), + contentDescription = "" + ) + Text( + text = stringResource(id = R.string.app_name), + fontSize = 20.sp, + color = Color.Black + ) + } + + LaunchedEffectWithLifecycle(event, handler) { + event.eventObserve { event -> when (event) { is SplashEvent.Login -> login(event) } } } } + +@Preview +@Composable +fun SplashScreenPreview() { + SplashScreen( + appState = rememberApplicationState(), + model = SplashModel(state = SplashState.Init), + event = MutableEventFlow(), + intent = {}, + handler = CoroutineExceptionHandler { _, _ -> } + ) +} diff --git a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashViewModel.kt b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashViewModel.kt index e6b05af4..22c1608b 100644 --- a/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashViewModel.kt +++ b/presentation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/ui/main/splash/SplashViewModel.kt @@ -35,6 +35,10 @@ class SplashViewModel @Inject constructor( } } + fun onIntent(intent: SplashIntent) { + + } + private suspend fun checkJwtToken() { updateJwtTokenUseCase().onSuccess { _event.emit(SplashEvent.Login.Success) diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index 67f9d811..6e99639b 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -1,5 +1,7 @@ + 장부 관리 앱 + 확인