androidkotlinandroid-jetpack-composeandroid-jetpack-compose-ui

how to tell the UI to change the Values to a computed property


usually in iOS i will do a didSet but Kotlin does not provide the same, how can I do Such a thing.

`

val dnValues = stringArrayResource(R.array.dn_values_array)

val pnValuesSteel = stringArrayResource(R.array.pn_values_array_full)
val pnValuesGray = stringArrayResource(R.array.pn_values_array_gray_iron)
val pnValuesDuctile = stringArrayResource(R.array.pn_values_array_ductile_iron)

var pnValues = pnValuesGray

val materialValues = stringArrayResource(R.array.materials_array)

val newMaterial: (String) -> Unit = { material ->

    when (material) {
        materialValues[0] -> {
            pnValues = pnValuesGray
        }

        materialValues[1] -> {
            pnValues = pnValuesDuctile
        }

        materialValues[2] -> {
            pnValues = pnValuesSteel
        }
    }
}`

when I debug the code I see the pnValues change the way I want, but the UI never refresh on the edit.


Solution

  • I have tried the previous comments, because the Values get stuck on the remembered ones and the Custom picker won't work.

    val dnValues = stringArrayResource(R.array.dn_values_array)
    
    val pnValuesSteel = stringArrayResource(R.array.pn_values_array_full)
    val pnValuesGray = stringArrayResource(R.array.pn_values_array_gray_iron)
    val pnValuesDuctile =  stringArrayResource(R.array.pn_values_array_ductile_iron)
    
    val materialValues = stringArrayResource(R.array.materials_array)
    
    var assignedMaterial by remember { mutableStateOf("") }
    
    val newMaterial: (String) -> Unit = { material ->
    
        when (material) {
            materialValues[0] -> {
                assignedMaterial = material
            }
    
            materialValues[1] -> {
                assignedMaterial = material
            }
    
            materialValues[2] -> {
                assignedMaterial = material
            }
        }
    }
    

    what I did was, just removing the pnValuesSteel, Gray, Ductile and set the correct one depending on the material which I retrieve from the chosen one on the picker. setting the pnValuesSteel, Gray, Ductile (values) I have done it in the same scope where it should be assigned. see below

            CircularList(
                        width = 60.dp,
                        itemHeight = 50.dp,
                        items = when (assignedMaterial) {
                            materialValues[0] -> {
                                pnValuesGray
                            }
    
                            materialValues[1] -> {
                                pnValuesDuctile
                            }
    
                            materialValues[2] -> {
                                pnValuesSteel
                            }
                            else ->{
                                pnValuesGray
                            }
                        }
    

    now it works perfectly.

    any comments on the solution would be appreciated.