update to compose dev09 + using liveData.observeAsState()

This commit is contained in:
John O'Reilly 2020-04-15 22:27:08 +01:00
parent 6ef1f2306c
commit 1a798f6357
3 changed files with 7 additions and 14 deletions

View file

@ -51,6 +51,7 @@ dependencies {
implementation "androidx.ui:ui-material:${Versions.compose}"
implementation "androidx.ui:ui-tooling:${Versions.compose}"
implementation "androidx.ui:ui-graphics:${Versions.compose}"
implementation "androidx.ui:ui-livedata:${Versions.compose}"
implementation 'com.google.android.material:material:1.1.0'

View file

@ -5,10 +5,11 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.compose.*
import androidx.compose.frames.ModelList
import androidx.lifecycle.Observer
import androidx.ui.core.Text
import androidx.ui.core.setContent
import androidx.ui.foundation.Text
import androidx.ui.layout.Column
import androidx.ui.layout.LayoutPadding
import androidx.ui.livedata.observeAsState
import androidx.ui.material.MaterialTheme
import androidx.ui.tooling.preview.Preview
import androidx.ui.unit.dp
@ -16,32 +17,23 @@ import com.surrus.common.remote.Assignment
import org.koin.android.viewmodel.ext.android.viewModel
@Model
class PeopleState(var peopleInSpace: ModelList<Assignment> = ModelList())
class MainActivity : AppCompatActivity() {
private val peopleInSpaceViewModel: PeopleInSpaceViewModel by viewModel()
private val peopleState = PeopleState()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
peopleInSpaceViewModel.peopleInSpace.observe(this, Observer {
peopleState.peopleInSpace.clear()
peopleState.peopleInSpace.addAll(it)
})
setContent {
mainLayout(peopleState)
mainLayout(peopleInSpaceViewModel.peopleInSpace.observeAsState())
}
}
}
@Composable
fun mainLayout(peopleState: PeopleState) {
fun mainLayout(peopleState: State<List<Assignment>?>) {
MaterialTheme {
Column {
peopleState.peopleInSpace.forEach { person ->
peopleState.value?.forEach { person ->
Row(person)
}
}

View file

@ -16,7 +16,7 @@ object Versions {
const val room = "2.1.0-beta01"
const val lifecycle = "2.2.0-alpha01"
const val fragment = "1.1.0-alpha09"
const val compose = "0.1.0-dev07"
const val compose = "0.1.0-dev09"
const val junit = "4.12"
const val coreTesting = "2.0.0"