8000 Add `previousBackStackEntryFlow` in `NavController` and `NavController.previousBackStackEntryAsState` by ShreckYe · Pull Request #740 · androidx/androidx · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add previousBackStackEntryFlow in NavController and NavController.previousBackStackEntryAsState #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: androidx-main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

10000 Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavGraph
import androidx.navigation.NavHostController
import androidx.navigation.Navigator

Expand All @@ -42,6 +43,21 @@ public fun NavController.currentBackStackEntryAsState(): State<NavBackStackEntry
return currentBackStackEntryFlow.collectAsState(null)
}

/**
* Gets the previous visible navigation back stack entry as a [MutableState]. When the given
* navController changes the back stack due to a [NavController.navigate] or
* [NavController.popBackStack] this will trigger a recompose and return the previous visible entry
* on the back stack.
*
* This skips over any [NavBackStackEntry] that is associated with a [NavGraph].
*
* @return a mutable state of the previous visible back stack entry or null if the back stack has
* less than two visible entries
*/
@Composable
public fun NavController.previousBackStackEntryAsState(): State<NavBackStackEntry?> =
previousBackStackEntryFlow.collectAsState(null)

/**
* Creates a NavHostController that handles the adding of the [ComposeNavigator] and
* [DialogNavigator]. Additional [Navigator] instances can be passed through [navigators] to be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.serializer

Expand Down Expand Up @@ -2954,6 +2955,15 @@ public open class NavController(
public val currentBackStackEntryFlow: Flow<NavBackStackEntry> =
_currentBackStackEntryFlow.asSharedFlow()

private fun List<NavBackStackEntry>.getPreviousBackStackEntry(): NavBackStackEntry? {
val iterator = reversed().iterator()
// throw the topmost destination away.
if (iterator.hasNext()) {
iterator.next()
}
return iterator.asSequence().firstOrNull { entry -> entry.destination !is NavGraph }
}

/**
* The previous visible [NavBackStackEntry].
*
Expand All @@ -2963,14 +2973,17 @@ public open class NavController(
* two visible entries
*/
public open val previousBackStackEntry: NavBackStackEntry?
get() {
val iterator = backQueue.reversed().iterator()
// throw the topmost destination away.
if (iterator.hasNext()) {
iterator.next()
}
return iterator.asSequence().firstOrNull { entry -> entry.destination !is NavGraph }
}
get() = backQueue.getPreviousBackStackEntry()

/**
* A [Flow] that will emit the previous visible [NavBackStackEntry] whenever it changes.
*
* This skips over any [NavBackStackEntry] that is associated with a [NavGraph].
*
* If the back stack has less than two visible entries, no item will be emitted.
*/
public val previousBackStackEntryFlow: Flow<NavBackStackEntry?> =
currentBackStack.map { it.getPreviousBackStackEntry() }

public companion object {
private const val TAG = "NavController"
Expand Down
0