Home > Net >  Remember mutableStateOf custom class in Android Compose
Remember mutableStateOf custom class in Android Compose

Time:06-10

I created a android compose component and to avoid multiple params, it takes only one parameter : an object

Here is my component :

@Composable
fun ValidationButton(validationButtonModel: ValidationButtonModel)

My object :

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class ValidationButtonModel(
    var title: Int,
    var contentDescription: Int,
    var rfidReadingStarted: Boolean,
    var progression: Float,
    var read: Int,
    var expected: Int,
) : Parcelable

Here is how i define it to compose for being remember between recomposition AND being a state (launch recomposition when change) :

val validationButtonModelState by rememberSaveable() {
        mutableStateOf(
            ValidationButtonModel(
                R.string.common_validate,
                R.string.common_contentDescription,
                true,
                0.1f,
                7,
                10
            )
        )
    }

But if i try to update it with for example this :

ValidationButton(
                    validationButtonModelState,
                )

Button(onClick = 
          {
                 validationButtonModelState.rfidReadingStarted = true
                 if (validationButtonModelState.progression < 1.0f) {
                    validationButtonModelState.progression  = 0.1f
                    validationButtonModelState.read  = 1
                 }
          }, content = {
              Text("INCREMENT")
            })

There is no re composition.

I try to add a Saver but doesn't work too :

val validationButtonModelSaver = listSaver<ValidationButtonModel, Any>(
    save = { listOf(it.rfidReadingStarted, it.read, it.progression, it.expected, it.title, it.contentDescription) },
    restore = { ValidationButtonModel(rfidReadingStarted = it[0] as Boolean, read = it[1] as Int, progression = it[2] as Float, expected = it[4] as Int, title = it[5] as Int, contentDescription = it[6] as Int)}
)

val validationButtonModelState by rememberSaveable(validationButtonModelSaver) {
        mutableStateOf(
            ValidationButtonModel(
                R.string.common_validate,
                R.string.common_contentDescription,
                true,
                0.1f,
                7,
                10
            )
        )
    }

Am i missing something ? Following this : https://developer.android.com/jetpack/compose/state#parcelize It should works

CodePudding user response:

If you only change the fields inside your model, mutableStateOf(ModValidationButtonModel) it won't work, as you are still updating your old instance. You need to pass in a new instance of your model effectively.

try

validationButtonModelState = ModValidationButtonModel()

or since it is a data class you can also do

validationButtonModelState = validationButtonModelState.copy(
    read = validationButtonModelState.read 1
)
  • Related