https://kotlinlang.org logo
#compose
Title
# compose
g

Guy Bieber

04/29/2020, 6:01 PM
2. What is the correct way to bind data from your @Model to a @Composable function parameter? Here is an example:
Copy code
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)
z

Zach Klippenstein (he/him) [MOD]

04/29/2020, 6:08 PM
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
👍 1
Unrelated, but
dataModel
should probably be a
val
since it is itself mutable.
❤️ 1
g

Guy Bieber

04/29/2020, 6:13 PM