Lilly
10/07/2021, 6:47 PM@Stable
and would expect that a change to the property would reflect the new state but it doesn't:
@Stable
data class StableState(var isChecked: Boolean)
@Composable
fun StableTest(state: StableState) {
Checkbox(checked = state.isChecked, onCheckedChange = {
state.isChecked = it
})
}
What did I miss?mkrussel
10/07/2021, 6:59 PMStable
class should get reported back to compose through a compose state object.Lilly
10/07/2021, 7:09 PMAlex Vanyo
10/07/2021, 7:11 PMStableState
that is stable with the same interface could be:
@Stable
class StableState(isChecked: Boolean) {
var isChecked by mutableStateOf(isChecked)
}
mkrussel
10/07/2021, 7:11 PMState
variable in your object that changes when that property changes. I believe the state can be private and it will still work.
Not sure if it is possible to have a stable non immutable data classCasey Brooks
10/07/2021, 7:15 PMvar
with val mutableStateOf
is the main way to make a property able to notify Compose of changesStable
, unless you override the implementation of equals/hashcode
. You could change the property of one instance and make it not equal to another that it was previously equal to (thus breaking rule #1 of @Stable
)Lilly
10/07/2021, 7:18 PM@Stable
private class SnackbarDataImpl(
override val message: String,
override val actionLabel: String?,
override val duration: SnackbarDuration,
private val continuation: CancellableContinuation<SnackbarResult>
) : SnackbarData { ... }
or
@Stable
class DrawerState(
initialValue: DrawerValue,
confirmStateChange: (DrawerValue) -> Boolean = { true }
) { .. }
I see here primitives not State
objects.Casey Brooks
10/07/2021, 7:23 PMSnackbarDataImpl
are all `val`s, meaning the class is immutable. Compose has no need of ever recomposing on any particular property in the class. Since nothing in the class could ever change, the corresponding UI will always be the same. To actually update the UI with a new snackbar, you'd replacce the old instance with a new instance, which Compose would detect as a change and then update the corresponding parts of the UI as a result. But Compoe will only know that there's a new Snackbar when a mutableStateOf
has that new snackbar instance set to it.Lilly
10/07/2021, 7:25 PMCasey Brooks
10/07/2021, 7:28 PMState
variable and displays the actual snackbar data as anything else would. The same pattern holds for scrolling, ripples, genreic animatioons, and everything else. The data behind the whole of Compose is always immutable at the data class level, or else uses mutableStateOf
to make sure Compose knows when something changesLilly
10/07/2021, 7:35 PMandwhen applied to a type indicates a type that is mutable, but the Compose runtime will be notified if and when any public properties or method behavior would yield different results from a previous invocation. Such a type may only back its properties using other@Stable
or@Stable
types.@Immutable
MutableState
is such a @Stable
type:
@Stable
interface MutableState<T> : State<T>