Uli Bubenheimer
01/27/2024, 10:06 AMremember
has access to the prior value, but does not expose it. Is there an API that does?
This can be done via an effect, but this implies an additional recomposition to use the new value.
I need this for updating a list that blends old values with new values from changed inputs. Right now I'm working around the issue with a second `remember`ed MutableState
that is updated to the prior value on every recomposition. Seems ridiculously complicated when remember
already has the info, just won't make it available.
This is what I want, conceptually; I am aware that the API would have to look a little different to accommodate initial calculation:
val list = remember(modelData) { priorValue -> mergeUpdatedList(priorValue, modelData) }
Zach Klippenstein (he/him) [MOD]
01/27/2024, 6:25 PMUli Bubenheimer
01/27/2024, 7:27 PMUli Bubenheimer
01/27/2024, 8:04 PM@ComposeCompilerApi
calls, and the original Composer.cache()
is @ComposeCompilerApi
as well. What are the implications of redefining and leveraging @ComposeCompilerApi
functions in app code?
inline fun <T> Composer.cache(
invalid: Boolean,
initial: @DisallowComposableCalls () -> T,
block: @DisallowComposableCalls (T) -> T
): T {
@Suppress("UNCHECKED_CAST")
return rememberedValue().let {
if (it === Composer.Empty) {
val value = initial()
updateRememberedValue(value)
value
} else if (invalid) {
val value = block(it as T)
updateRememberedValue(value)
value
} else it as T
}
}