Skip to content

Commit

Permalink
Refactor router api to mirror decompose api
Browse files Browse the repository at this point in the history
  • Loading branch information
xxfast committed Sep 21, 2023
1 parent a8fe6de commit fc08f7e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion decompose-router/api/android/decompose-router.api
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class io/github/xxfast/decompose/router/RouterContextKt {
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;
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;
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;
}

public final class io/github/xxfast/decompose/router/SavedState : android/os/Parcelable {
Expand Down
2 changes: 1 addition & 1 deletion decompose-router/api/desktop/decompose-router.api
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class io/github/xxfast/decompose/router/RouterContextKt {
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;
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;
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;
}

public final class io/github/xxfast/decompose/router/SavedState : com/arkivanov/essenty/parcelable/Parcelable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import io.github.xxfast.decompose.screen.nested.NestedScreen
@OptIn(ExperimentalDecomposeApi::class)
@Composable
fun HomeScreen() {
val router: Router<HomeScreens> =
rememberRouter(HomeScreens::class, stack = listOf(HomeScreens.List))
val router: Router<HomeScreens> = rememberRouter(HomeScreens::class) { listOf(HomeScreens.List) }

RoutedContent(
router = router,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun NestedScreen(
onBack: () -> Unit,
onSelect: (Int) -> Unit
) {
val router: Router<NestedScreens> = rememberRouter(NestedScreens::class, stack = listOf(Home))
val router: Router<NestedScreens> = rememberRouter(NestedScreens::class) { listOf(Home) }

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import kotlin.reflect.KClass
* Router with a given navigator and a stack
* Detailed breakdown of this available [here](https://proandroiddev.com/diy-compose-multiplatform-navigation-with-decompose-94ac8126e6b5)
*
* @param navigator decompose navigator to use
* @param navigation decompose navigator to use
* @param stack state of decompose child stack to use
*/
class Router<C : Parcelable>(
private val navigator: StackNavigation<C>,
private val navigation: StackNavigation<C>,
val stack: State<ChildStack<C, RouterContext>>,
) : StackNavigation<C> by navigator
) : StackNavigation<C> by navigation

/***
* Compositional local for component context
Expand All @@ -43,33 +43,35 @@ val LocalRouter: ProvidableCompositionLocal<Router<*>?> =
* Creates a router that retains a stack of [C] configuration
*
* @param type configuration class type
* @param stack initial stack of configurations
* @param key
* @param initialStack initial stack of configurations
* @param handleBackButton should the router handle back button
*/
@Composable
fun <C : Parcelable> rememberRouter(
type: KClass<C>,
key: Any = "${type.key}.router",
stack: List<C>,
handleBackButton: Boolean = true
key: Any = type.key,
handleBackButton: Boolean = true,
initialStack: () -> List<C>,
): Router<C> {
val routerContext = LocalRouterContext.current
val keyStr = key.toString()
val routerContext: RouterContext = LocalRouterContext.current
val routerKey = "$key.router"

return remember {
routerContext.getOrCreate(key = keyStr) {
val navigation = StackNavigation<C>()
Router(
navigator = navigation,
stack = routerContext.childStack(
return remember(routerKey) {
routerContext.getOrCreate(key = routerKey) {
val navigation: StackNavigation<C> = StackNavigation()
val stack: State<ChildStack<C, RouterContext>> = routerContext
.childStack(
source = navigation,
initialStack = { stack },
initialStack = initialStack,
configurationClass = type,
key = keyStr,
key = routerKey,
handleBackButton = handleBackButton,
childFactory = { _, childComponentContext -> RouterContext(childComponentContext) },
).asState(routerContext.lifecycle),
)
)
.asState(routerContext.lifecycle)

Router(navigation, stack)
}
}
}
Expand Down

0 comments on commit fc08f7e

Please sign in to comment.