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

Add Route extensions and delegates #35

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

Merged
merged 4 commits into from
May 26, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.JavaVersion
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.VersionCatalogsExtension

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import org.gradle.api.Action
import org.gradle.api.JavaVersion
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.configure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* limitations under the License.
*/

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension

/*
* Copyright 2021 Google LLC
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ fun <R : Node> threePaneEntry(
}
Box(
modifier =
if (shouldAnimate) Modifier.animateEnterExit(
enter = enterTransition(),
exit = exitTransition()
)
else Modifier,
if (shouldAnimate) Modifier.animateEnterExit(
enter = enterTransition(),
exit = exitTransition()
)
else Modifier,
content = {
original(destination)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.tunjid.treenav.Node
import com.tunjid.treenav.compose.threepane.ThreePane
import com.tunjid.treenav.compose.transforms.PaneTransform
import com.tunjid.treenav.compose.transforms.Transform
import com.tunjid.treenav.compose.threepane.ThreePane

/**
* An [Transform] that selectively displays panes for a [ThreePane] layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ internal fun <Destination : Node, NavigationState : Node, Pane> DecoratedNavEntr
val navigationState by state.navigationState
val backStack = remember { mutableStateListOf<Destination>() }.also { mutableBackStack ->
state.backStackTransform(navigationState).let { currentBackStack ->
val sameBackStack = currentBackStack == mutableBackStack
if (sameBackStack) return@let

mutableBackStack.clear()
mutableBackStack.addAll(currentBackStack)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import androidx.compose.ui.unit.LayoutDirection
@Stable
internal object Defaults {

val EmptyElement: @Composable (Any?, Modifier) -> Unit = { _, _ -> }
val EmptyElement: @Composable (Any?, Modifier) -> Unit = { _, _ -> }

@ExperimentalSharedTransitionApi
val DefaultBoundsTransform = BoundsTransform { _, _ -> DefaultSpring }
val DefaultBoundsTransform = BoundsTransform { _, _ -> DefaultSpring }

@ExperimentalSharedTransitionApi
val ParentClip: OverlayClip =
val ParentClip: OverlayClip =
object : OverlayClip {
override fun getClipPath(
sharedContentState: SharedContentState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.tunjid.treenav.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import com.tunjid.treenav.Node
import com.tunjid.treenav.compose.transforms.CompoundTransform
import com.tunjid.treenav.compose.transforms.DestinationTransform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import com.tunjid.treenav.compose.transforms.RenderTransform
@Stable
class PaneEntry<Pane, Destination : Node>(
internal val renderTransform: RenderTransform<Pane, Destination>,
internal val paneTransform: @Composable (Destination) -> Map<Pane, Destination?>,
internal val paneTransform: @Composable (Destination) -> Map<Pane, Destination?>,
internal val content: @Composable PaneScope<Pane, Destination>.(Destination) -> Unit,
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.tunjid.treenav.backStack
* A convenience method for reading the back stack for this [MultiStackNav]
* optimized for consumption for a [MultiPaneDisplay].
*/
inline fun <reified Destination: Node> MultiStackNav.multiPaneDisplayBackstack(): List<Destination> =
inline fun <reified Destination : Node> MultiStackNav.multiPaneDisplayBackstack(): List<Destination> =
backStack(
includeCurrentDestinationChildren = true,
placeChildrenBeforeParent = true,
Expand All @@ -37,7 +37,7 @@ inline fun <reified Destination: Node> MultiStackNav.multiPaneDisplayBackstack()
* A convenience method for reading the back stack for this [MultiStackNav]
* optimized for consumption for a [MultiPaneDisplay].
*/
inline fun <reified Destination: Node> StackNav.multiPaneDisplayBackstack(): List<Destination> =
inline fun <reified Destination : Node> StackNav.multiPaneDisplayBackstack(): List<Destination> =
backStack(
includeCurrentDestinationChildren = true,
placeChildrenBeforeParent = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,14 @@ private fun Modifier.fillMaxConstraints() =
layout { measurable, constraints ->
val placeable = measurable.measure(
constraints.copy(
minWidth = constraints.maxWidth,
maxHeight = constraints.maxHeight
minWidth = when {
constraints.hasBoundedWidth -> constraints.maxWidth
else -> constraints.minWidth
},
minHeight = when {
constraints.hasBoundedHeight -> constraints.maxHeight
else -> constraints.minHeight
}
)
)
layout(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

package com.tunjid.treenav.compose.navigation3

import androidx.compose.runtime.Composable

/**
Expand All @@ -26,7 +27,7 @@ import androidx.compose.runtime.Composable
* @param content content for this entry to be displayed when this entry is active
*/
internal open class NavEntry<T : Any>(
public open val key: T,
public open val metadata: Map<String, Any> = emptyMap(),
public open val content: @Composable (T) -> Unit
open val key: T,
open val metadata: Map<String, Any> = emptyMap(),
open val content: @Composable (T) -> Unit
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.compose.runtime.Composable
*
* @param navEntry the [NavEntry] to wrap
*/
internal open class NavEntryWrapper<T : Any>(public val navEntry: NavEntry<T>) :
internal open class NavEntryWrapper<T : Any>(val navEntry: NavEntry<T>) :
NavEntry<T>(navEntry.key, navEntry.metadata, navEntry.content) {
override val key: T
get() = navEntry.key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import androidx.savedstate.compose.LocalSavedStateRegistryOwner
import com.tunjid.treenav.compose.navigation3.NavEntryDecorator
import com.tunjid.treenav.compose.navigation3.navEntryDecorator

@Composable internal expect fun shouldRemoveViewModelStoreCallback(): () -> Boolean
@Composable
internal expect fun shouldRemoveViewModelStoreCallback(): () -> Boolean

internal expect fun SavedStateViewModelFactory(
savedStateRegistryOwner: SavedStateRegistryOwner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.tunjid.treenav.compose.transforms

import androidx.compose.runtime.Composable
import com.tunjid.treenav.Node
import com.tunjid.treenav.compose.PaneScope
import com.tunjid.treenav.compose.MultiPaneDisplay
import com.tunjid.treenav.compose.MultiPaneDisplayState
import com.tunjid.treenav.compose.PaneScope

/**
* Provides APIs for adjusting what is presented in a [MultiPaneDisplay].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package com.tunjid.treenav.compose
import com.tunjid.treenav.Node
import com.tunjid.treenav.StackNav
import com.tunjid.treenav.backStack
import com.tunjid.treenav.requireCurrent
import com.tunjid.treenav.pop
import com.tunjid.treenav.push
import com.tunjid.treenav.requireCurrent
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@
package com.tunjid.treenav.compose

import com.tunjid.treenav.MultiStackNav
import com.tunjid.treenav.Node
import com.tunjid.treenav.StackNav
import com.tunjid.treenav.backStack
import com.tunjid.treenav.requireCurrent
import com.tunjid.treenav.pop
import com.tunjid.treenav.push
import com.tunjid.treenav.reversedBackStackSequence
import com.tunjid.treenav.switch
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue


class StackNavExtTest {
Expand All @@ -47,11 +40,13 @@ class StackNavExtTest {
.push(TestNode("E", children = listOf(TestNode("1"), TestNode("2"))))
.push(TestNode("F"))

println(pushed.backStack(
includeCurrentDestinationChildren = true,
placeChildrenBeforeParent = true,
distinctDestinations = true,
))
println(
pushed.backStack(
includeCurrentDestinationChildren = true,
placeChildrenBeforeParent = true,
distinctDestinations = true,
)
)
assertEquals(
expected = pushed.backStack(
includeCurrentDestinationChildren = true,
Expand Down
3 changes: 1 addition & 2 deletions library/compose/src/main/res/values/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
~ limitations under the License.
-->

<manifest>
</manifest>
<manifest></manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,64 @@ fun routeString(
}
}

/**
* Creates a [Route] containing the specified [params] and [children].
*/
fun routeOf(
params: RouteParams,
children: List<Node> = emptyList(),
): Route = BasicRoute(
routeParams = params,
children = children
)

/**
* Creates a [Route] by combining the [path], [pathArgs] and [queryParams]
* into [RouteParams] with the specified [children].
*/
@Suppress("unused")
fun routeOf(
path: String,
pathArgs: Map<String, String> = emptyMap(),
queryParams: Map<String, List<String>> = emptyMap(),
children: List<Node> = listOf(),
): Route = BasicRoute(
path = path,
pathArgs = pathArgs,
queryParams = queryParams,
children = children
)

fun interface RouteParser {
fun parse(pathAndQueries: String): Route?
/**
* Basic route definition
*/
private class BasicRoute(
override val routeParams: RouteParams,
override val children: List<Node> = emptyList(),
) : Route {

constructor(
path: String,
pathArgs: Map<String, String> = emptyMap(),
queryParams: Map<String, List<String>> = emptyMap(),
children: List<Node> = emptyList(),
) : this(
routeParams = RouteParams(
pathAndQueries = path,
pathArgs = pathArgs,
queryParams = queryParams,
),
children = children
)

override fun toString(): String = id
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Route) return false

return id == other.id
}

override fun hashCode(): Int = id.hashCode()
}

Loading