You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
API overhaul because splitting the keys and the composables don't work.
All of our tests and demos were built using `String` as the key, with `content` that does nothing but render the key.
This approach doesn't reflect reality very well, and masked #63, where keys for more interesting objects can get out of sync with the `content` lambda that can render them.
When popping, you would wind up crashing when the up to date lambda is unable to interpret the key for the screen that is being animated away.
The fix is to change the API from something that takes a list of keys and a function that can render them, to a list of model objects that themselves are able to provide `@Composable Content()`.
IMHO the updated API actually feels pretty good, more like the conventional hoisted-state `@Composable Foo(model: FooModel)` idiom.
(Of course I've been working on this all day, so I'm biased.)
We provide a new interface:
```kotlin
interface BackstackFrame<out K : Any> {
val key: K
@composable fun Content()
}
```
And change the signature of the `Backstack()` function:
```kotlin
fun <K : Any> Backstack(
frames: List<BackstackFrame<K>>,
modifier: Modifier = Modifier,
frameController: FrameController<K>
)
```
Note that the param type, `K`, is still the type of the key, not the type of a particular flavor of `BackstackFrame`.
This makes it easy for us to provide convenience functions to map lists of arbitrary model objects to `BackstackFrame` instances, so it's not much more verbose than it used to be to make it go.
Before:
```kotlin
Backstack(backstack) { screen ->
when(screen) {
Screen.ContactList -> ShowContactList(navigator)
is Screen.ContactDetails -> ShowContact(screen.id, navigator)
is Screen.EditContact -> ShowEditContact(screen.id, navigator)
}
}
```
After:
```kotlin
Backstack(
backstack.toBackstackModel { screen ->
when(screen) {
Screen.ContactList -> ShowContactList(navigator)
is Screen.ContactDetails -> ShowContact(screen.id, navigator)
is Screen.EditContact -> ShowEditContact(screen.id, navigator)
}
)
```
Note that there are two flavors of `toBackstackModel`. The second one supports models with more interesting keys.
```kotlin
data class Portrait(
val id: Int,
val url: String
)
Backstack(
backstack.toBackstackModel(
getKey = { it.id }
) {
PrettyPicture(it.url)
}
)
```
Fixes#63
Copy file name to clipboardexpand all lines: compose-backstack/api/compose-backstack.api
+14-11
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,11 @@
1
+
public abstract interface class com/zachklipp/compose/backstack/BackstackFrame {
2
+
public abstract fun Content (Landroidx/compose/runtime/Composer;I)V
3
+
public abstract fun getKey ()Ljava/lang/Object;
4
+
}
5
+
1
6
public final class com/zachklipp/compose/backstack/BackstackKt {
2
-
public static final fun Backstack (Ljava/util/List;Landroidx/compose/ui/Modifier;Lcom/zachklipp/compose/backstack/BackstackTransition;Landroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function0;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)V
3
-
public static final fun Backstack (Ljava/util/List;Landroidx/compose/ui/Modifier;Lcom/zachklipp/compose/backstack/BackstackTransition;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
4
-
public static final fun Backstack (Ljava/util/List;Landroidx/compose/ui/Modifier;Lcom/zachklipp/compose/backstack/FrameController;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;II)V
5
-
public static synthetic fun Backstack$default (Ljava/util/List;Landroidx/compose/ui/Modifier;Lcom/zachklipp/compose/backstack/BackstackTransition;Landroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function0;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;ILjava/lang/Object;)V
7
+
public static final fun Backstack (Ljava/util/List;Landroidx/compose/ui/Modifier;Lcom/zachklipp/compose/backstack/BackstackTransition;Landroidx/compose/runtime/Composer;II)V
8
+
public static final fun Backstack (Ljava/util/List;Landroidx/compose/ui/Modifier;Lcom/zachklipp/compose/backstack/FrameController;Landroidx/compose/runtime/Composer;II)V
6
9
}
7
10
8
11
public abstract interface class com/zachklipp/compose/backstack/BackstackTransition {
@@ -30,15 +33,15 @@ public abstract interface class com/zachklipp/compose/backstack/FrameController
30
33
public abstract fun updateBackstack (Ljava/util/List;)V
31
34
}
32
35
33
-
public final class com/zachklipp/compose/backstack/FrameController$BackstackFrame {
34
-
public fun <init> (Ljava/lang/Object;Landroidx/compose/ui/Modifier;)V
35
-
public synthetic fun <init> (Ljava/lang/Object;Landroidx/compose/ui/Modifier;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
36
-
public final fun component1 ()Ljava/lang/Object;
36
+
public final class com/zachklipp/compose/backstack/FrameController$FrameAndModifier {
37
+
public fun <init> (Lcom/zachklipp/compose/backstack/BackstackFrame;Landroidx/compose/ui/Modifier;)V
38
+
public synthetic fun <init> (Lcom/zachklipp/compose/backstack/BackstackFrame;Landroidx/compose/ui/Modifier;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
39
+
public final fun component1 ()Lcom/zachklipp/compose/backstack/BackstackFrame;
37
40
public final fun component2 ()Landroidx/compose/ui/Modifier;
38
-
public final fun copy (Ljava/lang/Object;Landroidx/compose/ui/Modifier;)Lcom/zachklipp/compose/backstack/FrameController$BackstackFrame;
39
-
public static synthetic fun copy$default (Lcom/zachklipp/compose/backstack/FrameController$BackstackFrame;Ljava/lang/Object;Landroidx/compose/ui/Modifier;ILjava/lang/Object;)Lcom/zachklipp/compose/backstack/FrameController$BackstackFrame;
41
+
public final fun copy (Lcom/zachklipp/compose/backstack/BackstackFrame;Landroidx/compose/ui/Modifier;)Lcom/zachklipp/compose/backstack/FrameController$FrameAndModifier;
42
+
public static synthetic fun copy$default (Lcom/zachklipp/compose/backstack/FrameController$FrameAndModifier;Lcom/zachklipp/compose/backstack/BackstackFrame;Landroidx/compose/ui/Modifier;ILjava/lang/Object;)Lcom/zachklipp/compose/backstack/FrameController$FrameAndModifier;
40
43
public fun equals (Ljava/lang/Object;)Z
41
-
public final fun getKey ()Ljava/lang/Object;
44
+
public final fun getFrame ()Lcom/zachklipp/compose/backstack/BackstackFrame;
42
45
public final fun getModifier ()Landroidx/compose/ui/Modifier;
0 commit comments