@@ -20,7 +20,6 @@ import androidx.compose.ui.Modifier
2020import androidx.compose.ui.platform.LocalContext
2121import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
2222import androidx.lifecycle.compose.collectAsStateWithLifecycle
23- import androidx.lifecycle.lifecycleScope
2423import androidx.localbroadcastmanager.content.LocalBroadcastManager
2524import androidx.navigation.compose.rememberNavController
2625import androidx.startup.AppInitializer
@@ -41,7 +40,6 @@ import com.pomonyang.mohanyang.presentation.util.MnNotificationManager
4140import com.pomonyang.mohanyang.presentation.util.MohanyangEventLogger
4241import com.pomonyang.mohanyang.presentation.util.collectWithLifecycle
4342import com.pomonyang.mohanyang.ui.MohaNyangApp
44- import com.pomonyang.mohanyang.ui.MohaNyangAppState
4543import com.pomonyang.mohanyang.ui.rememberMohaNyangAppState
4644import dagger.hilt.android.AndroidEntryPoint
4745import javax.inject.Inject
@@ -64,33 +62,19 @@ class MainActivity : ComponentActivity() {
6462
6563 private var keepSplashOnScreen = true
6664
65+ private var splashTime = 0L
66+
6767 override fun onCreate (savedInstanceState : Bundle ? ) {
6868 handleSplashScreen()
6969 super .onCreate(savedInstanceState)
70-
70+ viewModel.handleEvent( MainEvent . Init )
7171 configureAppStartup()
7272
7373 setContent {
7474 val activity = (LocalContext .current as ? Activity )
75-
7675 val state by viewModel.state.collectAsStateWithLifecycle()
77- val coroutineScope = rememberCoroutineScope()
78-
79- val mohaNyangAppState = rememberMohaNyangAppState(
80- isNewUser = viewModel.checkIfNewUser(),
81- networkMonitor = networkMonitor,
82- coroutineScope = coroutineScope,
83- navHostController = rememberNavController().apply {
84- NavigationViewTrackingEffect (navController = this )
85- },
86- )
87-
8876 var showDialog by remember { mutableStateOf(false ) }
8977
90- LaunchedEffect (Unit ) {
91- viewModel.handleEvent(MainEvent .Init )
92- }
93-
9478 viewModel.effects.collectWithLifecycle { effect ->
9579 when (effect) {
9680 MainEffect .ShowDialog -> {
@@ -125,7 +109,6 @@ class MainActivity : ComponentActivity() {
125109 AppScreen (
126110 modifier = Modifier ,
127111 viewState = state,
128- mohaNyangAppState = mohaNyangAppState,
129112 eventLogger = eventLogger,
130113 )
131114 }
@@ -137,9 +120,19 @@ class MainActivity : ComponentActivity() {
137120 fun AppScreen (
138121 modifier : Modifier ,
139122 viewState : MainState ,
140- mohaNyangAppState : MohaNyangAppState ,
141123 eventLogger : MohanyangEventLogger ,
142124 ) {
125+ LaunchedEffect (viewState.isDataFetched) {
126+ if (viewState.isDataFetched) {
127+ val loadDuration = System .currentTimeMillis() - splashTime
128+ val loadRemainTime = (SPLASH_DELAY - loadDuration).coerceAtLeast(0 )
129+ if (loadRemainTime > 0 ) {
130+ delay(loadRemainTime)
131+ }
132+ keepSplashOnScreen = false
133+ }
134+ }
135+
143136 when {
144137 viewState.isInternalError -> ServerErrorScreen (onClickNavigateToHome = { })
145138 viewState.isInvalidError ->
@@ -149,11 +142,17 @@ class MainActivity : ComponentActivity() {
149142 )
150143
151144 viewState.isLoading -> LoadingScreen (modifier = modifier)
152- else -> {
153- MohaNyangApp (
154- mohaNyangAppState = mohaNyangAppState,
155- logger = eventLogger,
145+ viewState.isDataFetched -> {
146+ val coroutineScope = rememberCoroutineScope()
147+ val mohaNyangAppState = rememberMohaNyangAppState(
148+ isNewUser = viewState.isNewUser!! ,
149+ networkMonitor = networkMonitor,
150+ coroutineScope = coroutineScope,
151+ navHostController = rememberNavController().apply {
152+ NavigationViewTrackingEffect (navController = this )
153+ },
156154 )
155+ MohaNyangApp (mohaNyangAppState = mohaNyangAppState, logger = eventLogger)
157156 }
158157 }
159158 }
@@ -178,11 +177,8 @@ class MainActivity : ComponentActivity() {
178177 }
179178
180179 private fun handleSplashScreen () {
180+ splashTime = System .currentTimeMillis()
181181 installSplashScreen().setKeepOnScreenCondition { keepSplashOnScreen }
182- lifecycleScope.launch {
183- delay(SPLASH_DELAY )
184- keepSplashOnScreen = false
185- }
186182 }
187183
188184 override fun onResume () {
0 commit comments