Skip to content

Commit

Permalink
Updated the navigation code
Browse files Browse the repository at this point in the history
  • Loading branch information
aritra-tech committed Aug 22, 2024
1 parent cf6e276 commit 618058d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 154 deletions.
11 changes: 0 additions & 11 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import androidx.compose.ui.graphics.vector.ImageVector
data class BottomNavItem(
val name: String,
val route: String,
val icon: ImageVector,
val selectedIcon: ImageVector,
val unselectedIcon: ImageVector
)
219 changes: 77 additions & 142 deletions app/src/main/java/com/aritra/notify/navigation/NotifyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,20 @@ import android.content.Intent
import android.widget.Toast
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lightbulb
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.outlined.Lightbulb
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.NavType.Companion.IntType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.navDeepLink
import com.aritra.notify.R
import androidx.navigation.*
import androidx.navigation.compose.*
import com.aritra.notify.ui.screens.notes.addEditScreen.route.AddEditRoute
import com.aritra.notify.ui.screens.notes.homeScreen.NoteScreen
import com.aritra.notify.ui.screens.notes.trash.TrashNoteEffect
Expand All @@ -45,41 +27,78 @@ import com.aritra.notify.ui.screens.settingsScreen.SettingsScreen

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun NotifyApp(navController: NavHostController = rememberNavController()) {
val screensWithHiddenNavBar = listOf(
"${NotifyScreens.AddEditNotes.name}/{noteId}",
NotifyScreens.TrashNoteScreen.name
)

val backStackEntry = navController.currentBackStackEntryAsState()
val trashViewModel = hiltViewModel<TrashNoteViewModel>()
fun NotifyApp(
navController: NavHostController = rememberNavController()
) {
val trashViewModel: TrashNoteViewModel = hiltViewModel()
val state by trashViewModel.state.collectAsState()
val effect by trashViewModel.effect.collectAsState()
val context = LocalContext.current

LaunchedEffect(effect) {
effect?.let {
when (it) {
TrashNoteEffect.Close -> {
navController.popBackStack()
trashViewModel.resetEffect()
}
val bottomNavItems = remember {
listOf(
BottomNavItem(
name = "Notes",
route = NotifyScreens.Notes.name,
selectedIcon = Icons.Filled.Lightbulb,
unselectedIcon = Icons.Outlined.Lightbulb
),
BottomNavItem(
name = "Settings",
route = NotifyScreens.Settings.name,
selectedIcon = Icons.Filled.Settings,
unselectedIcon = Icons.Outlined.Settings
)
)
}

is TrashNoteEffect.Message -> {
Toast.makeText(context, it.message, Toast.LENGTH_SHORT).show()
trashViewModel.closePage()
}
var selectedItemIndex by rememberSaveable { mutableIntStateOf(0) }

val routesWithoutBottomBar = remember {
setOf("${NotifyScreens.AddEditNotes.name}/{noteId}", NotifyScreens.TrashNoteScreen.name)
}

val currentRoute = navController.currentBackStackEntryAsState().value?.destination?.route

LaunchedEffect(effect) {
when (val currentEffect = effect) {
TrashNoteEffect.Close -> {
navController.popBackStack()
trashViewModel.resetEffect()
}
is TrashNoteEffect.Message -> {
Toast.makeText(context, currentEffect.message, Toast.LENGTH_SHORT).show()
trashViewModel.closePage()
}
null -> {}
}
}

Scaffold(
bottomBar = {
BottomNavigationBar(
backStackEntry,
screensWithHiddenNavBar,
navController
)
if (currentRoute !in routesWithoutBottomBar) {
NavigationBar {
bottomNavItems.forEachIndexed { index, item ->
NavigationBarItem(
selected = selectedItemIndex == index,
onClick = {
selectedItemIndex = index
navController.navigate(item.route) {
popUpTo(navController.graph.startDestinationId)
launchSingleTop = true
}
},
label = { Text(text = item.name) },
icon = {
Icon(
imageVector = if (index == selectedItemIndex) item.selectedIcon else item.unselectedIcon,
contentDescription = item.name
)
}
)
}
}
}
}
) { scaffoldPadding ->
SharedTransitionLayout {
Expand All @@ -88,11 +107,9 @@ fun NotifyApp(navController: NavHostController = rememberNavController()) {
startDestination = NotifyScreens.Notes.name,
modifier = Modifier.padding(scaffoldPadding)
) {
composable(
route = NotifyScreens.Notes.name
) {
composable(NotifyScreens.Notes.name) {
NoteScreen(
onFabClicked = { navController.navigate(NotifyScreens.AddEditNotes.name + "/-1") },
onFabClicked = { navController.navigate("${NotifyScreens.AddEditNotes.name}/-1") },
navigateToUpdateNoteScreen = { noteId ->
navController.navigate("${NotifyScreens.AddEditNotes.name}/$noteId")
},
Expand All @@ -102,11 +119,7 @@ fun NotifyApp(navController: NavHostController = rememberNavController()) {

composable(
route = "${NotifyScreens.AddEditNotes.name}/{noteId}",
arguments = listOf(
navArgument("noteId") {
type = IntType
}
),
arguments = listOf(navArgument("noteId") { type = NavType.IntType }),
deepLinks = listOf(
navDeepLink {
uriPattern = NavDeepLinks.addNotesUriPattern
Expand All @@ -121,15 +134,11 @@ fun NotifyApp(navController: NavHostController = rememberNavController()) {
)
}

composable(
route = NotifyScreens.Settings.name
) {
composable(NotifyScreens.Settings.name) {
SettingsScreen(controller = navController)
}

composable(
route = NotifyScreens.TrashNoteScreen.name
) {
composable(NotifyScreens.TrashNoteScreen.name) {
TrashNoteScreen(
trashNoteState = state,
onEvent = trashViewModel::onEvent,
Expand All @@ -139,78 +148,4 @@ fun NotifyApp(navController: NavHostController = rememberNavController()) {
}
}
}
}

@Composable
fun BottomNavigationBar(
backStackEntry: State<NavBackStackEntry?>,
screensWithHiddenNavBar: List<String>,
navController: NavHostController,
) {
if (backStackEntry.value?.destination?.route !in screensWithHiddenNavBar) {
NavigationBar(
containerColor = Color.Transparent,
modifier = Modifier.height(75.dp)
) {
val bottomNavItem = listOf(
BottomNavItem(
name = "Notes",
route = NotifyScreens.Notes.name,
icon = ImageVector.vectorResource(R.drawable.note_outline)
),
BottomNavItem(
name = "Settings",
route = NotifyScreens.Settings.name,
icon = ImageVector.vectorResource(R.drawable.settings_outline)
)
)

bottomNavItem.forEach { item ->
NavigationBarItem(
alwaysShowLabel = true,
icon = {
Icon(
imageVector = item.icon,
contentDescription = item.name,
tint = if (backStackEntry.value?.destination?.route == item.route) {
MaterialTheme.colorScheme.onSurface
} else {
MaterialTheme.colorScheme.secondary
}
)
},
label = {
Text(
text = item.name,
color = if (backStackEntry.value?.destination?.route == item.route) {
MaterialTheme.colorScheme.onSurface
} else {
MaterialTheme.colorScheme.secondary
},
fontWeight = if (backStackEntry.value?.destination?.route == item.route) {
FontWeight.SemiBold
} else {
FontWeight.Normal
}
)
},
selected = backStackEntry.value?.destination?.route == item.route,
onClick = {
val currentDestination = navController.currentBackStackEntry?.destination?.route
if (item.route != currentDestination) {
navController.navigate(item.route) {
navController.graph.findStartDestination().let { route ->
popUpTo(route.id) {
saveState = true
}
}
launchSingleTop = true
restoreState = true
}
}
}
)
}
}
}
}
}

0 comments on commit 618058d

Please sign in to comment.