Cleanup show current tab
This commit is contained in:
		
							parent
							
								
									d268fcd389
								
							
						
					
					
						commit
						27526151b0
					
				
					 7 changed files with 38 additions and 57 deletions
				
			
		|  | @ -2,17 +2,8 @@ package be.ugent.sel.studeez | |||
| 
 | ||||
| import android.content.res.Resources | ||||
| import androidx.compose.foundation.layout.padding | ||||
| import androidx.compose.material.MaterialTheme | ||||
| 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.remember | ||||
| import androidx.compose.runtime.rememberCoroutineScope | ||||
| import androidx.compose.material.* | ||||
| import androidx.compose.runtime.* | ||||
| import androidx.compose.ui.Modifier | ||||
| import androidx.compose.ui.platform.LocalConfiguration | ||||
| import androidx.compose.ui.platform.LocalContext | ||||
|  | @ -21,6 +12,7 @@ import androidx.hilt.navigation.compose.hiltViewModel | |||
| import androidx.navigation.NavHostController | ||||
| import androidx.navigation.compose.NavHost | ||||
| import androidx.navigation.compose.composable | ||||
| import androidx.navigation.compose.currentBackStackEntryAsState | ||||
| import androidx.navigation.compose.rememberNavController | ||||
| import be.ugent.sel.studeez.common.composable.drawer.DrawerViewModel | ||||
| import be.ugent.sel.studeez.common.composable.navbar.NavigationBarViewModel | ||||
|  | @ -89,6 +81,8 @@ fun StudeezNavGraph( | |||
|     val drawerViewModel: DrawerViewModel = hiltViewModel() | ||||
|     val navBarViewModel: NavigationBarViewModel = hiltViewModel() | ||||
| 
 | ||||
|     val backStackEntry by appState.navController.currentBackStackEntryAsState() | ||||
| 
 | ||||
|     NavHost( | ||||
|         navController = appState.navController, | ||||
|         startDestination = StudeezDestinations.SPLASH_SCREEN, | ||||
|  | @ -106,6 +100,9 @@ fun StudeezNavGraph( | |||
|             appState.navigateAndPopUp(route, popUp) | ||||
|         } | ||||
| 
 | ||||
|         val getCurrentScreen: () -> String? = { | ||||
|             backStackEntry?.destination?.route | ||||
|         } | ||||
| 
 | ||||
|         composable(StudeezDestinations.SPLASH_SCREEN) { | ||||
|             SplashRoute(openAndPopUp, viewModel = hiltViewModel()) | ||||
|  | @ -123,6 +120,7 @@ fun StudeezNavGraph( | |||
|             HomeRoute( | ||||
|                 open, | ||||
|                 openAndPopUp, | ||||
|                 getCurrentScreen, | ||||
|                 viewModel = hiltViewModel(), | ||||
|                 drawerViewModel = drawerViewModel, | ||||
|                 navBarViewModel = navBarViewModel, | ||||
|  | @ -133,7 +131,7 @@ fun StudeezNavGraph( | |||
|         // TODO Sessions screen | ||||
| 
 | ||||
|         composable(StudeezDestinations.PROFILE_SCREEN) { | ||||
|             ProfileRoute(open, openAndPopUp, viewModel = hiltViewModel()) | ||||
|             ProfileRoute(open, openAndPopUp, getCurrentScreen, viewModel = hiltViewModel()) | ||||
|         } | ||||
| 
 | ||||
|         composable(StudeezDestinations.TIMER_OVERVIEW_SCREEN) { | ||||
|  | @ -161,6 +159,7 @@ fun StudeezNavGraph( | |||
|             TimerSelectionRoute( | ||||
|                 open, | ||||
|                 openAndPopUp, | ||||
|                 getCurrentScreen, | ||||
|                 viewModel = hiltViewModel(), | ||||
|                 drawerViewModel = drawerViewModel, | ||||
|                 navBarViewModel = navBarViewModel, | ||||
|  |  | |||
|  | @ -2,26 +2,19 @@ package be.ugent.sel.studeez.common.composable | |||
| 
 | ||||
| import androidx.compose.foundation.layout.PaddingValues | ||||
| import androidx.compose.foundation.layout.RowScope | ||||
| import androidx.compose.material.FabPosition | ||||
| import androidx.compose.material.Icon | ||||
| import androidx.compose.material.IconButton | ||||
| import androidx.compose.material.Scaffold | ||||
| import androidx.compose.material.ScaffoldState | ||||
| import androidx.compose.material.Text | ||||
| import androidx.compose.material.TopAppBar | ||||
| import androidx.compose.material.* | ||||
| import androidx.compose.material.icons.Icons | ||||
| import androidx.compose.material.icons.filled.Edit | ||||
| import androidx.compose.material.icons.filled.Menu | ||||
| import androidx.compose.material.rememberScaffoldState | ||||
| import androidx.compose.runtime.Composable | ||||
| import androidx.compose.runtime.rememberCoroutineScope | ||||
| import androidx.compose.ui.tooling.preview.Preview | ||||
| import be.ugent.sel.studeez.R | ||||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.common.composable.drawer.Drawer | ||||
| import be.ugent.sel.studeez.common.composable.drawer.DrawerActions | ||||
| import be.ugent.sel.studeez.common.composable.navbar.NavigationBar | ||||
| import be.ugent.sel.studeez.common.composable.navbar.NavigationBarActions | ||||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.ui.theme.StudeezTheme | ||||
| import kotlinx.coroutines.CoroutineScope | ||||
| import kotlinx.coroutines.launch | ||||
|  |  | |||
|  | @ -1,5 +1,6 @@ | |||
| package be.ugent.sel.studeez.common.composable.navbar | ||||
| 
 | ||||
| import android.util.Log | ||||
| import androidx.compose.material.BottomNavigation | ||||
| import androidx.compose.material.BottomNavigationItem | ||||
| import androidx.compose.material.Icon | ||||
|  | @ -19,7 +20,7 @@ import be.ugent.sel.studeez.ui.theme.StudeezTheme | |||
| import be.ugent.sel.studeez.R.string as AppText | ||||
| 
 | ||||
| data class NavigationBarActions( | ||||
|     val selectedTab: (String) -> Boolean, | ||||
|     val isSelectedTab: (String) -> Boolean, | ||||
|     val onHomeClick: () -> Unit, | ||||
|     val onTasksClick: () -> Unit, | ||||
|     val onSessionsClick: () -> Unit, | ||||
|  | @ -29,9 +30,13 @@ data class NavigationBarActions( | |||
| fun getNavigationBarActions( | ||||
|     navigationBarViewModel: NavigationBarViewModel, | ||||
|     open: (String) -> Unit, | ||||
|     getCurrentScreen: () -> String? | ||||
| ): NavigationBarActions { | ||||
|     return NavigationBarActions( | ||||
|         selectedTab = { navigationBarViewModel.isSelected(it) }, | ||||
|         isSelectedTab = { screen -> | ||||
|             Log.v("TEMP", getCurrentScreen().toString()) // TODO Remove | ||||
|             screen == getCurrentScreen() | ||||
|         }, | ||||
|         onHomeClick = { | ||||
|             navigationBarViewModel.onHomeClick(open) | ||||
|         }, | ||||
|  | @ -49,16 +54,15 @@ fun getNavigationBarActions( | |||
| 
 | ||||
| @Composable | ||||
| fun NavigationBar( | ||||
|     navigationBarActions: NavigationBarActions, | ||||
|     navigationBarActions: NavigationBarActions | ||||
| ) { | ||||
|     BottomNavigation( | ||||
|         elevation = 10.dp | ||||
|     ) { | ||||
| 
 | ||||
|         BottomNavigationItem( | ||||
|             icon = { Icon(imageVector = Icons.Default.List, resources().getString(AppText.home)) }, | ||||
|             label = { Text(text = resources().getString(AppText.home)) }, | ||||
|             selected = navigationBarActions.selectedTab(HOME_SCREEN), | ||||
|             selected = navigationBarActions.isSelectedTab(HOME_SCREEN), | ||||
|             onClick = navigationBarActions.onHomeClick | ||||
|         ) | ||||
| 
 | ||||
|  | @ -69,7 +73,7 @@ fun NavigationBar( | |||
|                 ) | ||||
|             }, | ||||
|             label = { Text(text = resources().getString(AppText.tasks)) }, | ||||
|             // TODO selected = navigationBarActions.selectedTab(TASKS_SCREEN), | ||||
|             // TODO selected = navigationBarActions.isSelectedTab(TASKS_SCREEN), | ||||
|             selected = false, | ||||
|             onClick = navigationBarActions.onTasksClick | ||||
|         ) | ||||
|  | @ -84,7 +88,7 @@ fun NavigationBar( | |||
|                 ) | ||||
|             }, | ||||
|             label = { Text(text = resources().getString(AppText.sessions)) }, | ||||
|             // TODO selected = navigationBarActions.selectedTab(SESSIONS_SCREEN), | ||||
|             // TODO selected = navigationBarActions.isSelectedTab(SESSIONS_SCREEN), | ||||
|             selected = false, | ||||
|             onClick = navigationBarActions.onSessionsClick | ||||
|         ) | ||||
|  | @ -96,7 +100,7 @@ fun NavigationBar( | |||
|                 ) | ||||
|             }, | ||||
|             label = { Text(text = resources().getString(AppText.profile)) }, | ||||
|             selected = navigationBarActions.selectedTab(PROFILE_SCREEN), | ||||
|             selected = navigationBarActions.isSelectedTab(PROFILE_SCREEN), | ||||
|             onClick = navigationBarActions.onProfileClick | ||||
|         ) | ||||
| 
 | ||||
|  | @ -107,6 +111,8 @@ fun NavigationBar( | |||
| @Composable | ||||
| fun NavigationBarPreview() { | ||||
|     StudeezTheme { | ||||
|         NavigationBar(NavigationBarActions({ false }, {}, {}, {}, {})) | ||||
|         NavigationBar( | ||||
|             navigationBarActions = NavigationBarActions({ false }, {}, {}, {}, {}), | ||||
|         ) | ||||
|     } | ||||
| } | ||||
|  | @ -7,41 +7,26 @@ import be.ugent.sel.studeez.navigation.StudeezDestinations.PROFILE_SCREEN | |||
| import be.ugent.sel.studeez.screens.StudeezViewModel | ||||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||||
| import javax.inject.Inject | ||||
| import javax.inject.Singleton | ||||
| 
 | ||||
| @HiltViewModel | ||||
| class NavigationBarViewModel @Inject constructor( | ||||
|     private val accountDAO: AccountDAO, | ||||
|     var selectedTab: SelectedTabState, | ||||
|     logService: LogService | ||||
| ) : StudeezViewModel(logService) { | ||||
| 
 | ||||
|     fun isSelected(screen: String): Boolean { | ||||
|         return screen == selectedTab.selectedTab | ||||
|     } | ||||
| 
 | ||||
|     fun onHomeClick(open: (String) -> Unit) { | ||||
|         selectedTab.selectedTab = HOME_SCREEN | ||||
|         open(HOME_SCREEN) | ||||
|     } | ||||
| 
 | ||||
|     fun onTasksClick(open: (String) -> Unit) { | ||||
|         // TODO | ||||
|         // selectedTab.selectedTab = TASKS_SCREEN | ||||
|     } | ||||
| 
 | ||||
|     fun onSessionsClick(open: (String) -> Unit) { | ||||
|         // TODO | ||||
|         // selectedTab.selectedTab = SESSIONS_SCREEN | ||||
|     } | ||||
| 
 | ||||
|     fun onProfileClick(open: (String) -> Unit) { | ||||
|         selectedTab.selectedTab = PROFILE_SCREEN | ||||
|         open(PROFILE_SCREEN) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @Singleton | ||||
| class SelectedTabState @Inject constructor() { | ||||
|     var selectedTab: String = HOME_SCREEN | ||||
| } | ||||
|  | @ -23,6 +23,7 @@ import be.ugent.sel.studeez.resources | |||
| fun HomeRoute( | ||||
|     open: (String) -> Unit, | ||||
|     openAndPopUp: (String, String) -> Unit, | ||||
|     getCurrentScreen: () -> String?, | ||||
|     viewModel: HomeViewModel, | ||||
|     drawerViewModel: DrawerViewModel, | ||||
|     navBarViewModel: NavigationBarViewModel, | ||||
|  | @ -30,7 +31,7 @@ fun HomeRoute( | |||
|     HomeScreen( | ||||
|         onStartSessionClick = { viewModel.onStartSessionClick(open) }, | ||||
|         drawerActions = getDrawerActions(drawerViewModel, open, openAndPopUp), | ||||
|         navigationBarActions = getNavigationBarActions(navBarViewModel, open), | ||||
|         navigationBarActions = getNavigationBarActions(navBarViewModel, open, getCurrentScreen), | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
|  | @ -45,7 +46,7 @@ fun HomeScreen( | |||
|         title = resources().getString(R.string.home), | ||||
|         drawerActions = drawerActions, | ||||
|         navigationBarActions = navigationBarActions, | ||||
|         action = { FriendsAction() } | ||||
|         barAction = { FriendsAction() } | ||||
|     ) { | ||||
|         BasicButton(R.string.start_session, Modifier.basicButton()) { | ||||
|             onStartSessionClick() | ||||
|  |  | |||
|  | @ -4,22 +4,17 @@ import androidx.compose.material.Icon | |||
| import androidx.compose.material.IconButton | ||||
| import androidx.compose.material.icons.Icons | ||||
| import androidx.compose.material.icons.filled.Edit | ||||
| 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.runtime.* | ||||
| import androidx.compose.ui.tooling.preview.Preview | ||||
| import androidx.hilt.navigation.compose.hiltViewModel | ||||
| import be.ugent.sel.studeez.R | ||||
| import be.ugent.sel.studeez.common.composable.Headline | ||||
| import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate | ||||
| import be.ugent.sel.studeez.resources | ||||
| 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.getNavigationBarActions | ||||
| import be.ugent.sel.studeez.resources | ||||
| import kotlinx.coroutines.CoroutineScope | ||||
| import be.ugent.sel.studeez.R.string as AppText | ||||
| 
 | ||||
|  | @ -42,12 +37,13 @@ fun getProfileActions( | |||
| fun ProfileRoute( | ||||
|     open: (String) -> Unit, | ||||
|     openAndPopUp: (String, String) -> Unit, | ||||
|     getCurrentScreen: () -> String?, | ||||
|     viewModel: ProfileViewModel, | ||||
| ) { | ||||
|     ProfileScreen( | ||||
|         profileActions = getProfileActions(viewModel, open), | ||||
|         drawerActions = getDrawerActions(hiltViewModel(), open, openAndPopUp), | ||||
|         navigationBarActions = getNavigationBarActions(hiltViewModel(), open), | ||||
|         navigationBarActions = getNavigationBarActions(hiltViewModel(), open, getCurrentScreen), | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
|  | @ -65,7 +61,7 @@ fun ProfileScreen( | |||
|         title = resources().getString(AppText.profile), | ||||
|         drawerActions = drawerActions, | ||||
|         navigationBarActions = navigationBarActions, | ||||
|         action = { EditAction(onClick = profileActions.onEditProfileClick) } | ||||
|         barAction = { EditAction(onClick = profileActions.onEditProfileClick) } | ||||
|     ) { | ||||
|         Headline(text = (username ?: resources().getString(R.string.no_username))) | ||||
|     } | ||||
|  |  | |||
|  | @ -41,6 +41,7 @@ fun getTimerSelectionActions( | |||
| fun TimerSelectionRoute( | ||||
|     open: (String) -> Unit, | ||||
|     openAndPopUp: (String, String) -> Unit, | ||||
|     getCurrentScreen: () -> String?, | ||||
|     viewModel: TimerSelectionViewModel, | ||||
|     drawerViewModel: DrawerViewModel, | ||||
|     navBarViewModel: NavigationBarViewModel, | ||||
|  | @ -48,7 +49,7 @@ fun TimerSelectionRoute( | |||
|     TimerSelectionScreen( | ||||
|         timerSelectionActions = getTimerSelectionActions(viewModel, open), | ||||
|         drawerActions = getDrawerActions(drawerViewModel, open, openAndPopUp), | ||||
|         navigationBarActions = getNavigationBarActions(navBarViewModel, open), | ||||
|         navigationBarActions = getNavigationBarActions(navBarViewModel, open, getCurrentScreen), | ||||
|     ) | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Reference in a new issue