Mark
01/27/2025, 6:27 AMMark
01/27/2025, 6:27 AMval LocalFooBarSnapshot = staticCompositionLocalOf<FooBarSnapshot> {
...
}
data class FooBarSnapshot(
val foo1: Foo1
val foo2: Foo2
val bar1: Bar1
val bar2: Bar2
)
Now suppose we also have:
data class FooSnapshot(
val foo1: Foo1
val foo2: Foo2
)
where:
data class FooBarSnapshot(
...
) {
val foo by lazy {
FooSnapshot(foo1, foo2)
}
}
Is it okay to define:
val LocalFooSnapshot = staticCompositionLocalOf<FooSnapshot> {
...
}
@Composable
fun Theme() {
...
LocalFooBarSnapshot provides fooBarSnapshot,
LocalFooSnapshot provides fooBarSnapshot.foo,
...
}
My concern is that you can potentially end up with two different `FooSnapshot`s: one from LocalFooBarSnapshot.current.foo
and another from LocalFooSnapshot.current
(considering that any composable can provide its own LocalFooSnapshot
to its child composables)Winson Chiu
01/27/2025, 7:00 AMprovidesComputed
.Mark
01/27/2025, 7:16 AMFooSnapshot
can be computed from FooBarSnapshot
Iād rather not do so because FooBarSnapshot
is expensive to calculate. They are both a subset of another snapshot. Itās just that FooBarSnapshot
is a super set of FooSnapshot
but with some additional expensive-to-compute properties.Mark
01/27/2025, 7:17 AMLocalFooBarSnapshot
and LocalFooSnapshot
available to a composable.Winson Chiu
01/27/2025, 7:20 AMMark
01/27/2025, 7:50 AMZach Klippenstein (he/him) [MOD]
01/27/2025, 4:12 PMMark
01/29/2025, 6:22 AMMark
01/29/2025, 6:28 AMZach Klippenstein (he/him) [MOD]
01/29/2025, 5:20 PMprovidesComputed
but I donāt understand why. Youāre already computing both objects to provide them in your theme.Winson Chiu
01/29/2025, 5:39 PMfun test() {
CompositionLocalProvider(
LocalFooBar provides fooBarSnapshot,
LocalFoo providesComputed { LocalFooBar.currentValue.foo },
)
}
Winson Chiu
01/29/2025, 5:39 PM.foo
lazy if it's expensive and it'll only get computed the first time it's accessed.Mark
01/30/2025, 3:27 AM