Skip to content

Commit

Permalink
[Refactoring] Splash Screen Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ajou4095 committed Jan 28, 2024
1 parent 5b9ee33 commit 5066174
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 45 deletions.
4 changes: 0 additions & 4 deletions app/src/main/res/values/strings.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.splash

sealed interface SplashIntent
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.splash

data class SplashModel(
val state: SplashState
)
Original file line number Diff line number Diff line change
@@ -1,67 +1,39 @@
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
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<SplashEvent>,
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) {
Expand Down Expand Up @@ -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 { _, _ -> }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class SplashViewModel @Inject constructor(
}
}

fun onIntent(intent: SplashIntent) {

}

private suspend fun checkJwtToken() {
updateJwtTokenUseCase().onSuccess {
_event.emit(SplashEvent.Login.Success)
Expand Down
2 changes: 2 additions & 0 deletions presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">장부 관리 앱</string>

<!-- <screen_name>_<anything> 의 양식으로 작성 부탁드립니다. -->

<string name="dialog_confirm">확인</string>
Expand Down

0 comments on commit 5066174

Please sign in to comment.