Skip to content

Commit

Permalink
Updated wear dependencies & moved playground submodules & created Rou…
Browse files Browse the repository at this point in the history
…teOrDirection
  • Loading branch information
raamcosta committed Mar 2, 2024
1 parent 053a699 commit d7c13dd
Show file tree
Hide file tree
Showing 135 changed files with 167 additions and 194 deletions.
7 changes: 5 additions & 2 deletions .idea/compiler.xml

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

7 changes: 5 additions & 2 deletions .idea/deploymentTargetDropDown.xml

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

11 changes: 6 additions & 5 deletions .idea/gradle.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 @@ -24,7 +24,7 @@ internal class ModuleOutputWriter(
navGraphs: List<RawNavGraphGenParams>,
destinations: List<CodeGenProcessedDestination>
) {
val navGraphTrees = if (codeGenConfig.generateNavGraphs && navGraphs.isNotEmpty()) {
val navGraphTrees = if (codeGenConfig.generateNavGraphs && destinations.any { it.navGraphInfo != null }) {
val graphTrees = makeNavGraphTrees(navGraphs, destinations)
navGraphsSingleObjectWriter.write(graphTrees, destinations)
mermaidGraphWriter.write(submodules, graphTrees)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.ramcosta.composedestinations.codegen.commons.NAV_GRAPH_ANNOTATION_QUA
import com.ramcosta.composedestinations.codegen.commons.NAV_HOST_GRAPH_ANNOTATION
import com.ramcosta.composedestinations.codegen.commons.NAV_HOST_GRAPH_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.commons.NAV_HOST_PARAM_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.facades.Logger
import com.ramcosta.composedestinations.codegen.model.DeepLink
import com.ramcosta.composedestinations.codegen.model.Importable
import com.ramcosta.composedestinations.codegen.model.NavGraphInfo
Expand Down Expand Up @@ -194,7 +195,7 @@ fun KSType.getNavArgsDelegateType(
if (ksClassDeclaration.isNothing) {
return null
}

Logger.instance.warn("asdas ${ksClassDeclaration.qualifiedName!!.asString()}")
val parameters = ksClassDeclaration.primaryConstructor!!
.parameters
.map { it.toParameter(resolver, navTypeSerializersByType) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import com.ramcosta.composedestinations.spec.*
@Composable
fun rememberWearNavHostEngine(
state: SwipeDismissableNavHostState = rememberSwipeDismissableNavHostState(),
userSwipeEnabled: Boolean = true
): NavHostEngine {
val defaultNavHostEngine = rememberNavHostEngine()

return remember {
WearNavHostEngine(defaultNavHostEngine, state)
return remember(userSwipeEnabled, defaultNavHostEngine, state) {
WearNavHostEngine(userSwipeEnabled, defaultNavHostEngine, state)
}
}

internal class WearNavHostEngine(
private val userSwipeEnabled: Boolean,
private val defaultNavHostEngine: NavHostEngine,
private val state: SwipeDismissableNavHostState,
) : NavHostEngine {
Expand Down Expand Up @@ -56,6 +58,7 @@ internal class WearNavHostEngine(
modifier = modifier,
route = route,
state = state,
userSwipeEnabled = userSwipeEnabled,
builder = builder
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.navigation.Navigator
import com.ramcosta.composedestinations.spec.DestinationSpec
import com.ramcosta.composedestinations.spec.Direction
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.ramcosta.composedestinations.spec.Route
import com.ramcosta.composedestinations.spec.RouteOrDirection

/**
* Contract for a navigator of [DestinationSpec].
Expand Down Expand Up @@ -121,30 +121,13 @@ interface DestinationsNavigator {
*/
@MainThread
fun popBackStack(
route: Route,
route: RouteOrDirection,
inclusive: Boolean,
saveState: Boolean = false,
): Boolean {
return popBackStack(route.route, inclusive, saveState)
}

/**
* Takes in a [Direction].
* If there are multiple entries in the back stack for the same [DestinationSpec]
* or [NavGraphSpec], then specifying the arguments (which is what [Direction] allows)
* means a specific entry for the given arguments is the one targeted.
*
* @see [NavController.popBackStack]
*/
@MainThread
fun popBackStack(
direction: Direction,
inclusive: Boolean,
saveState: Boolean = false,
): Boolean {
return popBackStack(direction.route, inclusive, saveState)
}

/**
* @see [NavController.popBackStack]
*/
Expand All @@ -159,18 +142,7 @@ interface DestinationsNavigator {
* @see [NavController.clearBackStack]
*/
@MainThread
fun clearBackStack(route: Route): Boolean = clearBackStack(route.route)

/**
* Takes in a [Direction].
* If there are multiple entries in the back stack for the same [DestinationSpec]
* or [NavGraphSpec], then specifying the arguments (which is what [Direction] allows)
* means a specific entry for the given arguments is the one targeted.
*
* @see [NavController.clearBackStack]
*/
@MainThread
fun clearBackStack(direction: Direction): Boolean = clearBackStack(direction.route)
fun clearBackStack(route: RouteOrDirection): Boolean = clearBackStack(route.route)

/**
* @see [NavController.clearBackStack]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.navigation.NavOptionsBuilder
import androidx.navigation.PopUpToBuilder
import com.ramcosta.composedestinations.spec.Direction
import com.ramcosta.composedestinations.spec.Route
import com.ramcosta.composedestinations.spec.RouteOrDirection

/**
* Like [NavController.navigate], but uses [Direction] instead of a String route.
Expand All @@ -19,84 +20,37 @@ fun NavController.navigate(
}

/**
* Like [NavOptionsBuilder.popUpTo] but uses [Route] instead of a String route,
* Like [NavOptionsBuilder.popUpTo] but uses [Route] or [Direction] instead of a String route,
* it more "Compose Destinations friendly".
*/
fun NavOptionsBuilder.popUpTo(route: Route, popUpToBuilder: PopUpToBuilder.() -> Unit = {}) {
fun NavOptionsBuilder.popUpTo(route: RouteOrDirection, popUpToBuilder: PopUpToBuilder.() -> Unit = {}) {
popUpTo(route.route, popUpToBuilder)
}

/**
* Like [popUpTo] but takes in a [Direction].
*
* If there are multiple entries in the back stack for the same Destination
* or NavGraph, then specifying the arguments (which is what [Direction] allows)
* means a specific entry for the given arguments will be targeted.
*/
fun NavOptionsBuilder.popUpTo(direction: Direction, popUpToBuilder: PopUpToBuilder.() -> Unit = {}) {
popUpTo(direction.route, popUpToBuilder)
}

/**
* Like [NavController.popBackStack] but uses [Route] instead of a String route, making
* Like [NavController.popBackStack] but [Route] or [Direction] instead of a String route, making
* it more "Compose Destinations friendly".
*/
@MainThread
fun NavController.popBackStack(
route: Route,
route: RouteOrDirection,
inclusive: Boolean,
saveState: Boolean = false
): Boolean = popBackStack(route.route, inclusive, saveState)

/**
* Like [popBackStack] but takes in a [Direction].
*
* If there are multiple entries in the back stack for the same Destination
* or NavGraph, then specifying the arguments (which is what [Direction] allows)
* means a specific entry for the given arguments will be targeted.
*/
@MainThread
fun NavController.popBackStack(
direction: Direction,
inclusive: Boolean,
saveState: Boolean = false
): Boolean = popBackStack(direction.route, inclusive, saveState)

/**
* Like [NavController.clearBackStack] but uses [Route] instead of a String route, making
* Like [NavController.clearBackStack] but [Route] or [Direction] instead of a String route, making
* it more "Compose Destinations friendly".
*/
@MainThread
fun NavController.clearBackStack(route: Route): Boolean = clearBackStack(route.route)

/**
* Like [NavController.clearBackStack] but takes in a [Direction].
* If there are multiple entries in the back stack for the same Destination
* or NavGraph, then specifying the arguments (which is what [Direction] allows)
* means a specific entry for the given arguments will be targeted.
*/
@MainThread
fun NavController.clearBackStack(direction: Direction): Boolean = clearBackStack(direction.route)
fun NavController.clearBackStack(route: RouteOrDirection): Boolean = clearBackStack(route.route)

/**
* Like [androidx.navigation.NavController.getBackStackEntry] but uses a
* [Route] instead of a route string.
* [Route] or [Direction] instead of a route string.
*/
fun NavController.getBackStackEntry(
route: Route
route: RouteOrDirection
): NavBackStackEntry {
return getBackStackEntry(route.route)
}

/**
* Like [getBackStackEntry] but takes in a [Direction].
*
* If there are multiple entries in the back stack for the same Destination
* or NavGraph, then specifying the arguments (which is what [Direction] allows)
* means a specific entry for the given arguments will be targeted.
*/
fun NavController.getBackStackEntry(
direction: Direction
): NavBackStackEntry {
return getBackStackEntry(direction.route)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ package com.ramcosta.composedestinations.spec
* Interface for all classes that contain a route
* that is ready to be used in navigation.
*
* [NavGraphSpec] are [Direction]s since they can
* be navigated to and don't require arguments.
*
* Generated [TypedDestinationSpec] are [Direction] if they don't
* have any navigation argument. If they do, you can
* Generated [TypedDestinationSpec] and [TypedNavGraphSpec] are [Direction]
* if they don't have any navigation argument. If they do, you can
* call the invoke function passing the arguments to get a [Direction].
*/
interface Direction {
interface Direction: RouteOrDirection {

val route: String
/**
* Route with nav arguments filled in.
*/
override val route: String
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ typealias Route = TypedRoute<*>
* [TypedRoute] instances are not suitable to be navigated
* to unless they're also [Direction].
*/
sealed interface TypedRoute<NAV_ARGS> {
sealed interface TypedRoute<NAV_ARGS>: RouteOrDirection {

/**
* Full route that will be added to the navigation graph
* Full route pattern that will be added to the navigation graph.
* Navigation arguments are not filled in.
*/
val route: String
override val route: String

/**
* Prefix of the route - basically [route] without argument info.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ramcosta.composedestinations.spec

/**
* [Route] or [Direction]
*/
sealed interface RouteOrDirection {
val route: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.ramcosta.composedestinations.spec.Direction
import com.ramcosta.composedestinations.spec.NavGraphSpec
import com.ramcosta.composedestinations.spec.NavHostGraphSpec
import com.ramcosta.composedestinations.spec.Route
import com.ramcosta.composedestinations.spec.RouteOrDirection
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.transform
Expand Down Expand Up @@ -96,10 +97,10 @@ fun NavController.currentDestinationAsState(): State<DestinationSpec?> {
}

/**
* Checks if a given [Route] (which is either [com.ramcosta.composedestinations.spec.NavGraphSpec]
* Checks if a given [Route] (which is either [com.ramcosta.composedestinations.spec.NavGraphSpec] or [Direction]
* or [com.ramcosta.composedestinations.spec.DestinationSpec]) is currently somewhere in the back stack.
*/
fun NavController.isRouteOnBackStack(route: Route): Boolean {
fun NavController.isRouteOnBackStack(route: RouteOrDirection): Boolean {
return runCatching { getBackStackEntry(route) }.isSuccess
}

Expand All @@ -115,25 +116,13 @@ fun NavController.isDirectionOnBackStack(direction: Direction): Boolean {
* your Composables get recomposed when this changes.
*/
@Composable
fun NavController.isRouteOnBackStackAsState(route: Route): State<Boolean> {
fun NavController.isRouteOnBackStackAsState(route: RouteOrDirection): State<Boolean> {
val mappedFlow = remember(currentBackStackEntryFlow) {
currentBackStackEntryFlow.map { isRouteOnBackStack(route) }
}
return mappedFlow.collectAsState(initial = isRouteOnBackStack(route))
}

/**
* Same as [isDirectionOnBackStack] but provides a [State] which you can use to make sure
* your Composables get recomposed when this changes.
*/
@Composable
fun NavController.isDirectionOnBackStackAsState(direction: Direction): State<Boolean> {
val mappedFlow = remember(currentBackStackEntryFlow) {
currentBackStackEntryFlow.map { isDirectionOnBackStack(direction) }
}
return mappedFlow.collectAsState(initial = isDirectionOnBackStack(direction))
}

/**
* If this [Route] is a [DestinationSpec], returns it
*
Expand Down
3 changes: 2 additions & 1 deletion docs/FeatureYNavGraph.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ title: FeatureY Navigation Graph
graph TD
feature_y(["FeatureYGraph"]) -- "start" --- feature_y_home("FeatureYHome")
feature_y(["FeatureYGraph"]) --- feature_y_internal_args_screen("FeatureYInternalArgsScreen")
feature_y(["FeatureYGraph"]) --- sub_feature_y_public_side_screen_destination("SubFeatureYPublicSideScreen 🧩")
feature_y(["FeatureYGraph"]) --- sub_feature_y_nav_g(["SubFeatureYGraph 🧩"])
click sub_feature_y_nav_g "SubFeatureYNavGraph.mmd" "See SubFeatureYGraph details" _blank
classDef destination fill:#5383EC,stroke:#ffffff;
class feature_y_home,feature_y_internal_args_screen destination;
class feature_y_home,feature_y_internal_args_screen,sub_feature_y_public_side_screen_destination destination;
classDef navgraph fill:#63BC76,stroke:#ffffff;
class feature_y,sub_feature_y_nav_g navgraph;
Loading

0 comments on commit d7c13dd

Please sign in to comment.