update to Jetpack Compose dev05

This commit is contained in:
John O'Reilly 2020-02-21 20:22:07 +00:00
parent b1c7d2c89f
commit 80e6da5274
2 changed files with 20 additions and 16 deletions

View file

@ -34,10 +34,12 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.ui:ui-framework:0.1.0-dev03'
implementation 'androidx.ui:ui-layout:0.1.0-dev03'
implementation 'androidx.ui:ui-material:0.1.0-dev03'
implementation 'androidx.ui:ui-tooling:0.1.0-dev03'
implementation 'androidx.ui:ui-framework:0.1.0-dev05'
implementation 'androidx.ui:ui-layout:0.1.0-dev05'
implementation 'androidx.ui:ui-material:0.1.0-dev05'
implementation 'androidx.ui:ui-tooling:0.1.0-dev05'
implementation 'androidx.ui:ui-graphics:0.1.0-dev05'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

View file

@ -6,11 +6,11 @@ import androidx.compose.*
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.ui.core.Text
import androidx.ui.core.dp
import androidx.ui.core.setContent
import androidx.ui.layout.Column
import androidx.ui.layout.Padding
import androidx.ui.layout.LayoutPadding
import androidx.ui.material.MaterialTheme
import androidx.ui.unit.dp
import com.surrus.common.remote.Assignment
import org.koin.android.viewmodel.ext.android.viewModel
@ -28,7 +28,7 @@ class MainActivity : AppCompatActivity() {
@Composable
fun mainLayout(peopleInSpaceViewModel: PeopleInSpaceViewModel) {
MaterialTheme {
val people = +observe(peopleInSpaceViewModel.peopleInSpace)
val people = observe(peopleInSpaceViewModel.peopleInSpace)
Column {
people?.forEach { person ->
Row(person)
@ -40,24 +40,26 @@ fun mainLayout(peopleInSpaceViewModel: PeopleInSpaceViewModel) {
@Composable
fun Row(person: Assignment) {
Padding(16.dp) {
Text(text = "${person.name} (${person.craft})")
}
Text(
text = "${person.name} (${person.craft})",
modifier = LayoutPadding(16.dp)
)
}
// from https://medium.com/swlh/android-mvi-with-jetpack-compose-b0890f5156ac
fun <T> observe(data: LiveData<T>) = effectOf<T?> {
val result = +state<T?> { data.value }
val observer = +memo { Observer<T> { result.value = it } }
// update: since dev05, version from that article was updated based
// on https://twitter.com/intelligibabble/status/1205318193960472576
@Composable fun <T> observe(data: LiveData<T>) : T? {
val result = state<T?> { data.value }
val observer = remember { Observer<T> { result.value = it } }
+onCommit(data) {
onCommit(data) {
data.observeForever(observer)
onDispose { data.removeObserver(observer) }
}
result.value
return result.value
}