Skip to content
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

Wrap ComponentContext with RouterContext #59

Merged
merged 1 commit into from
Sep 21, 2023
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 @@ -17,12 +17,12 @@ import androidx.wear.compose.material.SwipeToDismissBox
import androidx.wear.compose.material.SwipeToDismissKeys.Background
import androidx.wear.compose.material.rememberSwipeToDismissBoxState
import com.arkivanov.decompose.Child
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.router.stack.pop
import io.github.xxfast.decompose.LocalComponentContext
import io.github.xxfast.decompose.router.LocalRouter
import io.github.xxfast.decompose.router.LocalRouterContext
import io.github.xxfast.decompose.router.Router
import io.github.xxfast.decompose.router.RouterContext

@OptIn(ExperimentalWearFoundationApi::class)
@Composable
Expand All @@ -31,9 +31,9 @@ fun <C : Parcelable> RoutedContent(
modifier: Modifier = Modifier,
content: @Composable (C) -> Unit,
) {
val stack: ChildStack<C, ComponentContext> by router.stack
val active: Child.Created<C, ComponentContext> = stack.active
val background: Child.Created<C, ComponentContext>? = stack.backStack.lastOrNull()
val stack: ChildStack<C, RouterContext> by router.stack
val active: Child.Created<C, RouterContext> = stack.active
val background: Child.Created<C, RouterContext>? = stack.backStack.lastOrNull()
val holder: SaveableStateHolder = rememberSaveableStateHolder()
holder.RetainStates(stack.getConfigurations())

Expand All @@ -55,7 +55,7 @@ fun <C : Parcelable> RoutedContent(
) { isBackground ->
val child = if (isBackground) requireNotNull(background) else active
holder.SaveableStateProvider(child.configuration.key()) {
CompositionLocalProvider(LocalComponentContext provides child.instance) {
CompositionLocalProvider(LocalRouterContext provides child.instance) {
HierarchicalFocusCoordinator(requiresFocus = { !isBackground }) {
content(child.configuration)
}
Expand Down
23 changes: 19 additions & 4 deletions decompose-router/api/android/decompose-router.api
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
public final class io/github/xxfast/decompose/NavigatorKt {
public static final fun getLocalComponentContext ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public final class io/github/xxfast/decompose/router/Router : com/arkivanov/decompose/router/stack/StackNavigation {
public static final field $stable I
public fun <init> (Lcom/arkivanov/decompose/router/stack/StackNavigation;Landroidx/compose/runtime/State;)V
Expand All @@ -11,6 +7,25 @@ public final class io/github/xxfast/decompose/router/Router : com/arkivanov/deco
public fun unsubscribe (Lkotlin/jvm/functions/Function1;)V
}

public final class io/github/xxfast/decompose/router/RouterContext : com/arkivanov/decompose/ComponentContext {
public static final field $stable I
public fun <init> (Lcom/arkivanov/essenty/lifecycle/Lifecycle;Lcom/arkivanov/essenty/statekeeper/StateKeeper;Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Lcom/arkivanov/essenty/backhandler/BackHandler;)V
public synthetic fun <init> (Lcom/arkivanov/essenty/lifecycle/Lifecycle;Lcom/arkivanov/essenty/statekeeper/StateKeeper;Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Lcom/arkivanov/essenty/backhandler/BackHandler;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getBackHandler ()Lcom/arkivanov/essenty/backhandler/BackHandler;
public fun getInstanceKeeper ()Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;
public fun getLifecycle ()Lcom/arkivanov/essenty/lifecycle/Lifecycle;
public fun getStateKeeper ()Lcom/arkivanov/essenty/statekeeper/StateKeeper;
}

public final class io/github/xxfast/decompose/router/RouterContextExtKt {
public static final fun defaultRouterContext (Landroidx/activity/ComponentActivity;)Lio/github/xxfast/decompose/router/RouterContext;
public static final fun defaultRouterContext (Landroidx/fragment/app/Fragment;Landroidx/activity/OnBackPressedDispatcher;)Lio/github/xxfast/decompose/router/RouterContext;
}

public final class io/github/xxfast/decompose/router/RouterContextKt {
public static final fun getLocalRouterContext ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public final class io/github/xxfast/decompose/router/RouterKt {
public static final fun getLocalRouter ()Landroidx/compose/runtime/ProvidableCompositionLocal;
public static final fun rememberOnRoute (Lkotlin/reflect/KClass;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper$Instance;
Expand Down
18 changes: 14 additions & 4 deletions decompose-router/api/desktop/decompose-router.api
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
public final class io/github/xxfast/decompose/NavigatorKt {
public static final fun getLocalComponentContext ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public final class io/github/xxfast/decompose/router/Router : com/arkivanov/decompose/router/stack/StackNavigation {
public static final field $stable I
public fun <init> (Lcom/arkivanov/decompose/router/stack/StackNavigation;Landroidx/compose/runtime/State;)V
Expand All @@ -11,6 +7,20 @@ public final class io/github/xxfast/decompose/router/Router : com/arkivanov/deco
public fun unsubscribe (Lkotlin/jvm/functions/Function1;)V
}

public final class io/github/xxfast/decompose/router/RouterContext : com/arkivanov/decompose/ComponentContext {
public static final field $stable I
public fun <init> (Lcom/arkivanov/essenty/lifecycle/Lifecycle;Lcom/arkivanov/essenty/statekeeper/StateKeeper;Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Lcom/arkivanov/essenty/backhandler/BackHandler;)V
public synthetic fun <init> (Lcom/arkivanov/essenty/lifecycle/Lifecycle;Lcom/arkivanov/essenty/statekeeper/StateKeeper;Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;Lcom/arkivanov/essenty/backhandler/BackHandler;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getBackHandler ()Lcom/arkivanov/essenty/backhandler/BackHandler;
public fun getInstanceKeeper ()Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper;
public fun getLifecycle ()Lcom/arkivanov/essenty/lifecycle/Lifecycle;
public fun getStateKeeper ()Lcom/arkivanov/essenty/statekeeper/StateKeeper;
}

public final class io/github/xxfast/decompose/router/RouterContextKt {
public static final fun getLocalRouterContext ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}

public final class io/github/xxfast/decompose/router/RouterKt {
public static final fun getLocalRouter ()Landroidx/compose/runtime/ProvidableCompositionLocal;
public static final fun rememberOnRoute (Lkotlin/reflect/KClass;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper$Instance;
Expand Down
1 change: 1 addition & 0 deletions decompose-router/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ kotlin {
implementation(libs.decompose.compose.multiplatform)
implementation(libs.androidx.activity.ktx)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.fragment.ktx)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.CompositionLocalProvider
import androidx.core.view.WindowCompat
import com.arkivanov.decompose.DefaultComponentContext
import com.arkivanov.decompose.defaultComponentContext
import io.github.xxfast.decompose.router.LocalRouterContext
import io.github.xxfast.decompose.router.RouterContext
import io.github.xxfast.decompose.router.defaultRouterContext
import io.github.xxfast.decompose.screen.HomeScreen

class TestActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
val rootComponentContext: DefaultComponentContext = defaultComponentContext()
val rootRouterContext: RouterContext = defaultRouterContext()

setContent {
Surface {
CompositionLocalProvider(LocalComponentContext provides rootComponentContext) {
CompositionLocalProvider(LocalRouterContext provides rootRouterContext) {
MaterialTheme {
HomeScreen()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.arkivanov.decompose.extensions.compose.jetbrains.stack.animation.slid
import com.arkivanov.decompose.extensions.compose.jetbrains.stack.animation.stackAnimation
import com.arkivanov.decompose.router.stack.pop
import com.arkivanov.decompose.router.stack.push
import io.github.xxfast.decompose.LocalComponentContext
import io.github.xxfast.decompose.router.LocalRouterContext
import io.github.xxfast.decompose.router.Router
import io.github.xxfast.decompose.router.content.RoutedContent
import io.github.xxfast.decompose.router.rememberRouter
Expand All @@ -26,7 +26,7 @@ fun HomeScreen() {
animation = predictiveBackAnimation(
animation = stackAnimation(slide()),
onBack = { router.pop() },
backHandler = LocalComponentContext.current.backHandler
backHandler = LocalRouterContext.current.backHandler
)
) { screen ->
when (screen) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.xxfast.decompose.router

import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedDispatcher
import androidx.fragment.app.Fragment
import com.arkivanov.decompose.defaultComponentContext

fun ComponentActivity.defaultRouterContext(): RouterContext =
RouterContext(defaultComponentContext())

fun Fragment.defaultRouterContext(
onBackPressedDispatcher: OnBackPressedDispatcher?,
): RouterContext =
RouterContext(defaultComponentContext(onBackPressedDispatcher))

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import androidx.compose.runtime.DisallowComposableCalls
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.router.stack.StackNavigation
import com.arkivanov.decompose.router.stack.childStack
import com.arkivanov.decompose.value.Value
import com.arkivanov.decompose.value.observe
import com.arkivanov.essenty.instancekeeper.InstanceKeeper
import com.arkivanov.essenty.instancekeeper.InstanceKeeper.Instance
import com.arkivanov.essenty.instancekeeper.getOrCreate
import com.arkivanov.essenty.lifecycle.Lifecycle
import com.arkivanov.essenty.parcelable.Parcelable
import com.arkivanov.essenty.statekeeper.StateKeeper
import io.github.xxfast.decompose.LocalComponentContext
import io.github.xxfast.decompose.rememberChildStack
import kotlin.reflect.KClass

/***
Expand All @@ -28,7 +30,7 @@ import kotlin.reflect.KClass
*/
class Router<C : Parcelable>(
private val navigator: StackNavigation<C>,
val stack: State<ChildStack<C, ComponentContext>>,
val stack: State<ChildStack<C, RouterContext>>,
) : StackNavigation<C> by navigator

/***
Expand All @@ -51,16 +53,31 @@ fun <C : Parcelable> rememberRouter(
stack: List<C>,
handleBackButton: Boolean = true
): Router<C> {
val navigator: StackNavigation<C> = remember { StackNavigation() }
val childStackState: State<ChildStack<C, ComponentContext>> = rememberChildStack(
source = navigator,
initialStack = { stack },
key = key.toString(), // Has to use strings for Android 😢
handleBackButton = handleBackButton,
type = type,
)
val routerContext = LocalRouterContext.current
val keyStr = key.toString()

return remember { Router(navigator = navigator, stack = childStackState) }
return remember {
Copy link
Owner

Choose a reason for hiding this comment

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

@arkivanov Do you think we need to key this remember with our key

return remember(key) {
    routerContext.getOrCreate(key = keyStr) { .. }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you can use keyStr, the same as for getOrCreate

routerContext.getOrCreate(key = keyStr) {
val navigation = StackNavigation<C>()
Router(
navigator = navigation,
stack = routerContext.childStack(
source = navigation,
initialStack = { stack },
configurationClass = type,
key = keyStr,
handleBackButton = handleBackButton,
childFactory = { _, childComponentContext -> RouterContext(childComponentContext) },
).asState(routerContext.lifecycle),
xxfast marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
}

private fun <T : Any> Value<T>.asState(lifecycle: Lifecycle): State<T> {
val state = mutableStateOf(value)
observe(lifecycle = lifecycle) { state.value = it }
return state
}

/***
Expand All @@ -77,7 +94,7 @@ fun <T : Instance> rememberOnRoute(
key: Any = type.key,
block: @DisallowComposableCalls (savedState: SavedStateHandle) -> T
): T {
val component: ComponentContext = LocalComponentContext.current
val component: RouterContext = LocalRouterContext.current
val stateKeeper: StateKeeper = component.stateKeeper
val instanceKeeper: InstanceKeeper = component.instanceKeeper
val instanceKey = "$key.instance"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.xxfast.decompose.router

import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.staticCompositionLocalOf
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.DefaultComponentContext
import com.arkivanov.essenty.backhandler.BackHandler
import com.arkivanov.essenty.instancekeeper.InstanceKeeper
import com.arkivanov.essenty.lifecycle.Lifecycle
import com.arkivanov.essenty.statekeeper.StateKeeper

class RouterContext internal constructor(
private val delegate: ComponentContext,
) : ComponentContext by delegate {

constructor(
lifecycle: Lifecycle,
stateKeeper: StateKeeper? = null,
instanceKeeper: InstanceKeeper? = null,
backHandler: BackHandler? = null,
) : this(DefaultComponentContext(lifecycle, stateKeeper, instanceKeeper, backHandler))

internal val storage: MutableMap<Any, Any> = HashMap()
}

internal inline fun <reified T : Any> RouterContext.getOrCreate(key: Any, factory: () -> T) : T {
var instance: T? = storage[key] as T?
if (instance == null) {
instance = factory()
storage[key] = instance
}

return instance
}

/***
* Compositional local for [RouterContext].
*
* Based on [Arkadii](https://github.com/arkivanov)'s [article](https://proandroiddev.com/a-comprehensive-hundred-line-navigation-for-jetpack-desktop-compose-5b723c4f256e)
* Original [source](https://github.com/arkivanov/ComposeNavigatorExample/blob/d786d92632fe22e4d7874645ba2071fb813f9ace/navigator/src/commonMain/kotlin/com/arkivanov/composenavigatorexample/navigator/Navigator.kt)
*/
val LocalRouterContext: ProvidableCompositionLocal<RouterContext> =
staticCompositionLocalOf { error("Root RouterContext was not provided") }
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package io.github.xxfast.decompose.router.content
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.extensions.compose.jetbrains.stack.Children
import com.arkivanov.decompose.extensions.compose.jetbrains.stack.animation.StackAnimation
import com.arkivanov.essenty.parcelable.Parcelable
import io.github.xxfast.decompose.LocalComponentContext
import io.github.xxfast.decompose.router.LocalRouter
import io.github.xxfast.decompose.router.LocalRouterContext
import io.github.xxfast.decompose.router.Router
import io.github.xxfast.decompose.router.RouterContext

/***
* Composable to hoist content that are navigated by the router
Expand All @@ -23,7 +23,7 @@ import io.github.xxfast.decompose.router.Router
fun <C : Parcelable> RoutedContent(
router: Router<C>,
modifier: Modifier = Modifier,
animation: StackAnimation<C, ComponentContext>? = null,
animation: StackAnimation<C, RouterContext>? = null,
content: @Composable (C) -> Unit,
) {
CompositionLocalProvider(LocalRouter provides router) {
Expand All @@ -32,7 +32,7 @@ fun <C : Parcelable> RoutedContent(
modifier = modifier,
animation = animation,
) { child ->
CompositionLocalProvider(LocalComponentContext provides child.instance) {
CompositionLocalProvider(LocalRouterContext provides child.instance) {
content(child.configuration)
}
}
Expand Down
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[versions]
agp = "8.1.1"
androidx-activity ="1.7.2"
androidx-fragment ="1.6.1"
compose-multiplatform = "1.5.0-beta02"
compose-test-rule = "1.5.0"
decompose = "2.1.0-compose-experimental-alpha-07"
essenty = "1.2.0-alpha-06"
decompose = "2.1.0-compose-experimental"
essenty = "1.2.0"
horologist = "0.4.12"
kotlin = "1.9.0"
wear-compose = "1.3.0-alpha03"
Expand All @@ -13,6 +14,7 @@ wear-compose = "1.3.0-alpha03"
agp = { module = "com.android.tools.build:gradle", version.ref = "agp" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity" }
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version = "androidx-activity" }
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version = "androidx-fragment" }
compose-multiplatform = { module = "org.jetbrains.compose:compose-gradle-plugin", version.ref = "compose-multiplatform" }
compose-ui-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose-test-rule" }
compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose-test-rule" }
Expand Down