Grigorii Yurkov
11/02/2020, 2:34 PMmap
analog for MutableState
?Adam Powell
11/02/2020, 2:42 PMGrigorii Yurkov
11/02/2020, 2:42 PMAdam Powell
11/02/2020, 2:43 PMGrigorii Yurkov
11/02/2020, 2:47 PMclass MyViewModel : ViewModel() {
var string by mutableState("1")
val int get() = string.toInt()
}
Like this?Adam Powell
11/02/2020, 2:48 PMstring
are tracked, and the getter reads string
mutableLiveData
though 🙂Grigorii Yurkov
11/02/2020, 2:49 PMAdam Powell
11/02/2020, 2:50 PMsnapshotFlow {}
Grigorii Yurkov
11/02/2020, 2:59 PMAdam Powell
11/02/2020, 3:21 PMGrigorii Yurkov
11/02/2020, 3:32 PMAdam Powell
11/02/2020, 3:38 PMvar one = mutableStateOf("one")
var two = mutableStateOf("two")
val myFlow = snapshotFlow { "$one and $two" }
then you can take advantage of transactional changes of both one
and two
- they can't get out of sync with one anotherGrigorii Yurkov
11/02/2020, 5:17 PMclass StateHolder {
var state by mutableStateOf(0)
var stateBridgeProperty: Int
get() {
val clazz = Class.forName("ru.rpuxa.composeexperiments.StateHolder")
val method = clazz.getMethod("getState")
val value = method.invoke(this) as Int
return value
}
set(value) {
val clazz = Class.forName("ru.rpuxa.composeexperiments.StateHolder")
val method = clazz.declaredMethods.find { it.name == "setState" }!!
method.invoke(this, value)
}
}
Then I created two composable functions that just count number of clicks
val first = StateHolder()
val second = StateHolder()
@Composable
fun MyTest() {
Log.d("MyDebug", "First function recomposed")
Column {
Text(text = first.stateBridgeProperty.toString())
Button(onClick = {
first.stateBridgeProperty++
}) {
Text("++")
}
}
}
@Composable
fun MyTest2() {
Log.d("MyDebug", "Second function recomposed")
Column {
Text(text = second.stateBridgeProperty.toString())
Button(onClick = {
second.stateBridgeProperty++
}) {
Text("++")
}
}
}
I have a question - when I click the first button HOW do you understand you need to recompose first function and don't touch second? I understand you see that first.state
changed but you must have no idea that first.stateBridgeProperty
connected with first.state
and therefore you must not know you need to recompose first function.Adam Powell
11/02/2020, 5:29 PMSnapshotMutableState
object that perform the tracking. In the case of your reflection example above, those methods still get called so the tracking still happens: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/MutableState.kt;l=293?q=SnapshotMutableStateGrigorii Yurkov
11/02/2020, 5:32 PMAdam Powell
11/02/2020, 5:41 PMsnapshotFlow
might be illustrative: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/SnapshotFlow.kt;l=64Oleg Khotskin
11/02/2020, 8:43 PMAdam Powell
11/02/2020, 8:52 PM