11package com.raghav.spacedawnv2.ui
22
3- import android.app.Activity
43import android.os.Bundle
54import androidx.activity.ComponentActivity
65import androidx.activity.compose.setContent
7- import androidx.compose.animation.AnimatedContentTransitionScope
86import androidx.compose.foundation.layout.RowScope
97import androidx.compose.foundation.layout.fillMaxSize
108import androidx.compose.foundation.layout.padding
@@ -13,40 +11,30 @@ import androidx.compose.material3.MaterialTheme
1311import androidx.compose.material3.NavigationBar
1412import androidx.compose.material3.NavigationBarItem
1513import androidx.compose.material3.Scaffold
16- import androidx.compose.material3.SnackbarDuration
1714import androidx.compose.material3.SnackbarHost
1815import androidx.compose.material3.SnackbarHostState
19- import androidx.compose.material3.SnackbarResult
2016import androidx.compose.material3.Surface
2117import androidx.compose.material3.Text
2218import androidx.compose.runtime.Composable
2319import androidx.compose.runtime.getValue
2420import androidx.compose.runtime.remember
2521import androidx.compose.runtime.rememberCoroutineScope
26- import androidx.compose.runtime.rememberUpdatedState
2722import androidx.compose.ui.Modifier
28- import androidx.compose.ui.platform.LocalContext
29- import androidx.compose.ui.res.stringResource
3023import androidx.compose.ui.tooling.preview.Preview
3124import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
3225import androidx.navigation.NavDestination
3326import androidx.navigation.NavDestination.Companion.hierarchy
3427import androidx.navigation.NavHostController
35- import androidx.navigation.compose.NavHost
36- import androidx.navigation.compose.composable
3728import androidx.navigation.compose.currentBackStackEntryAsState
3829import androidx.navigation.compose.rememberNavController
39- import com.raghav.spacedawnv2.R
40- import com.raghav.spacedawnv2.launchesscreen.LaunchesScreen
4130import com.raghav.spacedawnv2.navigation.Destination
4231import com.raghav.spacedawnv2.navigation.LaunchesScreen
4332import com.raghav.spacedawnv2.navigation.RemindersScreen
44- import com.raghav.spacedawnv2.remindersscreen.RemindersScreen
33+ import com.raghav.spacedawnv2.navigation.SpaceDawnNavHost
4534import com.raghav.spacedawnv2.ui.theme.SpaceDawnTheme
4635import com.raghav.spacedawnv2.ui.theme.spacing
4736import com.raghav.spacedawnv2.util.Helpers.Companion.navigateSingleTopTo
4837import dagger.hilt.android.AndroidEntryPoint
49- import kotlinx.coroutines.launch
5038
5139@AndroidEntryPoint
5240class MainActivity : ComponentActivity () {
@@ -81,89 +69,12 @@ fun SpaceDawnApp(modifier: Modifier = Modifier) {
8169 SnackbarHost (snackbarHostState)
8270 }
8371 ) { innerPadding ->
84- NavHost (
72+ SpaceDawnNavHost (
8573 navController = navController,
86- startDestination = LaunchesScreen .route,
87- modifier = Modifier .padding(innerPadding)
88- ) {
89- // Launches Screen
90- composable(
91- LaunchesScreen .route,
92- enterTransition = {
93- slideIntoContainer(AnimatedContentTransitionScope .SlideDirection .Right )
94- },
95- exitTransition = {
96- slideOutOfContainer(AnimatedContentTransitionScope .SlideDirection .Left )
97- }
98- ) {
99- val activity = (LocalContext .current as ? Activity )
100- val actionLabel by rememberUpdatedState(
101- newValue = stringResource(id = R .string.reminders)
102- )
103- LaunchesScreen (
104- systemBackButtonClicked = { activity?.finish() },
105- reminderSetSuccessfully = {
106- scope.launch {
107- val actionTaken = snackbarHostState.showSnackbar(
108- it,
109- actionLabel = actionLabel,
110- withDismissAction = true ,
111- duration = SnackbarDuration .Short
112- )
113- when (actionTaken) {
114- SnackbarResult .ActionPerformed -> {
115- navController.navigateSingleTopTo(RemindersScreen .route)
116- }
117-
118- else -> {}
119- }
120- }
121- },
122- reminderNotSet = {
123- scope.launch {
124- snackbarHostState.showSnackbar(
125- it,
126- withDismissAction = true
127- )
128- }
129- }
130- )
131- }
132- // Reminders Screen
133- composable(
134- RemindersScreen .route,
135- enterTransition = {
136- slideIntoContainer(AnimatedContentTransitionScope .SlideDirection .Left )
137- },
138- exitTransition = {
139- slideOutOfContainer(AnimatedContentTransitionScope .SlideDirection .Right )
140- }
141- ) {
142- val snackBarMessage by rememberUpdatedState(
143- newValue = stringResource(R .string.reminder_cancelled_successfully)
144- )
145- RemindersScreen (
146- onBackPressed = { navController.navigateSingleTopTo(LaunchesScreen .route) },
147- reminderNotCancelled = { message ->
148- scope.launch {
149- snackbarHostState.showSnackbar(
150- message.toString(),
151- withDismissAction = true
152- )
153- }
154- },
155- reminderCancelled = {
156- scope.launch {
157- snackbarHostState.showSnackbar(
158- // need to extract to string res
159- snackBarMessage,
160- withDismissAction = true
161- )
162- }
163- }
164- )
165- }
166- }
74+ modifier = Modifier .padding(innerPadding),
75+ snackbarHostState = snackbarHostState,
76+ coroutineScope = scope
77+ )
16778 }
16879}
16980
0 commit comments