Skip to content

Commit abe5aab

Browse files
committed
refactor code
1 parent 997c8d7 commit abe5aab

File tree

2 files changed

+120
-95
lines changed

2 files changed

+120
-95
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.raghav.spacedawnv2.navigation
2+
3+
import android.app.Activity
4+
import androidx.compose.animation.AnimatedContentTransitionScope
5+
import androidx.compose.material3.SnackbarDuration
6+
import androidx.compose.material3.SnackbarHostState
7+
import androidx.compose.material3.SnackbarResult
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.runtime.getValue
10+
import androidx.compose.runtime.rememberUpdatedState
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.platform.LocalContext
13+
import androidx.compose.ui.res.stringResource
14+
import androidx.navigation.NavHostController
15+
import androidx.navigation.compose.NavHost
16+
import androidx.navigation.compose.composable
17+
import com.raghav.spacedawnv2.R
18+
import com.raghav.spacedawnv2.launchesscreen.LaunchesScreen
19+
import com.raghav.spacedawnv2.remindersscreen.RemindersScreen
20+
import com.raghav.spacedawnv2.util.Helpers.Companion.navigateSingleTopTo
21+
import kotlinx.coroutines.CoroutineScope
22+
import kotlinx.coroutines.launch
23+
24+
@Composable
25+
fun SpaceDawnNavHost(
26+
navController: NavHostController,
27+
modifier: Modifier = Modifier,
28+
snackbarHostState: SnackbarHostState,
29+
coroutineScope: CoroutineScope
30+
) {
31+
NavHost(
32+
navController = navController,
33+
startDestination = LaunchesScreen.route,
34+
modifier = modifier
35+
) {
36+
// Launches Screen
37+
composable(
38+
LaunchesScreen.route,
39+
enterTransition = {
40+
slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Right)
41+
},
42+
exitTransition = {
43+
slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Left)
44+
}
45+
) {
46+
val activity = (LocalContext.current as? Activity)
47+
val actionLabel by rememberUpdatedState(
48+
newValue = stringResource(id = R.string.reminders)
49+
)
50+
LaunchesScreen(
51+
systemBackButtonClicked = { activity?.finish() },
52+
reminderSetSuccessfully = {
53+
coroutineScope.launch {
54+
val actionTaken = snackbarHostState.showSnackbar(
55+
it,
56+
actionLabel = actionLabel,
57+
withDismissAction = true,
58+
duration = SnackbarDuration.Short
59+
)
60+
when (actionTaken) {
61+
SnackbarResult.ActionPerformed -> {
62+
navController.navigateSingleTopTo(RemindersScreen.route)
63+
}
64+
65+
else -> {}
66+
}
67+
}
68+
},
69+
reminderNotSet = {
70+
coroutineScope.launch {
71+
snackbarHostState.showSnackbar(
72+
it,
73+
withDismissAction = true
74+
)
75+
}
76+
}
77+
)
78+
}
79+
// Reminders Screen
80+
composable(
81+
RemindersScreen.route,
82+
enterTransition = {
83+
slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Left)
84+
},
85+
exitTransition = {
86+
slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Right)
87+
}
88+
) {
89+
val snackBarMessage by rememberUpdatedState(
90+
newValue = stringResource(R.string.reminder_cancelled_successfully)
91+
)
92+
RemindersScreen(
93+
onBackPressed = { navController.navigateSingleTopTo(LaunchesScreen.route) },
94+
reminderNotCancelled = { message ->
95+
coroutineScope.launch {
96+
snackbarHostState.showSnackbar(
97+
message.toString(),
98+
withDismissAction = true
99+
)
100+
}
101+
},
102+
reminderCancelled = {
103+
coroutineScope.launch {
104+
snackbarHostState.showSnackbar(
105+
// need to extract to string res
106+
snackBarMessage,
107+
withDismissAction = true
108+
)
109+
}
110+
}
111+
)
112+
}
113+
}
114+
}

app/src/main/java/com/raghav/spacedawnv2/ui/MainActivity.kt

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.raghav.spacedawnv2.ui
22

3-
import android.app.Activity
43
import android.os.Bundle
54
import androidx.activity.ComponentActivity
65
import androidx.activity.compose.setContent
7-
import androidx.compose.animation.AnimatedContentTransitionScope
86
import androidx.compose.foundation.layout.RowScope
97
import androidx.compose.foundation.layout.fillMaxSize
108
import androidx.compose.foundation.layout.padding
@@ -13,40 +11,30 @@ import androidx.compose.material3.MaterialTheme
1311
import androidx.compose.material3.NavigationBar
1412
import androidx.compose.material3.NavigationBarItem
1513
import androidx.compose.material3.Scaffold
16-
import androidx.compose.material3.SnackbarDuration
1714
import androidx.compose.material3.SnackbarHost
1815
import androidx.compose.material3.SnackbarHostState
19-
import androidx.compose.material3.SnackbarResult
2016
import androidx.compose.material3.Surface
2117
import androidx.compose.material3.Text
2218
import androidx.compose.runtime.Composable
2319
import androidx.compose.runtime.getValue
2420
import androidx.compose.runtime.remember
2521
import androidx.compose.runtime.rememberCoroutineScope
26-
import androidx.compose.runtime.rememberUpdatedState
2722
import androidx.compose.ui.Modifier
28-
import androidx.compose.ui.platform.LocalContext
29-
import androidx.compose.ui.res.stringResource
3023
import androidx.compose.ui.tooling.preview.Preview
3124
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
3225
import androidx.navigation.NavDestination
3326
import androidx.navigation.NavDestination.Companion.hierarchy
3427
import androidx.navigation.NavHostController
35-
import androidx.navigation.compose.NavHost
36-
import androidx.navigation.compose.composable
3728
import androidx.navigation.compose.currentBackStackEntryAsState
3829
import androidx.navigation.compose.rememberNavController
39-
import com.raghav.spacedawnv2.R
40-
import com.raghav.spacedawnv2.launchesscreen.LaunchesScreen
4130
import com.raghav.spacedawnv2.navigation.Destination
4231
import com.raghav.spacedawnv2.navigation.LaunchesScreen
4332
import com.raghav.spacedawnv2.navigation.RemindersScreen
44-
import com.raghav.spacedawnv2.remindersscreen.RemindersScreen
33+
import com.raghav.spacedawnv2.navigation.SpaceDawnNavHost
4534
import com.raghav.spacedawnv2.ui.theme.SpaceDawnTheme
4635
import com.raghav.spacedawnv2.ui.theme.spacing
4736
import com.raghav.spacedawnv2.util.Helpers.Companion.navigateSingleTopTo
4837
import dagger.hilt.android.AndroidEntryPoint
49-
import kotlinx.coroutines.launch
5038

5139
@AndroidEntryPoint
5240
class 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

Comments
 (0)