Model

@Target([AnnotationTarget.CLASS]) @StableMarker annotation class Model
androidx.compose.Model

Model can be applied to a class which represents your application's data model, and will cause instances of the class to become observable, such that a read of a property of an instance of this class during the invocation of a composable function will cause that component to be "subscribed" to mutations of that instance. Composable functions which directly or indirectly read properties of the model class, the composables will be recomposed whenever any properties of the the model are written to.

Model also adds an MVCC transaction system to ensure data consistency across threads.

Consider the following example:

import androidx.compose.memo
import androidx.ui.core.TextField
import androidx.ui.material.Button

@Model
class LoginState(var username: String, var password: String) {
    fun login() = Api.login(username, password)
}

@Composable
fun LoginScreen() {
    val model = +memo { LoginState("user", "pass") }

    TextField(
        value = model.username,
        onValueChange = { model.username = it }
    )
    TextField(
        value = model.password,
        onValueChange = { model.password = it }
    )
    Button(text = "Login", onClick = { model.login() })
}
In this example, LoginScreen is recomposed every time the username and password of themodel updates, keeping the UI synchronized with the model. There is no need to callandroidx.compose.Recompose manually.

Summary

Public constructors

Model can be applied to a class which represents your application's data model, and will cause instances of the class to become observable, such that a read of a property of an instance of this class during the invocation of a composable function will cause that component to be "subscribed" to mutations of that instance.

Public constructors

<init>

Model()

Model can be applied to a class which represents your application's data model, and will cause instances of the class to become observable, such that a read of a property of an instance of this class during the invocation of a composable function will cause that component to be "subscribed" to mutations of that instance. Composable functions which directly or indirectly read properties of the model class, the composables will be recomposed whenever any properties of the the model are written to.

Model also adds an MVCC transaction system to ensure data consistency across threads.

Consider the following example:

import androidx.compose.memo
import androidx.ui.core.TextField
import androidx.ui.material.Button

@Model
class LoginState(var username: String, var password: String) {
    fun login() = Api.login(username, password)
}

@Composable
fun LoginScreen() {
    val model = +memo { LoginState("user", "pass") }

    TextField(
        value = model.username,
        onValueChange = { model.username = it }
    )
    TextField(
        value = model.password,
        onValueChange = { model.password = it }
    )
    Button(text = "Login", onClick = { model.login() })
}
In this example, LoginScreen is recomposed every time the username and password of themodel updates, keeping the UI synchronized with the model. There is no need to callandroidx.compose.Recompose manually.