mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 18:47:03 +05:30
refactor: move navigation rail to nav3
This commit is contained in:
parent
17289a26f1
commit
74ccf74f8d
3 changed files with 6 additions and 44 deletions
|
@ -19,16 +19,15 @@ import androidx.compose.material3.NavigationRail
|
|||
import androidx.compose.material3.NavigationRailItem
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import dev.msfjarvis.claw.android.ui.navigation.matches
|
||||
import dev.msfjarvis.claw.android.ui.navigation.Destination
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
|
||||
@Composable
|
||||
fun ClawNavigationRail(
|
||||
navController: NavController,
|
||||
backStack: SnapshotStateList<Destination>,
|
||||
items: ImmutableList<NavigationItem>,
|
||||
isVisible: Boolean,
|
||||
modifier: Modifier = Modifier,
|
||||
|
@ -50,11 +49,10 @@ fun ClawNavigationRail(
|
|||
modifier = Modifier,
|
||||
) {
|
||||
NavigationRail(modifier = modifier) {
|
||||
val navBackStackEntry = navController.currentBackStackEntryAsState().value
|
||||
val currentDestination = navBackStackEntry?.destination
|
||||
val currentDestination = backStack.firstOrNull()
|
||||
Spacer(Modifier.weight(1f))
|
||||
items.forEach { navItem ->
|
||||
val isSelected = currentDestination.matches(navItem.destination)
|
||||
val isSelected = currentDestination == navItem.destination
|
||||
NavigationRailItem(
|
||||
icon = {
|
||||
Crossfade(isSelected, label = "nav-label") {
|
||||
|
@ -70,11 +68,7 @@ fun ClawNavigationRail(
|
|||
if (isSelected) {
|
||||
navItem.listStateResetCallback()
|
||||
} else {
|
||||
navController.navigate(navItem.destination) {
|
||||
popUpTo(navController.graph.startDestinationId) { saveState = true }
|
||||
launchSingleTop = true
|
||||
restoreState = true
|
||||
}
|
||||
backStack.add(navItem.destination)
|
||||
}
|
||||
},
|
||||
modifier = Modifier.testTag(navItem.label.uppercase()),
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
/*
|
||||
* Copyright © Harsh Shandilya.
|
||||
* Use of this source code is governed by an MIT-style
|
||||
* license that can be found in the LICENSE file or at
|
||||
* https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
package dev.msfjarvis.claw.android.ui.navigation
|
||||
|
||||
import androidx.navigation.NavDestination
|
||||
import androidx.navigation.NavDestination.Companion.hasRoute
|
||||
import androidx.navigation.NavDestination.Companion.hierarchy
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
|
||||
/**
|
||||
* Walk through the [NavDestination]'s [hierarchy] to see if it has any destination that matches the
|
||||
* route defined by [dest].
|
||||
*/
|
||||
fun NavDestination?.matches(dest: Destination): Boolean {
|
||||
return this?.hierarchy?.any { it.hasRoute(dest::class) } == true
|
||||
}
|
||||
|
||||
/** Check if this [NavDestination] [matches] any of the potential navigation [destinations]. */
|
||||
fun NavDestination?.any(destinations: ImmutableList<Destination>): Boolean {
|
||||
return destinations.any { this?.matches(it) == true }
|
||||
}
|
||||
|
||||
/** Check if this [NavDestination] [matches] none of the potential navigation [destinations]. */
|
||||
fun NavDestination?.none(destinations: ImmutableList<Destination>): Boolean {
|
||||
return destinations.none { this?.matches(it) == true }
|
||||
}
|
|
@ -65,8 +65,6 @@ import dev.msfjarvis.claw.android.ui.navigation.Saved
|
|||
import dev.msfjarvis.claw.android.ui.navigation.Search
|
||||
import dev.msfjarvis.claw.android.ui.navigation.Settings
|
||||
import dev.msfjarvis.claw.android.ui.navigation.User
|
||||
import dev.msfjarvis.claw.android.ui.navigation.any
|
||||
import dev.msfjarvis.claw.android.ui.navigation.none
|
||||
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
|
||||
import dev.msfjarvis.claw.common.comments.CommentsPage
|
||||
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue