From 22cb11861b1c9b88faced7514e3439712b4ff000 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Fri, 16 Sep 2022 04:42:55 +0200 Subject: [PATCH] Add: BottomNavBar --- .../kotlin/com/machiav3lli/fdroid/Common.kt | 4 + .../fdroid/ui/navigation/BottomNavBar.kt | 109 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/main/kotlin/com/machiav3lli/fdroid/ui/navigation/BottomNavBar.kt diff --git a/src/main/kotlin/com/machiav3lli/fdroid/Common.kt b/src/main/kotlin/com/machiav3lli/fdroid/Common.kt index 657f91ab..9c716394 100644 --- a/src/main/kotlin/com/machiav3lli/fdroid/Common.kt +++ b/src/main/kotlin/com/machiav3lli/fdroid/Common.kt @@ -63,6 +63,10 @@ const val PREFS_LANGUAGE_DEFAULT = "system" const val EXTRA_REPOSITORY_ID = "repositoryId" + +const val NAV_MAIN = 0 +const val NAV_PREFS = 1 + interface RepoManager { fun onDeleteConfirm() } diff --git a/src/main/kotlin/com/machiav3lli/fdroid/ui/navigation/BottomNavBar.kt b/src/main/kotlin/com/machiav3lli/fdroid/ui/navigation/BottomNavBar.kt new file mode 100644 index 00000000..ad4f22be --- /dev/null +++ b/src/main/kotlin/com/machiav3lli/fdroid/ui/navigation/BottomNavBar.kt @@ -0,0 +1,109 @@ +/* + * Neo Store: An open-source modern F-Droid client. + * Copyright (C) 2022 Antonios Hazim + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.machiav3lli.fdroid.ui.navigation + +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.size +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.NavigationBar +import androidx.compose.material3.NavigationBarItem +import androidx.compose.material3.NavigationBarItemDefaults +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.navigation.NavController +import androidx.navigation.compose.currentBackStackEntryAsState +import com.machiav3lli.fdroid.NAV_MAIN +import com.machiav3lli.fdroid.NAV_PREFS + +@Composable +fun BottomNavBar(page: Int = NAV_MAIN, navController: NavController) { + val items = when (page) { + NAV_PREFS -> listOf( + NavItem.PersonalPrefs, + NavItem.UpdatesPrefs, + NavItem.ReposPrefs, + NavItem.OtherPrefs, + ) + else -> listOf( + NavItem.Explore, + NavItem.Latest, + NavItem.Installed, + ) + } + + NavigationBar( + modifier = Modifier.fillMaxWidth(), + containerColor = MaterialTheme.colorScheme.surface, + tonalElevation = 0.dp, + contentColor = MaterialTheme.colorScheme.onSurface + ) { + val navBackStackEntry by navController.currentBackStackEntryAsState() + val currentDestination = navBackStackEntry?.destination?.route + + items.forEach { item -> + val selected = currentDestination == item.destination + + NavigationBarItem( + icon = { + Icon( + painter = painterResource(id = item.icon), + contentDescription = stringResource(id = item.title), + modifier = Modifier.size(if (selected) 46.dp else 32.dp), + ) + }, + label = { + if (!selected) + Text( + text = stringResource(id = item.title), + style = MaterialTheme.typography.bodyMedium, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + }, + colors = NavigationBarItemDefaults.colors( + indicatorColor = MaterialTheme.colorScheme.surface, + selectedIconColor = MaterialTheme.colorScheme.primary, + selectedTextColor = MaterialTheme.colorScheme.primary, + unselectedIconColor = MaterialTheme.colorScheme.onSurface, + unselectedTextColor = MaterialTheme.colorScheme.onSurface, + ), + alwaysShowLabel = true, + selected = selected, + onClick = { + navController.navigate(item.destination) { + navController.currentDestination?.id?.let { + popUpTo(it) { + inclusive = true + saveState = true + } + } + launchSingleTop = true + restoreState = true + } + } + ) + } + } +} \ No newline at end of file