Skip to content

Commit

Permalink
Fix shadowed extension functions of NavController pt2
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Apr 27, 2024
1 parent 6a595a1 commit b3e7064
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 258 deletions.
2 changes: 1 addition & 1 deletion compose-destinations/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach
}

dependencies {
implementation(libs.compose.navigation)
api(libs.compose.navigation)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,9 @@ package com.ramcosta.composedestinations.navigation
import androidx.annotation.MainThread
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.NavOptionsBuilder
import androidx.navigation.Navigator
import androidx.navigation.PopUpToBuilder
import com.ramcosta.composedestinations.spec.Direction
import com.ramcosta.composedestinations.spec.Route
import java.util.WeakHashMap

class DestinationsNavOptionsBuilder(
private val jetpackBuilder: NavOptionsBuilder
) {

var launchSingleTop
get() = jetpackBuilder.launchSingleTop
set(value) {
jetpackBuilder.launchSingleTop = value
}

var restoreState
get() = jetpackBuilder.restoreState
set(value) {
jetpackBuilder.restoreState = value
}

val popUpToRoute: String?
get() = jetpackBuilder.popUpToRoute

fun popUpTo(route: Route, popUpToBuilder: PopUpToBuilder.() -> Unit = {}) {
jetpackBuilder.popUpTo(route.route, popUpToBuilder)
}
}

private val navigators: WeakHashMap<NavController, DestinationsNavigator> = WeakHashMap()
val NavController.navigator: DestinationsNavigator
get(): DestinationsNavigator {
return navigators[this] ?: DestinationsNavController(this)
.also { navigators[this] = it }
}


/**
* Implementation of [DestinationsNavigator] that uses
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.ramcosta.composedestinations.navigation

import androidx.navigation.NavOptionsBuilder
import androidx.navigation.PopUpToBuilder
import com.ramcosta.composedestinations.spec.Route

/**
* Like [NavOptionsBuilder] but has Compose Destinations friendly
* version of its APIs.
*/
class DestinationsNavOptionsBuilder(
private val jetpackBuilder: NavOptionsBuilder
) {

/**
* @see [NavOptionsBuilder.launchSingleTop]
*/
var launchSingleTop
get() = jetpackBuilder.launchSingleTop
set(value) {
jetpackBuilder.launchSingleTop = value
}

/**
* @see [NavOptionsBuilder.restoreState]
*/
var restoreState
get() = jetpackBuilder.restoreState
set(value) {
jetpackBuilder.restoreState = value
}

/**
* @see [NavOptionsBuilder.popUpToRoute]
*/
val popUpToRoute: String?
get() = jetpackBuilder.popUpToRoute

/**
* Like [NavOptionsBuilder.popUpTo] but accepting a [com.ramcosta.composedestinations.spec.Route]
* or [com.ramcosta.composedestinations.spec.Direction] to pop up to.
*
* @see [NavOptionsBuilder.popUpTo]
*/
fun popUpTo(route: Route, popUpToBuilder: PopUpToBuilder.() -> Unit = {}) {
jetpackBuilder.popUpTo(route.route, popUpToBuilder)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package com.ramcosta.composedestinations.result
*/
class EmptyResultBackNavigator<R> : ResultBackNavigator<R> {

override fun navigateBack(result: R, onlyIfResumed: Boolean) = Unit
override fun navigateBack(result: R) = Unit

override fun setResult(result: R) = Unit

override fun navigateBack(onlyIfResumed: Boolean) = Unit
override fun navigateBack() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.ramcosta.composedestinations.result

import androidx.compose.runtime.*
import androidx.navigation.NavController
import com.ramcosta.composedestinations.dynamic.DynamicDestinationSpec
import com.ramcosta.composedestinations.spec.DestinationSpec

/**
* Navigator that allows navigating back while passing
Expand Down Expand Up @@ -32,15 +30,16 @@ interface ResultBackNavigator<R> {
*
* Check [com.ramcosta.composedestinations.result.ResultRecipient] to see
* how to get the result.
*/
fun navigateBack(result: R)

/**
* Goes back to previous destination sending the last result set with [setResult]
* or just navigating if no result was set..
*
* @param onlyIfResumed if true, will ignore the navigation action if the current `NavBackStackEntry`
* is not in the RESUMED state. This avoids duplicate navigation actions.
* By default is false to have the same behaviour as [NavController].
* It uses [NavController.navigateUp] internally to go back.
*/
fun navigateBack(
result: R,
onlyIfResumed: Boolean = false
)
fun navigateBack()

/**
* Sets a [result] to be sent on the next [navigateBack] call.
Expand All @@ -52,16 +51,4 @@ interface ResultBackNavigator<R> {
* This also applies if you call [navigateBack] (with result) after calling this.
*/
fun setResult(result: R)

/**
* Goes back to previous destination sending the last result set with [setResult]
* or just navigating if no result was set..
*
* It uses [NavController.navigateUp] internally to go back.
*
* @param onlyIfResumed if true, will ignore the navigation action if the current `NavBackStackEntry`
* is not in the RESUMED state. This avoids duplicate navigation actions.
* By default is false to have the same behaviour as [NavController].
*/
fun navigateBack(onlyIfResumed: Boolean = false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,19 @@ import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import com.ramcosta.composedestinations.spec.DestinationSpec

internal class ResultBackNavigatorImpl<R>(
private val navController: NavController,
private val navBackStackEntry: NavBackStackEntry,
resultOriginType: Class<out DestinationSpec<*>>,
resultType: Class<R>
) : ResultBackNavigator<R> {

private val resultKey = resultKey(resultOriginType, resultType)
private val canceledKey = canceledKey(resultOriginType, resultType)

private val isResumed: Boolean
get() = navBackStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED

override fun navigateBack(
result: R,
onlyIfResumed: Boolean
) {
if (onlyIfResumed && !isResumed) {
return
}

override fun navigateBack(result: R) {
setResult(result)
navigateBack()
}
Expand All @@ -43,11 +31,7 @@ internal class ResultBackNavigatorImpl<R>(
}
}

override fun navigateBack(onlyIfResumed: Boolean) {
if (onlyIfResumed && !isResumed) {
return
}

override fun navigateBack() {
navController.navigateUp()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import com.ramcosta.composedestinations.dynamic.originalDestination
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.spec.DestinationStyle

@Composable
@PublishedApi
internal fun <R> resultBackNavigator(
destination: DestinationSpec<*>,
resultType: Class<R>,
navController: NavController,
navBackStackEntry: NavBackStackEntry
): ResultBackNavigator<R> {

val backNavigator = remember {
ResultBackNavigatorImpl(
navController = navController,
navBackStackEntry = navBackStackEntry,
resultOriginType = destination.originalDestination.javaClass,
resultType = resultType
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface DestinationScope<T>: DestinationScopeWithNoDependencies<T> {
*/
@Composable
inline fun <reified R> DestinationScopeWithNoDependencies<*>.resultBackNavigator(): ResultBackNavigator<R> =
resultBackNavigator(destination, R::class.java, navController, navBackStackEntry)
resultBackNavigator(destination, R::class.java, navController)

/**
* Returns a well typed [ResultRecipient] for this [DestinationScope]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.ramcosta.composedestinations.navigation.DependenciesContainerBuilder
import com.ramcosta.composedestinations.navigation.DestinationDependenciesContainer
import com.ramcosta.composedestinations.navigation.DestinationDependenciesContainerImpl
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.navigator
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.utils.toDestinationsNavigator

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
abstract class DestinationScopeImpl<T> : DestinationScope<T> {
Expand All @@ -22,7 +22,7 @@ abstract class DestinationScopeImpl<T> : DestinationScope<T> {
}

override val destinationsNavigator: DestinationsNavigator
get() = navController.navigator
get() = navController.toDestinationsNavigator()

@Composable
override fun buildDependencies(): DestinationDependenciesContainer {
Expand All @@ -46,7 +46,7 @@ abstract class NavGraphBuilderDestinationScopeImpl<T> : NavGraphBuilderDestinati
}

override fun destinationsNavigator(navController: NavController): DestinationsNavigator {
return navController.navigator
return navController.toDestinationsNavigator()
}

internal class Default<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface NavGraphBuilderDestinationScope<T> {
inline fun <reified R> NavGraphBuilderDestinationScope<*>.resultBackNavigator(
navController: NavController
): ResultBackNavigator<R> =
resultBackNavigator(destination, R::class.java, navController, navBackStackEntry)
resultBackNavigator(destination, R::class.java, navController)


/**
Expand Down
Loading

0 comments on commit b3e7064

Please sign in to comment.