refactor navbar to not need viewmodel in constructor
This commit is contained in:
		
							parent
							
								
									9da9873b58
								
							
						
					
					
						commit
						05c37b4168
					
				
					 6 changed files with 71 additions and 27 deletions
				
			
		|  | @ -21,6 +21,7 @@ import be.ugent.sel.studeez.resources | |||
| import be.ugent.sel.studeez.screens.drawer.Drawer | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerActions | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBar | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarActions | ||||
| import be.ugent.sel.studeez.ui.theme.StudeezTheme | ||||
| import kotlinx.coroutines.CoroutineScope | ||||
| import kotlinx.coroutines.launch | ||||
|  | @ -28,9 +29,8 @@ import kotlinx.coroutines.launch | |||
| @Composable | ||||
| fun PrimaryScreenTemplate( | ||||
|     title: String, | ||||
|     open: (String) -> Unit, | ||||
|     openAndPopUp: (String, String) -> Unit, | ||||
|     drawerActions: DrawerActions, | ||||
|     navigationBarActions: NavigationBarActions, | ||||
|     action: @Composable RowScope.() -> Unit = {}, | ||||
|     content: @Composable (PaddingValues) -> Unit | ||||
| ) { | ||||
|  | @ -61,7 +61,7 @@ fun PrimaryScreenTemplate( | |||
|             Drawer(drawerActions) | ||||
|         }, | ||||
| 
 | ||||
|         bottomBar = { NavigationBar(open) }, | ||||
|         bottomBar = { NavigationBar(navigationBarActions) }, | ||||
|         floatingActionButtonPosition = FabPosition.Center, | ||||
|         isFloatingActionButtonDocked = true, | ||||
|         floatingActionButton = { CollapsedAddButton() } | ||||
|  | @ -76,9 +76,8 @@ fun PrimaryScreenPreview() { | |||
|     StudeezTheme { | ||||
|         PrimaryScreenTemplate( | ||||
|             "Preview screen", | ||||
|             {}, | ||||
|             { _, _ -> run {} }, | ||||
|             DrawerActions({}, {}, {}, {}, {}), | ||||
|             NavigationBarActions({}, {}, {}, {}), | ||||
|             { | ||||
|                 IconButton(onClick = { /*TODO*/ }) { | ||||
|                     Icon( | ||||
|  |  | |||
|  | @ -15,6 +15,8 @@ import be.ugent.sel.studeez.common.ext.basicButton | |||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerActions | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerViewModel | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarActions | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarViewModel | ||||
| 
 | ||||
| @Composable | ||||
| fun HomeRoute( | ||||
|  | @ -43,11 +45,17 @@ fun HomeScreen( | |||
|         onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) }, | ||||
|         onAboutClick = { drawerViewModel.onAboutClick(open) }, | ||||
|     ) | ||||
|     val navigationBarViewModel: NavigationBarViewModel = hiltViewModel() | ||||
|     val navigationBarActions = NavigationBarActions( | ||||
|         onHomeClick = { navigationBarViewModel.onHomeClick(open) }, | ||||
|         onTasksClick = { navigationBarViewModel.onTasksClick(open) }, | ||||
|         onSessionsClick = { navigationBarViewModel.onSessionsClick(open) }, | ||||
|         onProfileClick = { navigationBarViewModel.onProfileClick(open) }, | ||||
|     ) | ||||
|     PrimaryScreenTemplate( | ||||
|         title = resources().getString(R.string.home), | ||||
|         open = open, | ||||
|         openAndPopUp = openAndPopUp, | ||||
|         drawerActions = drawerActions, | ||||
|         navigationBarActions = navigationBarActions, | ||||
|         action = { FriendsAction() } | ||||
|     ) { | ||||
|         BasicButton(R.string.start_session, Modifier.basicButton()) { | ||||
|  |  | |||
|  | @ -12,16 +12,20 @@ import androidx.compose.material.icons.outlined.DateRange | |||
| import androidx.compose.runtime.Composable | ||||
| import androidx.compose.ui.tooling.preview.Preview | ||||
| import androidx.compose.ui.unit.dp | ||||
| import androidx.hilt.navigation.compose.hiltViewModel | ||||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.ui.theme.StudeezTheme | ||||
| import be.ugent.sel.studeez.R.string as AppText | ||||
| 
 | ||||
| data class NavigationBarActions( | ||||
|     val onHomeClick: () -> Unit, | ||||
|     val onTasksClick: () -> Unit, | ||||
|     val onSessionsClick: () -> Unit, | ||||
|     val onProfileClick: () -> Unit, | ||||
| ) | ||||
| 
 | ||||
| @Composable | ||||
| fun NavigationBar( | ||||
|     open: (String) -> Unit, | ||||
|     viewModel: NavigationBarViewModel = hiltViewModel() | ||||
|     navigationBarActions: NavigationBarActions, | ||||
| ) { | ||||
|     // TODO Pass functions and new screens. | ||||
|     // TODO Pass which screen is selected. | ||||
|  | @ -33,31 +37,43 @@ fun NavigationBar( | |||
|             icon = { Icon(imageVector = Icons.Default.List, resources().getString(AppText.home)) }, | ||||
|             label = { Text(text = resources().getString(AppText.home)) }, | ||||
|             selected = false, // TODO | ||||
|             onClick = { viewModel.onHomeClick(open) } | ||||
|             onClick = navigationBarActions.onHomeClick | ||||
|         ) | ||||
| 
 | ||||
|         BottomNavigationItem( | ||||
|             icon = { Icon(imageVector = Icons.Default.Check, resources().getString(AppText.tasks)) }, | ||||
|             icon = { | ||||
|                 Icon( | ||||
|                     imageVector = Icons.Default.Check, resources().getString(AppText.tasks) | ||||
|                 ) | ||||
|             }, | ||||
|             label = { Text(text = resources().getString(AppText.tasks)) }, | ||||
|             selected = false, // TODO | ||||
|             onClick = { viewModel.onTasksClick(open) } | ||||
|             onClick = navigationBarActions.onTasksClick | ||||
|         ) | ||||
| 
 | ||||
|         // Hack to space the entries in the navigation bar, make space for fab | ||||
|         BottomNavigationItem(icon = {}, onClick = {}, selected = false) | ||||
| 
 | ||||
|         BottomNavigationItem( | ||||
|             icon = { Icon(imageVector = Icons.Outlined.DateRange, resources().getString(AppText.sessions)) }, | ||||
|             icon = { | ||||
|                 Icon( | ||||
|                     imageVector = Icons.Outlined.DateRange, resources().getString(AppText.sessions) | ||||
|                 ) | ||||
|             }, | ||||
|             label = { Text(text = resources().getString(AppText.sessions)) }, | ||||
|             selected = false, // TODO | ||||
|             onClick = { viewModel.onSessionsClick(open) } | ||||
|             onClick = navigationBarActions.onSessionsClick | ||||
|         ) | ||||
| 
 | ||||
|         BottomNavigationItem( | ||||
|             icon = { Icon(imageVector = Icons.Default.Person, resources().getString(AppText.profile)) }, | ||||
|             icon = { | ||||
|                 Icon( | ||||
|                     imageVector = Icons.Default.Person, resources().getString(AppText.profile) | ||||
|                 ) | ||||
|             }, | ||||
|             label = { Text(text = resources().getString(AppText.profile)) }, | ||||
|             selected = false, // TODO | ||||
|             onClick = { viewModel.onProfileClick(open) } | ||||
|             onClick = navigationBarActions.onProfileClick | ||||
|         ) | ||||
| 
 | ||||
|     } | ||||
|  | @ -67,9 +83,6 @@ fun NavigationBar( | |||
| @Composable | ||||
| fun NavigationBarPreview() { | ||||
|     StudeezTheme { | ||||
|         NavigationBar( | ||||
|             { _ -> {} }, | ||||
|             hiltViewModel() | ||||
|         ) | ||||
|         NavigationBar(NavigationBarActions({}, {}, {}, {})) | ||||
|     } | ||||
| } | ||||
|  | @ -12,6 +12,8 @@ import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate | |||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerActions | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerViewModel | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarActions | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarViewModel | ||||
| import be.ugent.sel.studeez.R.string as AppText | ||||
| 
 | ||||
| @Composable | ||||
|  | @ -32,11 +34,17 @@ fun ProfileScreen( | |||
|         onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) }, | ||||
|         onAboutClick = { drawerViewModel.onAboutClick(open) }, | ||||
|     ) | ||||
|     val navigationBarViewModel: NavigationBarViewModel = hiltViewModel() | ||||
|     val navigationBarActions = NavigationBarActions( | ||||
|         onHomeClick = { navigationBarViewModel.onHomeClick(open) }, | ||||
|         onTasksClick = { navigationBarViewModel.onTasksClick(open) }, | ||||
|         onSessionsClick = { navigationBarViewModel.onSessionsClick(open) }, | ||||
|         onProfileClick = { navigationBarViewModel.onProfileClick(open) }, | ||||
|     ) | ||||
|     PrimaryScreenTemplate( | ||||
|         title = resources().getString(AppText.profile), | ||||
|         open = open, | ||||
|         openAndPopUp = openAndPopUp, | ||||
|         drawerActions = drawerActions, | ||||
|         navigationBarActions = navigationBarActions, | ||||
|         action = { EditAction { viewModel.onEditProfileClick(open) } } | ||||
|     ) { | ||||
|         Headline(text = (username ?: resources().getString(R.string.no_username))) | ||||
|  |  | |||
|  | @ -30,6 +30,8 @@ import be.ugent.sel.studeez.data.local.models.timer_info.TimerInfo | |||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerActions | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerViewModel | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarActions | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarViewModel | ||||
| 
 | ||||
| @Composable | ||||
| fun TimerOverviewScreen( | ||||
|  | @ -47,11 +49,17 @@ fun TimerOverviewScreen( | |||
|         onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) }, | ||||
|         onAboutClick = { drawerViewModel.onAboutClick(open) }, | ||||
|     ) | ||||
|     val navigationBarViewModel: NavigationBarViewModel = hiltViewModel() | ||||
|     val navigationBarActions = NavigationBarActions( | ||||
|         onHomeClick = { navigationBarViewModel.onHomeClick(open) }, | ||||
|         onTasksClick = { navigationBarViewModel.onTasksClick(open) }, | ||||
|         onSessionsClick = { navigationBarViewModel.onSessionsClick(open) }, | ||||
|         onProfileClick = { navigationBarViewModel.onProfileClick(open) }, | ||||
|     ) | ||||
|     PrimaryScreenTemplate( | ||||
|         title = resources().getString(R.string.timers), | ||||
|         open = open, | ||||
|         openAndPopUp = openAndPopUp, | ||||
|         drawerActions = drawerActions, | ||||
|         navigationBarActions = navigationBarActions, | ||||
|     ) { | ||||
| 
 | ||||
|         Column { | ||||
|  |  | |||
|  | @ -11,6 +11,8 @@ import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate | |||
| import be.ugent.sel.studeez.resources | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerActions | ||||
| import be.ugent.sel.studeez.screens.drawer.DrawerViewModel | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarActions | ||||
| import be.ugent.sel.studeez.screens.navbar.NavigationBarViewModel | ||||
| import be.ugent.sel.studeez.screens.timer_overview.TimerEntry | ||||
| 
 | ||||
| @Composable | ||||
|  | @ -29,11 +31,17 @@ fun TimerSelectionScreen( | |||
|         onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) }, | ||||
|         onAboutClick = { drawerViewModel.onAboutClick(open) }, | ||||
|     ) | ||||
|     val navigationBarViewModel: NavigationBarViewModel = hiltViewModel() | ||||
|     val navigationBarActions = NavigationBarActions( | ||||
|         onHomeClick = { navigationBarViewModel.onHomeClick(open) }, | ||||
|         onTasksClick = { navigationBarViewModel.onTasksClick(open) }, | ||||
|         onSessionsClick = { navigationBarViewModel.onSessionsClick(open) }, | ||||
|         onProfileClick = { navigationBarViewModel.onProfileClick(open) }, | ||||
|     ) | ||||
|     PrimaryScreenTemplate( | ||||
|         title = resources().getString(R.string.timers), | ||||
|         open = open, | ||||
|         openAndPopUp = openAndPopUp, | ||||
|         drawerActions = drawerActions, | ||||
|         navigationBarActions = navigationBarActions, | ||||
|     ) { | ||||
| 
 | ||||
|         LazyColumn(verticalArrangement = Arrangement.spacedBy(7.dp)) { | ||||
|  |  | |||
		Reference in a new issue
	
	 brreynie
						brreynie