Dirk Hoffmann
03/02/2021, 11:20 AMtheapache64
03/02/2021, 1:34 PMAdam Powell
03/02/2021, 2:48 PMAdam Powell
03/02/2021, 2:49 PMAdam Powell
03/02/2021, 2:50 PMAdam Powell
03/02/2021, 2:51 PMAdam Powell
03/02/2021, 2:51 PMDirk Hoffmann
03/02/2021, 2:53 PMAdam Powell
03/02/2021, 3:45 PMobject MySingleton {
private var myPrivateState by mutableStateOf(0)
fun increment() {
myPrivateState++
}
fun getCurrentMessage() = when {
myPrivateState == 0 -> "none"
myPrivateState < 5 -> "a few"
else -> "a lot"
}
}
and then in your composable function you write:
Text(MySingleton.getCurrentMessage())
then the text will update whenever the singleton's internal state changes.Dirk Hoffmann
03/02/2021, 3:48 PMText(...)
if e.g. the parent method of the Text(...)
would NOT have the `MySingleton.myPriateState`somewhere as method parameter, the Text(...)
would still be redrawn on change, but parent method itself not ... have I understood correctly?Dirk Hoffmann
03/02/2021, 3:50 PM@Composable
Compose will remember the values of all its function-parameters and if some will change (and only then), the method is called/executed again with new new valuesAdam Powell
03/02/2021, 3:51 PMmutableStateOf
in MySingleton
was read. When it changes, compose will re-execute your composable function, which will call MySingleton.getCurrentMessage()
again, which will return a new value and record that that snapshot state object was still read during that recomposition.Dirk Hoffmann
03/02/2021, 3:54 PMState
?) all(?) methods in that stacktrace that are marked with @Composable
are "internally" stored in a "`Map<State, Set<Function>>`" ... would that be the right "mental abstraction" ?Adam Powell
03/02/2021, 7:57 PMAdam Powell
03/02/2021, 7:57 PMAdam Powell
03/02/2021, 7:58 PMAdam Powell
03/02/2021, 7:58 PMAdam Powell
03/02/2021, 7:59 PMAdam Powell
03/02/2021, 8:00 PMsnapshotFlow {}
flow builderAdam Powell
03/02/2021, 8:01 PMAdam Powell
03/02/2021, 8:03 PMColton Idle
03/03/2021, 3:02 AM