-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add nav graph, navigation class
- Loading branch information
1 parent
01ad0b0
commit ac518ec
Showing
3 changed files
with
90 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
app/src/main/java/com/nicolas/freegames/ui/navigation/InfoPlayNavGraph.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.nicolas.freegames.ui.navigation | ||
|
||
import androidx.activity.compose.BackHandler | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.NavType | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.navigation.navArgument | ||
import com.nicolas.freegames.ui.details.DetailScreen | ||
import com.nicolas.freegames.ui.home.HomeScreen | ||
import com.nicolas.freegames.ui.onboarding.OnBoarding | ||
import com.nicolas.freegames.utils.UserStore | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
@Composable | ||
fun InfoPlayNavGraph( | ||
modifier: Modifier = Modifier, | ||
navController: NavHostController = rememberNavController(), | ||
startDestination : String = Route.HOME.name | ||
) { | ||
|
||
val navigationActions = remember(navController) { | ||
InfoPlayNavigationActions(navController) | ||
} | ||
|
||
val currentContext = LocalContext.current | ||
val store = UserStore(currentContext) | ||
val hasAccessed = store.hasAccessedScreen.collectAsState(initial = false) | ||
val startScreen = if (hasAccessed.value) startDestination else Route.ONBOARDING.name | ||
|
||
NavHost( | ||
navController = navController, | ||
startDestination = startScreen, | ||
modifier = modifier | ||
) { | ||
composable(route = Route.ONBOARDING.name) { | ||
OnBoarding( | ||
onClickButton = { | ||
CoroutineScope(context = Dispatchers.Default).launch { | ||
store.setHasAccessedScreen(hasAccessed = true) | ||
} | ||
navigationActions.navigateToHome() | ||
} | ||
) | ||
} | ||
composable(route = Route.HOME.name) { | ||
BackHandler(true) {} | ||
HomeScreen(navController = navController) | ||
} | ||
composable( | ||
route = "${Route.DETAILS.name}/{gameId}", | ||
arguments = listOf(navArgument("gameId") { type = NavType.StringType }) | ||
) { | ||
val gameId = remember { it.arguments?.getString("gameId") } | ||
DetailScreen( | ||
gameId = gameId, | ||
navController = navController | ||
) | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/nicolas/freegames/ui/navigation/InfoPlayNavigation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.nicolas.freegames.ui.navigation | ||
|
||
import androidx.navigation.NavGraph.Companion.findStartDestination | ||
import androidx.navigation.NavHostController | ||
|
||
class InfoPlayNavigationActions(navController: NavHostController) { | ||
|
||
val navigateToHome: () -> Unit = { | ||
navController.navigate(Route.HOME.name) { | ||
popUpTo(navController.graph.findStartDestination().id) { | ||
saveState = true | ||
} | ||
launchSingleTop = true | ||
restoreState = true | ||
} | ||
} | ||
} |