Atul Gupta
12/06/2023, 3:38 PMval deltaState by remoberDelta(key, valueToTrack) { oldValue -> createNewValueBasedOnPreviousValue(oldValue) }
is there some api which can achieve this is nicer syntax?Atul Gupta
12/06/2023, 3:41 PMvalueToTrack
can be some data(maybe a data class) which is passed to composable. Trailing lambda should be invoked when Compose runtime thinks that valueToTrack
has been changedAtul Gupta
12/07/2023, 2:19 PM@Composable
fun <T> rememberRef(): MutableState<T?>
{
return remember {
object : MutableState<T?>
{
override var value: T? = null
override fun component1(): T? = value
override fun component2(): (T?) -> Unit = { value = it }
}
}
}
@Composable
fun <T> rememberPrevious(
current: T,
shouldUpdate: (prev: T?, curr: T) -> Boolean = { a: T?, b: T -> a != b },
): T?
{
val ref = rememberRef<T>()
SideEffect {
if (shouldUpdate(ref.value, current))
{
ref.value = current
}
}
return ref.value
}
ref: https://stackoverflow.com/questions/67801939/get-previous-value-of-state-in-composable-jetpack-composeZach Klippenstein (he/him) [MOD]
12/07/2023, 3:22 PMsnapshotFlow
with operators like scan
.
However, nothing can ever guarantee that either of these approaches will see every state because it’s very easy to transition to multiple intermediate states without any observer ever seeing them.Zach Klippenstein (he/him) [MOD]
12/07/2023, 3:23 PMAtul Gupta
12/07/2023, 3:24 PMThere's no api for it because trying to do this at all is usually a code smell.
I was not aware of this.. in my usecase I want to perform some task(UI event like haptic) when new element(of specific type) gets added to a LazyList.Atul Gupta
12/07/2023, 3:28 PMZach Klippenstein (he/him) [MOD]
12/07/2023, 3:35 PMAtul Gupta
12/07/2023, 3:40 PMZach Klippenstein (he/him) [MOD]
12/07/2023, 4:07 PM