Skip to content

Commit 38e229b

Browse files
authored
Fix rememberOnRoute to take in key if needed (#10)
1 parent feda652 commit 38e229b

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ fun ListDetailScreen() {
148148
@Composable
149149
fun DetailsScreen(detail: String) {
150150
// 📦 Scope an instance (a view model, a state-holder or whatever) to a route with [rememberOnRoute]
151-
// 1. Makes your instances survive configuration changes (on android) 🔁
152-
// 2. Holds-on the instance as long as it is in the backstack 🔗
153-
val instance: DetailInstance = rememberOnRoute { savedState -> DetailInstance(savedState, detail) }
151+
// This makes your instances survive configuration changes (on android) 🔁
152+
// And holds-on the instance as long as it is in the backstack 🔗
153+
// Pass in key if you want to reissue a new instance when key changes 🔑 (optional)
154+
val instance: DetailInstance = rememberOnRoute(key = detail) { savedState -> DetailInstance(savedState, detail) }
154155

155156
val state: DetailState by instance.states.collectAsState()
156157
Text(text = state.detail)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class io/github/xxfast/decompose/router/Router : com/arkivanov/deco
1313

1414
public final class io/github/xxfast/decompose/router/RouterKt {
1515
public static final fun getLocalRouter ()Landroidx/compose/runtime/ProvidableCompositionLocal;
16-
public static final fun rememberOnRoute (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;I)Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper$Instance;
16+
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;
1717
public static final fun rememberRouter (Lkotlin/reflect/KClass;Ljava/util/List;ZLandroidx/compose/runtime/Composer;II)Lio/github/xxfast/decompose/router/Router;
1818
}
1919

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class io/github/xxfast/decompose/router/Router : com/arkivanov/deco
1313

1414
public final class io/github/xxfast/decompose/router/RouterKt {
1515
public static final fun getLocalRouter ()Landroidx/compose/runtime/ProvidableCompositionLocal;
16-
public static final fun rememberOnRoute (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;I)Lcom/arkivanov/essenty/instancekeeper/InstanceKeeper$Instance;
16+
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;
1717
public static final fun rememberRouter (Lkotlin/reflect/KClass;Ljava/util/List;ZLandroidx/compose/runtime/Composer;II)Lio/github/xxfast/decompose/router/Router;
1818
}
1919

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ fun <C : Parcelable> rememberRouter(
7070
* Creates a instance of [T] that is scoped to the current route
7171
*
7272
* @param instanceClass class of [T] instance
73+
* @param key key to remember the instance with. Defaults to [instanceClass]
7374
* @param block lambda to create an instance of [T] with a given [SavedStateHandle]
7475
*/
7576
@Suppress("UNCHECKED_CAST")
7677
@Composable
7778
fun <T : Instance> rememberOnRoute(
7879
instanceClass: KClass<T>,
80+
key: Any = instanceClass,
7981
block: @DisallowComposableCalls (savedState: SavedStateHandle) -> T
8082
): T {
8183
val component: ComponentContext = LocalComponentContext.current
@@ -87,7 +89,7 @@ fun <T : Instance> rememberOnRoute(
8789
val instanceKey = "$packageName.instance"
8890
val stateKey = "$packageName.savedState"
8991

90-
val (instance, savedState) = remember(instanceClass) {
92+
val (instance, savedState) = remember(key) {
9193
val savedState: SavedStateHandle = instanceKeeper
9294
.getOrCreate(stateKey) { SavedStateHandle(stateKeeper.consume(stateKey, SavedState::class)) }
9395
var instance: T? = instanceKeeper.get(instanceKey) as T?

0 commit comments

Comments
 (0)