Skip to content

Commit

Permalink
feat: add nav graph, navigation class
Browse files Browse the repository at this point in the history
  • Loading branch information
aleixo-dev committed May 2, 2024
1 parent 01ad0b0 commit ac518ec
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 60 deletions.
63 changes: 3 additions & 60 deletions app/src/main/java/com/nicolas/freegames/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,18 @@ package com.nicolas.freegames

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
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.google.android.gms.ads.MobileAds
import com.nicolas.freegames.navigation.Screen
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.ui.navigation.InfoPlayNavGraph
import com.nicolas.freegames.ui.theme.BackgroundApplication
import com.nicolas.freegames.ui.theme.FreeGamesTheme
import com.nicolas.freegames.utils.UserStore
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
Expand All @@ -51,48 +33,9 @@ class MainActivity : ComponentActivity() {
keepScreenOpen = false
}
MobileAds.initialize(this)
NavControllerScreen()
InfoPlayNavGraph()
}
}
}
}
}

@Composable
fun NavControllerScreen() {
val navController = rememberNavController()
val currentContext = LocalContext.current
val store = UserStore(currentContext)
val hasAccessed = store.hasAccessedScreen.collectAsState(initial = false)
val startScreen = if (hasAccessed.value) Screen.HOME.name else Screen.ONBOARDING.name

NavHost(
navController = navController,
startDestination = startScreen
) {
composable(route = Screen.ONBOARDING.name) {
OnBoarding(
onClickButton = {
CoroutineScope(context = Dispatchers.Default).launch {
store.setHasAccessedScreen(hasAccessed = true)
}
navController.navigate(Screen.HOME.name)
}
)
}
composable(route = Screen.HOME.name) {
BackHandler(true) {}
HomeScreen(navController = navController)
}
composable(
route = "${Screen.DETAILS.name}/{gameId}",
arguments = listOf(navArgument("gameId") { type = NavType.StringType })
) {
val gameId = remember { it.arguments?.getString("gameId") }
DetailScreen(
gameId = gameId,
navController = navController
)
}
}
}
}
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
)
}
}

}
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
}
}
}

0 comments on commit ac518ec

Please sign in to comment.