refactor primaryScreenTemplate to not create drawerViewModel
This commit is contained in:
		
							parent
							
								
									2b2cc879b3
								
							
						
					
					
						commit
						9da9873b58
					
				
					 6 changed files with 122 additions and 43 deletions
				
			
		| 
						 | 
				
			
			@ -2,16 +2,24 @@ package be.ugent.sel.studeez.common.composable
 | 
			
		|||
 | 
			
		||||
import androidx.compose.foundation.layout.PaddingValues
 | 
			
		||||
import androidx.compose.foundation.layout.RowScope
 | 
			
		||||
import androidx.compose.material.*
 | 
			
		||||
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.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.screens.drawer.Drawer
 | 
			
		||||
import be.ugent.sel.studeez.screens.drawer.DrawerActions
 | 
			
		||||
import be.ugent.sel.studeez.screens.navbar.NavigationBar
 | 
			
		||||
import be.ugent.sel.studeez.ui.theme.StudeezTheme
 | 
			
		||||
import kotlinx.coroutines.CoroutineScope
 | 
			
		||||
| 
						 | 
				
			
			@ -22,6 +30,7 @@ fun PrimaryScreenTemplate(
 | 
			
		|||
    title: String,
 | 
			
		||||
    open: (String) -> Unit,
 | 
			
		||||
    openAndPopUp: (String, String) -> Unit,
 | 
			
		||||
    drawerActions: DrawerActions,
 | 
			
		||||
    action: @Composable RowScope.() -> Unit = {},
 | 
			
		||||
    content: @Composable (PaddingValues) -> Unit
 | 
			
		||||
) {
 | 
			
		||||
| 
						 | 
				
			
			@ -31,23 +40,25 @@ fun PrimaryScreenTemplate(
 | 
			
		|||
    Scaffold(
 | 
			
		||||
        scaffoldState = scaffoldState,
 | 
			
		||||
 | 
			
		||||
        topBar = { TopAppBar(
 | 
			
		||||
            title = { Text(text = title) },
 | 
			
		||||
            navigationIcon = {
 | 
			
		||||
                IconButton(onClick = {
 | 
			
		||||
                    coroutineScope.launch { scaffoldState.drawerState.open() }
 | 
			
		||||
                }) {
 | 
			
		||||
                    Icon(
 | 
			
		||||
                        imageVector = Icons.Default.Menu,
 | 
			
		||||
                        contentDescription = resources().getString(R.string.menu)
 | 
			
		||||
                    )
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            actions = action
 | 
			
		||||
        ) },
 | 
			
		||||
        topBar = {
 | 
			
		||||
            TopAppBar(
 | 
			
		||||
                title = { Text(text = title) },
 | 
			
		||||
                navigationIcon = {
 | 
			
		||||
                    IconButton(onClick = {
 | 
			
		||||
                        coroutineScope.launch { scaffoldState.drawerState.open() }
 | 
			
		||||
                    }) {
 | 
			
		||||
                        Icon(
 | 
			
		||||
                            imageVector = Icons.Default.Menu,
 | 
			
		||||
                            contentDescription = resources().getString(R.string.menu)
 | 
			
		||||
                        )
 | 
			
		||||
                    }
 | 
			
		||||
                },
 | 
			
		||||
                actions = action
 | 
			
		||||
            )
 | 
			
		||||
        },
 | 
			
		||||
 | 
			
		||||
        drawerContent = {
 | 
			
		||||
            Drawer(open, openAndPopUp)
 | 
			
		||||
            Drawer(drawerActions)
 | 
			
		||||
        },
 | 
			
		||||
 | 
			
		||||
        bottomBar = { NavigationBar(open) },
 | 
			
		||||
| 
						 | 
				
			
			@ -65,14 +76,17 @@ fun PrimaryScreenPreview() {
 | 
			
		|||
    StudeezTheme {
 | 
			
		||||
        PrimaryScreenTemplate(
 | 
			
		||||
            "Preview screen",
 | 
			
		||||
            { _ -> {}},
 | 
			
		||||
            { _, _ -> {}},
 | 
			
		||||
            { IconButton(onClick = { /*TODO*/ }) {
 | 
			
		||||
                Icon(
 | 
			
		||||
                    imageVector = Icons.Default.Edit,
 | 
			
		||||
                    contentDescription = "Edit"
 | 
			
		||||
                )
 | 
			
		||||
            }}
 | 
			
		||||
            {},
 | 
			
		||||
            { _, _ -> run {} },
 | 
			
		||||
            DrawerActions({}, {}, {}, {}, {}),
 | 
			
		||||
            {
 | 
			
		||||
                IconButton(onClick = { /*TODO*/ }) {
 | 
			
		||||
                    Icon(
 | 
			
		||||
                        imageVector = Icons.Default.Edit,
 | 
			
		||||
                        contentDescription = "Edit"
 | 
			
		||||
                    )
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
        ) {}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -23,14 +23,17 @@ import be.ugent.sel.studeez.R
 | 
			
		|||
import be.ugent.sel.studeez.resources
 | 
			
		||||
import be.ugent.sel.studeez.ui.theme.StudeezTheme
 | 
			
		||||
 | 
			
		||||
data class DrawerActions(
 | 
			
		||||
    val onHomeButtonClick: () -> Unit,
 | 
			
		||||
    val onTimersClick: () -> Unit,
 | 
			
		||||
    val onSettingsClick: () -> Unit,
 | 
			
		||||
    val onLogoutClick: () -> Unit,
 | 
			
		||||
    val onAboutClick: () -> Unit,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun Drawer(
 | 
			
		||||
    onHomeButtonClick: () -> Unit,
 | 
			
		||||
    onTimersClick: () -> Unit,
 | 
			
		||||
    onSettingsClick: () -> Unit,
 | 
			
		||||
    onLogoutClick: () -> Unit,
 | 
			
		||||
    onAboutClick: () -> Unit,
 | 
			
		||||
    drawerActions: DrawerActions,
 | 
			
		||||
) {
 | 
			
		||||
    Column(
 | 
			
		||||
        modifier = Modifier.fillMaxWidth()
 | 
			
		||||
| 
						 | 
				
			
			@ -43,29 +46,29 @@ fun Drawer(
 | 
			
		|||
            DrawerEntry(
 | 
			
		||||
                icon = Icons.Default.Home,
 | 
			
		||||
                text = resources().getString(R.string.home),
 | 
			
		||||
                onClick = onHomeButtonClick,
 | 
			
		||||
                onClick = drawerActions.onHomeButtonClick,
 | 
			
		||||
            )
 | 
			
		||||
            DrawerEntry(
 | 
			
		||||
                icon = ImageVector.vectorResource(id = R.drawable.ic_timer),
 | 
			
		||||
                text = resources().getString(R.string.timers),
 | 
			
		||||
                onClick = onTimersClick,
 | 
			
		||||
                onClick = drawerActions.onTimersClick,
 | 
			
		||||
            )
 | 
			
		||||
            DrawerEntry(
 | 
			
		||||
                icon = Icons.Default.Settings,
 | 
			
		||||
                text = resources().getString(R.string.settings),
 | 
			
		||||
                onClick = onSettingsClick,
 | 
			
		||||
                onClick = drawerActions.onSettingsClick,
 | 
			
		||||
            )
 | 
			
		||||
            DrawerEntry(
 | 
			
		||||
                icon = ImageVector.vectorResource(id = R.drawable.ic_logout),
 | 
			
		||||
                text = resources().getString(R.string.log_out),
 | 
			
		||||
                onClick = onLogoutClick,
 | 
			
		||||
                onClick = drawerActions.onLogoutClick,
 | 
			
		||||
            )
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        DrawerEntry(
 | 
			
		||||
            icon = Icons.Outlined.Info,
 | 
			
		||||
            text = resources().getString(R.string.about),
 | 
			
		||||
            onClick = onAboutClick,
 | 
			
		||||
            onClick = drawerActions.onAboutClick,
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -100,7 +103,8 @@ fun DrawerEntry(
 | 
			
		|||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun DrawerPreview() {
 | 
			
		||||
    val drawerActions = DrawerActions({}, {}, {}, {}, {})
 | 
			
		||||
    StudeezTheme {
 | 
			
		||||
        Drawer({}, {}, {}, {}, {})
 | 
			
		||||
        Drawer(drawerActions)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -6,37 +6,68 @@ import androidx.compose.material.icons.Icons
 | 
			
		|||
import androidx.compose.material.icons.filled.Person
 | 
			
		||||
import androidx.compose.runtime.Composable
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
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.BasicButton
 | 
			
		||||
import be.ugent.sel.studeez.common.composable.PrimaryScreenTemplate
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun HomeRoute(
 | 
			
		||||
    open: (String) -> Unit,
 | 
			
		||||
    openAndPopUp: (String, String) -> Unit,
 | 
			
		||||
    viewModel: HomeViewModel,
 | 
			
		||||
) {
 | 
			
		||||
    HomeScreen(
 | 
			
		||||
        open = open,
 | 
			
		||||
        openAndPopUp = openAndPopUp,
 | 
			
		||||
        onStartSessionClick = { viewModel.onStartSessionClick(open) }
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun HomeScreen(
 | 
			
		||||
    open: (String) -> Unit,
 | 
			
		||||
    openAndPopUp: (String, String) -> Unit,
 | 
			
		||||
    viewModel: HomeViewModel = hiltViewModel()
 | 
			
		||||
    onStartSessionClick: () -> Unit,
 | 
			
		||||
) {
 | 
			
		||||
    val drawerViewModel: DrawerViewModel = hiltViewModel()
 | 
			
		||||
    val drawerActions = DrawerActions(
 | 
			
		||||
        onHomeButtonClick = { drawerViewModel.onHomeButtonClick(open) },
 | 
			
		||||
        onTimersClick = { drawerViewModel.onTimersClick(open) },
 | 
			
		||||
        onSettingsClick = { drawerViewModel.onSettingsClick(open) },
 | 
			
		||||
        onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) },
 | 
			
		||||
        onAboutClick = { drawerViewModel.onAboutClick(open) },
 | 
			
		||||
    )
 | 
			
		||||
    PrimaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(R.string.home),
 | 
			
		||||
        open = open,
 | 
			
		||||
        openAndPopUp = openAndPopUp,
 | 
			
		||||
        drawerActions = drawerActions,
 | 
			
		||||
        action = { FriendsAction() }
 | 
			
		||||
    ) {
 | 
			
		||||
        BasicButton(R.string.start_session, Modifier.basicButton()) {
 | 
			
		||||
            viewModel.onStartSessionClick(open)
 | 
			
		||||
            onStartSessionClick()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun FriendsAction () {
 | 
			
		||||
fun FriendsAction() {
 | 
			
		||||
    IconButton(onClick = { /*TODO*/ }) {
 | 
			
		||||
        Icon(
 | 
			
		||||
            imageVector = Icons.Default.Person,
 | 
			
		||||
            contentDescription = resources().getString(R.string.friends)
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun HomeScreenPreview() {
 | 
			
		||||
    HomeScreen(open = {}, openAndPopUp = { _, _ -> run {} }, onStartSessionClick = {})
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,6 +10,8 @@ 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.screens.drawer.DrawerActions
 | 
			
		||||
import be.ugent.sel.studeez.screens.drawer.DrawerViewModel
 | 
			
		||||
import be.ugent.sel.studeez.R.string as AppText
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
| 
						 | 
				
			
			@ -22,11 +24,19 @@ fun ProfileScreen(
 | 
			
		|||
    LaunchedEffect(key1 = Unit) {
 | 
			
		||||
        username = viewModel.getUsername()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    val drawerViewModel: DrawerViewModel = hiltViewModel()
 | 
			
		||||
    val drawerActions = DrawerActions(
 | 
			
		||||
        onHomeButtonClick = { drawerViewModel.onHomeButtonClick(open) },
 | 
			
		||||
        onTimersClick = { drawerViewModel.onTimersClick(open) },
 | 
			
		||||
        onSettingsClick = { drawerViewModel.onSettingsClick(open) },
 | 
			
		||||
        onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) },
 | 
			
		||||
        onAboutClick = { drawerViewModel.onAboutClick(open) },
 | 
			
		||||
    )
 | 
			
		||||
    PrimaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(AppText.profile),
 | 
			
		||||
        open = open,
 | 
			
		||||
        openAndPopUp = openAndPopUp,
 | 
			
		||||
        drawerActions = drawerActions,
 | 
			
		||||
        action = { EditAction { viewModel.onEditProfileClick(open) } }
 | 
			
		||||
    ) {
 | 
			
		||||
        Headline(text = (username ?: resources().getString(R.string.no_username)))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,6 +28,8 @@ 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.TimerInfo
 | 
			
		||||
import be.ugent.sel.studeez.resources
 | 
			
		||||
import be.ugent.sel.studeez.screens.drawer.DrawerActions
 | 
			
		||||
import be.ugent.sel.studeez.screens.drawer.DrawerViewModel
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun TimerOverviewScreen(
 | 
			
		||||
| 
						 | 
				
			
			@ -37,11 +39,19 @@ fun TimerOverviewScreen(
 | 
			
		|||
) {
 | 
			
		||||
 | 
			
		||||
    val timers = viewModel.getUserTimers().collectAsState(initial = emptyList())
 | 
			
		||||
 | 
			
		||||
    val drawerViewModel: DrawerViewModel = hiltViewModel()
 | 
			
		||||
    val drawerActions = DrawerActions(
 | 
			
		||||
        onHomeButtonClick = { drawerViewModel.onHomeButtonClick(open) },
 | 
			
		||||
        onTimersClick = { drawerViewModel.onTimersClick(open) },
 | 
			
		||||
        onSettingsClick = { drawerViewModel.onSettingsClick(open) },
 | 
			
		||||
        onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) },
 | 
			
		||||
        onAboutClick = { drawerViewModel.onAboutClick(open) },
 | 
			
		||||
    )
 | 
			
		||||
    PrimaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(R.string.timers),
 | 
			
		||||
        open = open,
 | 
			
		||||
        openAndPopUp = openAndPopUp
 | 
			
		||||
        openAndPopUp = openAndPopUp,
 | 
			
		||||
        drawerActions = drawerActions,
 | 
			
		||||
    ) {
 | 
			
		||||
 | 
			
		||||
        Column {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,6 +9,8 @@ import androidx.hilt.navigation.compose.hiltViewModel
 | 
			
		|||
import be.ugent.sel.studeez.R
 | 
			
		||||
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.timer_overview.TimerEntry
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
| 
						 | 
				
			
			@ -19,11 +21,19 @@ fun TimerSelectionScreen(
 | 
			
		|||
) {
 | 
			
		||||
 | 
			
		||||
    val timers = viewModel.getAllTimers().collectAsState(initial = emptyList())
 | 
			
		||||
 | 
			
		||||
    val drawerViewModel: DrawerViewModel = hiltViewModel()
 | 
			
		||||
    val drawerActions = DrawerActions(
 | 
			
		||||
        onHomeButtonClick = { drawerViewModel.onHomeButtonClick(open) },
 | 
			
		||||
        onTimersClick = { drawerViewModel.onTimersClick(open) },
 | 
			
		||||
        onSettingsClick = { drawerViewModel.onSettingsClick(open) },
 | 
			
		||||
        onLogoutClick = { drawerViewModel.onLogoutClick(openAndPopUp) },
 | 
			
		||||
        onAboutClick = { drawerViewModel.onAboutClick(open) },
 | 
			
		||||
    )
 | 
			
		||||
    PrimaryScreenTemplate(
 | 
			
		||||
        title = resources().getString(R.string.timers),
 | 
			
		||||
        open = open,
 | 
			
		||||
        openAndPopUp = openAndPopUp,
 | 
			
		||||
        drawerActions = drawerActions,
 | 
			
		||||
    ) {
 | 
			
		||||
 | 
			
		||||
        LazyColumn(verticalArrangement = Arrangement.spacedBy(7.dp)) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue