Skip to content

Commit d0d37e2

Browse files
committed
Refactor router api to mirror decompose api
1 parent 8ea08e9 commit d0d37e2

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

decompose-router/api/android/decompose-router.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class io/github/xxfast/decompose/router/RouterContextKt {
2929
public final class io/github/xxfast/decompose/router/RouterKt {
3030
public static final fun getLocalRouter ()Landroidx/compose/runtime/ProvidableCompositionLocal;
3131
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;
32-
public static final fun rememberRouter (Lkotlin/reflect/KClass;Ljava/lang/Object;Ljava/util/List;ZLandroidx/compose/runtime/Composer;II)Lio/github/xxfast/decompose/router/Router;
32+
public static final fun rememberRouter (Lkotlin/reflect/KClass;Ljava/lang/Object;ZLkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;II)Lio/github/xxfast/decompose/router/Router;
3333
}
3434

3535
public final class io/github/xxfast/decompose/router/SavedState : android/os/Parcelable {

decompose-router/api/desktop/decompose-router.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class io/github/xxfast/decompose/router/RouterContextKt {
2424
public final class io/github/xxfast/decompose/router/RouterKt {
2525
public static final fun getLocalRouter ()Landroidx/compose/runtime/ProvidableCompositionLocal;
2626
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;
27-
public static final fun rememberRouter (Lkotlin/reflect/KClass;Ljava/lang/Object;Ljava/util/List;ZLandroidx/compose/runtime/Composer;II)Lio/github/xxfast/decompose/router/Router;
27+
public static final fun rememberRouter (Lkotlin/reflect/KClass;Ljava/lang/Object;ZLkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;II)Lio/github/xxfast/decompose/router/Router;
2828
}
2929

3030
public final class io/github/xxfast/decompose/router/SavedState : com/arkivanov/essenty/parcelable/Parcelable {

decompose-router/src/androidInstrumentedTest/kotlin/io/github/xxfast/decompose/screen/HomeScreen.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import io.github.xxfast.decompose.screen.nested.NestedScreen
1818
@OptIn(ExperimentalDecomposeApi::class)
1919
@Composable
2020
fun HomeScreen() {
21-
val router: Router<HomeScreens> =
22-
rememberRouter(HomeScreens::class, stack = listOf(HomeScreens.List))
21+
val router: Router<HomeScreens> = rememberRouter(HomeScreens::class) { listOf(HomeScreens.List) }
2322

2423
RoutedContent(
2524
router = router,

decompose-router/src/androidInstrumentedTest/kotlin/io/github/xxfast/decompose/screen/nested/NestedScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fun NestedScreen(
5555
onBack: () -> Unit,
5656
onSelect: (Int) -> Unit
5757
) {
58-
val router: Router<NestedScreens> = rememberRouter(NestedScreens::class, stack = listOf(Home))
58+
val router: Router<NestedScreens> = rememberRouter(NestedScreens::class) { listOf(Home) }
5959

6060
val items: List<Int> = buildList { repeat(50) { add(it) } }
6161

decompose-router/src/commonMain/kotlin/io/github/xxfast/decompose/router/Router.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import kotlin.reflect.KClass
2525
* Router with a given navigator and a stack
2626
* Detailed breakdown of this available [here](https://proandroiddev.com/diy-compose-multiplatform-navigation-with-decompose-94ac8126e6b5)
2727
*
28-
* @param navigator decompose navigator to use
28+
* @param navigation decompose navigator to use
2929
* @param stack state of decompose child stack to use
3030
*/
3131
class Router<C : Parcelable>(
32-
private val navigator: StackNavigation<C>,
32+
private val navigation: StackNavigation<C>,
3333
val stack: State<ChildStack<C, RouterContext>>,
34-
) : StackNavigation<C> by navigator
34+
) : StackNavigation<C> by navigation
3535

3636
/***
3737
* Compositional local for component context
@@ -43,33 +43,35 @@ val LocalRouter: ProvidableCompositionLocal<Router<*>?> =
4343
* Creates a router that retains a stack of [C] configuration
4444
*
4545
* @param type configuration class type
46-
* @param stack initial stack of configurations
46+
* @param key
47+
* @param initialStack initial stack of configurations
4748
* @param handleBackButton should the router handle back button
4849
*/
4950
@Composable
5051
fun <C : Parcelable> rememberRouter(
5152
type: KClass<C>,
52-
key: Any = "${type.key}.router",
53-
stack: List<C>,
54-
handleBackButton: Boolean = true
53+
key: Any = type.key,
54+
handleBackButton: Boolean = true,
55+
initialStack: () -> List<C>,
5556
): Router<C> {
56-
val routerContext = LocalRouterContext.current
57-
val keyStr = key.toString()
57+
val routerContext: RouterContext = LocalRouterContext.current
58+
val routerKey = "$key.router"
5859

59-
return remember {
60-
routerContext.getOrCreate(key = keyStr) {
61-
val navigation = StackNavigation<C>()
62-
Router(
63-
navigator = navigation,
64-
stack = routerContext.childStack(
60+
return remember(routerKey) {
61+
routerContext.getOrCreate(key = routerKey) {
62+
val navigation: StackNavigation<C> = StackNavigation()
63+
val stack: State<ChildStack<C, RouterContext>> = routerContext
64+
.childStack(
6565
source = navigation,
66-
initialStack = { stack },
66+
initialStack = initialStack,
6767
configurationClass = type,
68-
key = keyStr,
68+
key = routerKey,
6969
handleBackButton = handleBackButton,
7070
childFactory = { _, childComponentContext -> RouterContext(childComponentContext) },
71-
).asState(routerContext.lifecycle),
72-
)
71+
)
72+
.asState(routerContext.lifecycle)
73+
74+
Router(navigation, stack)
7375
}
7476
}
7577
}

0 commit comments

Comments
 (0)