g

    Guy Bieber

    2 years ago
    2. What is the correct way to bind data from your @Model to a @Composable function parameter? Here is an example:
    data class VertTempControl (
        var temp : Int = 72,
        var step : Int = 1
    ) : Control()
    ...
    @Model
    data class DataModel (
        var username : String = "dude",
        var password : String = "blah",
        var oldPage : Pages = Pages.SPLASH_PAGE,
        var newPage : Pages = Pages.SPLASH_PAGE,
        var temperature : Int = 72
    )
    var dataModel = DataModel()
    ...
                var vt = VertTempControl(
                    temp = dataModel.temperature,
                    step = 1
                ) 
    ...
    showControl (vt)
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago
    That looks fine, as long as the code from
    var vt = VertTempControl
    etc is inside a composable function. In that case, compose will see that you're reading a property on
    dataModel
    and will re-invoke your function when that property changes, which will recreate your
    VertTempControl
    object with the new value, pass it to
    showControl
    again, etc
    Unrelated, but
    dataModel
    should probably be a
    val
    since it is itself mutable.
    g

    Guy Bieber

    2 years ago