这是indexloc提供的服务,不要输入任何密码
Skip to content

Add extension function to retrieve a Flow of NavBackStackEntries #89

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions navigation/navigation-runtime-ktx/api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ package androidx.navigation {

public final class NavControllerKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
method public static kotlinx.coroutines.flow.Flow<androidx.navigation.NavBackStackEntry> getCurrentBackStackEntryFlow(androidx.navigation.NavController);
}

public final class NavHostKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ package androidx.navigation {

public final class NavControllerKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
method public static kotlinx.coroutines.flow.Flow<androidx.navigation.NavBackStackEntry> getCurrentBackStackEntryFlow(androidx.navigation.NavController);
}

public final class NavHostKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ package androidx.navigation {

public final class NavControllerKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
method public static kotlinx.coroutines.flow.Flow<androidx.navigation.NavBackStackEntry> getCurrentBackStackEntryFlow(androidx.navigation.NavController);
}

public final class NavHostKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.filters.SmallTest
import androidx.testutils.TestNavigator
import androidx.testutils.test
import com.google.common.truth.Truth.assertWithMessage
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.withIndex
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertTrue
import org.junit.Test

Expand All @@ -41,6 +47,31 @@ class NavControllerTest {
DESTINATION_ID in graph
)
}

@Test
@Suppress("EXPERIMENTAL_API_USAGE")
fun currentBackStackEntryFlow() = runBlocking {
navController.graph = navController.createGraph(startDestination = 1) {
test(1)
test(2)
test(3)
}

navController.currentBackStackEntryFlow
.take(navController.graph.count())
.withIndex()
.onEach { (index, backStackEntry) ->
val expectedDestination = index + 1
assertWithMessage("Flow emitted unexpected back stack entry (wrong destination)")
.that(backStackEntry.destination.id)
.isEqualTo(expectedDestination)

if (expectedDestination < navController.graph.count()) {
navController.navigate(expectedDestination + 1)
}
}
.collect()
}
}

private const val DESTINATION_ID = 1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
package androidx.navigation

import androidx.annotation.IdRes
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.sendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow

/**
* Construct a new [NavGraph]
Expand All @@ -26,3 +31,19 @@ public inline fun NavController.createGraph(
@IdRes startDestination: Int,
builder: NavGraphBuilder.() -> Unit
): NavGraph = navigatorProvider.navigation(id, startDestination, builder)

/**
* Creates and returns a [Flow] that will emit the currently active [NavBackStackEntry] whenever
* it changes. If there is no active [NavBackStackEntry], no item will be emitted.
*/
@ExperimentalCoroutinesApi
public val NavController.currentBackStackEntryFlow: Flow<NavBackStackEntry> get() = callbackFlow {
val listener = NavController.OnDestinationChangedListener { controller, _, _ ->
controller.currentBackStackEntry?.let(::sendBlocking)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better to use second argument instead? given that it is non null it will make things as easy as sendBlocking(destination)

another corner case question: sendBlocking can theoretically block on the main thread, when there is slow consumer and a lot of actions here. Though it seems like fairly theoretical concern

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be possible, but then it would be a Flow<NavDestination> instead of a Flow<NavBackStackEntry>, which is not what we want I guess (as NavBackStackEntry is the class that gives access to things like ViewModelStore and the SavedStateRegistry). But maybe it would make sense to have both, a backStackEntryFlow and a destinationFlow?

Valid point, I also wasn't sure what's best to use here. The other option would be to use offer, which could lead to lost items it the consumer is slow.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are specifically looking for the NavBackStackEntry here, not NavDestination.

}

addOnDestinationChangedListener(listener)
awaitClose {
removeOnDestinationChangedListener(listener)
}
}