Mark Murphy
08/19/2019, 5:13 PM@Model
be working in the current Compose code?
I created this based on the @Model
KDoc:
@Model
data class AgreementViewState(var terms: Boolean = true, var privacy: Boolean = false) {
val canProceed = terms && privacy
}
@Composable
private fun Agreement() {
Padding(8.dp) {
val viewState = +model { AgreementViewState() }
Column(mainAxisAlignment = MainAxisAlignment.Start, crossAxisAlignment = CrossAxisAlignment.Start) {
Row(mainAxisAlignment = MainAxisAlignment.Start) {
Checkbox(
checked = viewState.terms,
onCheckedChange = { viewState.terms = it }
)
Text(text = "I agree to the terms and conditions")
}
Row(mainAxisAlignment = MainAxisAlignment.Start) {
Checkbox(
checked = viewState.privacy,
onCheckedChange = { viewState.privacy = it }
)
Text(text = "I agree to the privacy policy")
}
Text(text = if (viewState.canProceed) "You may proceed" else "Please indicate your agreement")
}
}
}
The initial state works (the top Checkbox
is checked). And via logging I can confirm that my onCheckedChange
lambdas are being called. However, the UI is not being recomposed as I update the AgreementViewState
, so the checkboxes and text remain fixed.
By contrast, this works with automatic recomposition as I update the `checkboxState`:
@Composable
private fun SimpleCheckbox() {
Padding(8.dp) {
val checkboxState = +state { true }
Checkbox(
checked = checkboxState.value,
onCheckedChange = { checkboxState.value = it }
)
}
}
I'm trying to determine if there's something that I'm doing wrong or if this is just a current limitation. Thanks!matvei
08/19/2019, 5:26 PMMark Murphy
08/19/2019, 5:27 PMmatvei
08/19/2019, 5:40 PMMark Murphy
08/19/2019, 5:43 PMr
value not being set.
Then, I mistakenly tried cleaning the project, in case I had lingering code generated from the previous (nested) location. That completely broke the project, so now I need to try to convince the Compose custom Studio build to work again. 😣Sean McQuillan [G]
08/19/2019, 7:27 PMcanProceed
should be a getter (or extension property).canProceed
being a derived prop of the other observables). Do you see a similar issue if you switch it to a getter instead of a property initializer?Mark Murphy
08/19/2019, 7:45 PMLuca Nicoletti
08/19/2019, 7:59 PMvar
works fine for meval
and assigning it the constructor will lead to never be able to update the UI, right?Mark Murphy
08/19/2019, 8:02 PMcanProceed
would be a derived property. @Model
is all about the mutability, though.Luca Nicoletti
08/19/2019, 8:02 PMText(text = if (viewState.canProceed) "You may proceed" else "Please indicate your agreement")
will never print You may proceed
if you have canProceed
as a val initialized by default as falseget()
, as I would do even not using Compose 🙂