extract navbar and draweractions to StudeezNavGraph

This commit is contained in:
brreynie 2023-04-28 12:13:06 +02:00
parent cf91d727a0
commit 010c7cff54
6 changed files with 72 additions and 67 deletions

View file

@ -2,8 +2,18 @@ package be.ugent.sel.studeez
import android.content.res.Resources import android.content.res.Resources
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material.* import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.* import androidx.compose.material.Scaffold
import androidx.compose.material.ScaffoldState
import androidx.compose.material.Snackbar
import androidx.compose.material.SnackbarHost
import androidx.compose.material.Surface
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@ -14,8 +24,12 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import be.ugent.sel.studeez.common.composable.drawer.DrawerActions
import be.ugent.sel.studeez.common.composable.drawer.DrawerViewModel import be.ugent.sel.studeez.common.composable.drawer.DrawerViewModel
import be.ugent.sel.studeez.common.composable.drawer.getDrawerActions
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarViewModel import be.ugent.sel.studeez.common.composable.navbar.NavigationBarViewModel
import be.ugent.sel.studeez.common.composable.navbar.getNavigationBarActions
import be.ugent.sel.studeez.common.snackbar.SnackbarManager import be.ugent.sel.studeez.common.snackbar.SnackbarManager
import be.ugent.sel.studeez.navigation.StudeezDestinations import be.ugent.sel.studeez.navigation.StudeezDestinations
import be.ugent.sel.studeez.screens.home.HomeRoute import be.ugent.sel.studeez.screens.home.HomeRoute
@ -82,48 +96,51 @@ fun StudeezNavGraph(
val navBarViewModel: NavigationBarViewModel = hiltViewModel() val navBarViewModel: NavigationBarViewModel = hiltViewModel()
val backStackEntry by appState.navController.currentBackStackEntryAsState() val backStackEntry by appState.navController.currentBackStackEntryAsState()
val getCurrentScreen: () -> String? = { backStackEntry?.destination?.route }
val goBack: () -> Unit = { appState.popUp() }
val open: (String) -> Unit = { appState.navigate(it) }
val openAndPopUp: (String, String) -> Unit =
{ route, popUp -> appState.navigateAndPopUp(route, popUp) }
val drawerActions: DrawerActions = getDrawerActions(drawerViewModel, open, openAndPopUp)
val navigationBarActions: NavigationBarActions =
getNavigationBarActions(navBarViewModel, open, getCurrentScreen)
NavHost( NavHost(
navController = appState.navController, navController = appState.navController,
startDestination = StudeezDestinations.SPLASH_SCREEN, startDestination = StudeezDestinations.SPLASH_SCREEN,
modifier = modifier, modifier = modifier,
) { ) {
val goBack: () -> Unit = {
appState.popUp()
}
val open: (String) -> Unit = { route ->
appState.navigate(route)
}
val openAndPopUp: (String, String) -> Unit = { route, popUp ->
appState.navigateAndPopUp(route, popUp)
}
val getCurrentScreen: () -> String? = {
backStackEntry?.destination?.route
}
composable(StudeezDestinations.SPLASH_SCREEN) { composable(StudeezDestinations.SPLASH_SCREEN) {
SplashRoute(openAndPopUp, viewModel = hiltViewModel()) SplashRoute(
openAndPopUp,
viewModel = hiltViewModel(),
)
} }
composable(StudeezDestinations.LOGIN_SCREEN) { composable(StudeezDestinations.LOGIN_SCREEN) {
LoginRoute(openAndPopUp, viewModel = hiltViewModel()) LoginRoute(
openAndPopUp,
viewModel = hiltViewModel(),
)
} }
composable(StudeezDestinations.SIGN_UP_SCREEN) { composable(StudeezDestinations.SIGN_UP_SCREEN) {
SignUpRoute(openAndPopUp, viewModel = hiltViewModel()) SignUpRoute(
openAndPopUp,
viewModel = hiltViewModel(),
)
} }
composable(StudeezDestinations.HOME_SCREEN) { composable(StudeezDestinations.HOME_SCREEN) {
HomeRoute( HomeRoute(
open, open,
openAndPopUp,
getCurrentScreen,
viewModel = hiltViewModel(), viewModel = hiltViewModel(),
drawerViewModel = drawerViewModel, drawerActions = drawerActions,
navBarViewModel = navBarViewModel, navigationBarActions = navigationBarActions,
) )
} }
@ -131,20 +148,26 @@ fun StudeezNavGraph(
// TODO Sessions screen // TODO Sessions screen
composable(StudeezDestinations.PROFILE_SCREEN) { composable(StudeezDestinations.PROFILE_SCREEN) {
ProfileRoute(open, openAndPopUp, getCurrentScreen, viewModel = hiltViewModel()) ProfileRoute(
open,
viewModel = hiltViewModel(),
drawerActions = drawerActions,
navigationBarActions = navigationBarActions,
)
} }
composable(StudeezDestinations.TIMER_OVERVIEW_SCREEN) { composable(StudeezDestinations.TIMER_OVERVIEW_SCREEN) {
TimerOverviewRoute( TimerOverviewRoute(
open,
openAndPopUp,
viewModel = hiltViewModel(), viewModel = hiltViewModel(),
drawerViewModel = drawerViewModel drawerActions = drawerActions,
) )
} }
composable(StudeezDestinations.SESSION_SCREEN) { composable(StudeezDestinations.SESSION_SCREEN) {
SessionRoute(open, viewModel = hiltViewModel()) SessionRoute(
open,
viewModel = hiltViewModel()
)
} }
// TODO Timers screen // TODO Timers screen
@ -152,18 +175,18 @@ fun StudeezNavGraph(
// Edit screens // Edit screens
composable(StudeezDestinations.EDIT_PROFILE_SCREEN) { composable(StudeezDestinations.EDIT_PROFILE_SCREEN) {
EditProfileRoute(goBack, openAndPopUp, viewModel = hiltViewModel()) EditProfileRoute(
goBack,
openAndPopUp,
viewModel = hiltViewModel(),
)
} }
composable(StudeezDestinations.TIMER_SELECTION_SCREEN) { composable(StudeezDestinations.TIMER_SELECTION_SCREEN) {
TimerSelectionRoute( TimerSelectionRoute(
open, open,
openAndPopUp,
goBack, goBack,
getCurrentScreen,
viewModel = hiltViewModel(), viewModel = hiltViewModel(),
drawerViewModel = drawerViewModel,
navBarViewModel = navBarViewModel,
) )
} }
} }

View file

@ -11,27 +11,21 @@ import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.BasicButton import be.ugent.sel.studeez.common.composable.BasicButton
import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate
import be.ugent.sel.studeez.common.composable.drawer.DrawerActions import be.ugent.sel.studeez.common.composable.drawer.DrawerActions
import be.ugent.sel.studeez.common.composable.drawer.DrawerViewModel
import be.ugent.sel.studeez.common.composable.drawer.getDrawerActions
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarViewModel
import be.ugent.sel.studeez.common.composable.navbar.getNavigationBarActions
import be.ugent.sel.studeez.common.ext.basicButton import be.ugent.sel.studeez.common.ext.basicButton
import be.ugent.sel.studeez.resources import be.ugent.sel.studeez.resources
@Composable @Composable
fun HomeRoute( fun HomeRoute(
open: (String) -> Unit, open: (String) -> Unit,
openAndPopUp: (String, String) -> Unit,
getCurrentScreen: () -> String?,
viewModel: HomeViewModel, viewModel: HomeViewModel,
drawerViewModel: DrawerViewModel, drawerActions: DrawerActions,
navBarViewModel: NavigationBarViewModel, navigationBarActions: NavigationBarActions,
) { ) {
HomeScreen( HomeScreen(
onStartSessionClick = { viewModel.onStartSessionClick(open) }, onStartSessionClick = { viewModel.onStartSessionClick(open) },
drawerActions = getDrawerActions(drawerViewModel, open, openAndPopUp), drawerActions = drawerActions,
navigationBarActions = getNavigationBarActions(navBarViewModel, open, getCurrentScreen), navigationBarActions = navigationBarActions,
) )
} }
@ -41,7 +35,6 @@ fun HomeScreen(
drawerActions: DrawerActions, drawerActions: DrawerActions,
navigationBarActions: NavigationBarActions, navigationBarActions: NavigationBarActions,
) { ) {
PrimaryScreenTemplate( PrimaryScreenTemplate(
title = resources().getString(R.string.home), title = resources().getString(R.string.home),
drawerActions = drawerActions, drawerActions = drawerActions,

View file

@ -4,16 +4,18 @@ import androidx.compose.material.Icon
import androidx.compose.material.IconButton import androidx.compose.material.IconButton
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit import androidx.compose.material.icons.filled.Edit
import androidx.compose.runtime.* import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import be.ugent.sel.studeez.R import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.Headline import be.ugent.sel.studeez.common.composable.Headline
import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate
import be.ugent.sel.studeez.common.composable.drawer.DrawerActions import be.ugent.sel.studeez.common.composable.drawer.DrawerActions
import be.ugent.sel.studeez.common.composable.drawer.getDrawerActions
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions
import be.ugent.sel.studeez.common.composable.navbar.getNavigationBarActions
import be.ugent.sel.studeez.resources import be.ugent.sel.studeez.resources
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import be.ugent.sel.studeez.R.string as AppText import be.ugent.sel.studeez.R.string as AppText
@ -36,14 +38,14 @@ fun getProfileActions(
@Composable @Composable
fun ProfileRoute( fun ProfileRoute(
open: (String) -> Unit, open: (String) -> Unit,
openAndPopUp: (String, String) -> Unit,
getCurrentScreen: () -> String?,
viewModel: ProfileViewModel, viewModel: ProfileViewModel,
drawerActions: DrawerActions,
navigationBarActions: NavigationBarActions,
) { ) {
ProfileScreen( ProfileScreen(
profileActions = getProfileActions(viewModel, open), profileActions = getProfileActions(viewModel, open),
drawerActions = getDrawerActions(hiltViewModel(), open, openAndPopUp), drawerActions = drawerActions,
navigationBarActions = getNavigationBarActions(hiltViewModel(), open, getCurrentScreen), navigationBarActions = navigationBarActions,
) )
} }

View file

@ -1,6 +1,5 @@
package be.ugent.sel.studeez.screens.splash package be.ugent.sel.studeez.screens.splash
import android.window.SplashScreen
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column

View file

@ -13,8 +13,6 @@ import be.ugent.sel.studeez.common.composable.DrawerScreenTemplate
import be.ugent.sel.studeez.common.composable.StealthButton import be.ugent.sel.studeez.common.composable.StealthButton
import be.ugent.sel.studeez.common.composable.TimerEntry import be.ugent.sel.studeez.common.composable.TimerEntry
import be.ugent.sel.studeez.common.composable.drawer.DrawerActions import be.ugent.sel.studeez.common.composable.drawer.DrawerActions
import be.ugent.sel.studeez.common.composable.drawer.DrawerViewModel
import be.ugent.sel.studeez.common.composable.drawer.getDrawerActions
import be.ugent.sel.studeez.common.ext.basicButton import be.ugent.sel.studeez.common.ext.basicButton
import be.ugent.sel.studeez.data.local.models.timer_info.CustomTimerInfo import be.ugent.sel.studeez.data.local.models.timer_info.CustomTimerInfo
import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo
@ -40,14 +38,12 @@ fun getTimerOverviewActions(
@Composable @Composable
fun TimerOverviewRoute( fun TimerOverviewRoute(
open: (String) -> Unit,
openAndPopUp: (String, String) -> Unit,
viewModel: TimerOverviewViewModel, viewModel: TimerOverviewViewModel,
drawerViewModel: DrawerViewModel drawerActions: DrawerActions,
) { ) {
TimerOverviewScreen( TimerOverviewScreen(
timerOverviewActions = getTimerOverviewActions(viewModel), timerOverviewActions = getTimerOverviewActions(viewModel),
drawerActions = getDrawerActions(drawerViewModel, open, openAndPopUp) drawerActions = drawerActions,
) )
} }

View file

@ -2,8 +2,6 @@ package be.ugent.sel.studeez.screens.timer_selection
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
@ -11,8 +9,6 @@ import be.ugent.sel.studeez.R
import be.ugent.sel.studeez.common.composable.SecondaryScreenTemplate import be.ugent.sel.studeez.common.composable.SecondaryScreenTemplate
import be.ugent.sel.studeez.common.composable.StealthButton import be.ugent.sel.studeez.common.composable.StealthButton
import be.ugent.sel.studeez.common.composable.TimerEntry import be.ugent.sel.studeez.common.composable.TimerEntry
import be.ugent.sel.studeez.common.composable.drawer.DrawerViewModel
import be.ugent.sel.studeez.common.composable.navbar.NavigationBarViewModel
import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo
import be.ugent.sel.studeez.resources import be.ugent.sel.studeez.resources
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@ -36,12 +32,8 @@ fun getTimerSelectionActions(
@Composable @Composable
fun TimerSelectionRoute( fun TimerSelectionRoute(
open: (String) -> Unit, open: (String) -> Unit,
openAndPopUp: (String, String) -> Unit,
popUp: () -> Unit, popUp: () -> Unit,
getCurrentScreen: () -> String?,
viewModel: TimerSelectionViewModel, viewModel: TimerSelectionViewModel,
drawerViewModel: DrawerViewModel,
navBarViewModel: NavigationBarViewModel,
) { ) {
TimerSelectionScreen( TimerSelectionScreen(
timerSelectionActions = getTimerSelectionActions(viewModel, open), timerSelectionActions = getTimerSelectionActions(viewModel, open),