compose updates + initial basic navigation

This commit is contained in:
John O'Reilly 2020-06-20 09:42:29 +01:00
parent 5dceea9797
commit 9dbb29c421
5 changed files with 75 additions and 26 deletions

View file

@ -43,7 +43,7 @@ android {
}
dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.72"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
@ -54,7 +54,7 @@ dependencies {
implementation "androidx.ui:ui-graphics:${Versions.compose}"
implementation "androidx.ui:ui-livedata:${Versions.compose}"
implementation "dev.chrisbanes.accompanist:accompanist-coil:${Versions.coliVersion}"
implementation "dev.chrisbanes.accompanist:accompanist-coil:0.1.5"
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

View file

@ -6,13 +6,11 @@ import androidx.compose.*
import androidx.ui.core.Alignment
import androidx.ui.core.Modifier
import androidx.ui.core.setContent
import androidx.ui.foundation.AdapterList
import androidx.ui.foundation.Text
import androidx.ui.foundation.*
import androidx.ui.graphics.Color
import androidx.ui.layout.*
import androidx.ui.livedata.observeAsState
import androidx.ui.material.MaterialTheme
import androidx.ui.material.TopAppBar
import androidx.ui.material.*
import androidx.ui.text.TextStyle
import androidx.ui.tooling.preview.Preview
import androidx.ui.tooling.preview.PreviewParameter
@ -39,21 +37,35 @@ class MainActivity : AppCompatActivity() {
@Composable
fun mainLayout(peopleState: State<List<Assignment>>) {
val scaffoldState = remember { ScaffoldState() }
MaterialTheme {
Column {
MaterialTheme {
Scaffold(
scaffoldState = scaffoldState,
topAppBar = {
TopAppBar(
title = {
Text("People In Space")
}
)
AdapterList(data = peopleState.value) { person ->
PersonView(person)
},
bodyContent = {
when (val screen = PeopleInSpaceNavigation.currentScreen) {
is Screen.Home -> PersonList(peopleState)
is Screen.PersonDetails -> PersonDetailsView(screen.person)
}
}
)
}
}
}
@Composable
fun PersonList(peopleState: State<List<Assignment>>) {
AdapterList(data = peopleState.value) { person ->
PersonView(person, itemClick = { navigateTo(Screen.PersonDetails(it)) })
}
}
val personImages = mapOf(
"Chris Cassidy" to "https://www.nasa.gov/sites/default/files/styles/side_image/public/thumbnails/image/9368855148_f79942efb7_o.jpg?itok=-w5yoryN",
@ -64,23 +76,38 @@ val personImages = mapOf(
)
@Composable
fun PersonView(person: Assignment) {
Row(modifier = Modifier.padding(16.dp) + Modifier.fillMaxWidth(), verticalGravity = Alignment.CenterVertically) {
fun PersonView(person: Assignment, itemClick : (person : Assignment) -> Unit) {
Row(
modifier = Modifier.padding(16.dp) + Modifier.fillMaxWidth()
+ Modifier.clickable(onClick = { itemClick(person) }),
verticalGravity = Alignment.CenterVertically
) {
personImages[person.name]?.let { imageUrl ->
CoilImage(
data = personImages[person.name]!!,
data = imageUrl,
modifier = Modifier.preferredSize(60.dp)
)
} ?: Spacer(modifier = Modifier.preferredSize(60.dp))
Spacer(modifier = Modifier.preferredSize(12.dp))
Column {
Text(text = person.name, style = TextStyle(fontSize = 20.sp))
Text(text = person.craft, style = TextStyle(color = Color.DarkGray, fontSize = 14.sp))
Text(
text = person.craft,
style = TextStyle(color = Color.DarkGray, fontSize = 14.sp)
)
}
}
}
@Composable
fun PersonDetailsView(person: Assignment) {
Text(person.name, style = TextStyle(fontSize = 20.sp))
}
class PersonProvider : CollectionPreviewParameterProvider<Assignment>(
listOf(
Assignment("ISS", "Chris Cassidy"),
@ -92,6 +119,6 @@ class PersonProvider : CollectionPreviewParameterProvider<Assignment>(
@Composable
fun DefaultPreview(@PreviewParameter(PersonProvider::class) person: Assignment) {
MaterialTheme {
PersonView(person)
PersonView(person, itemClick = {})
}
}

View file

@ -0,0 +1,22 @@
package com.surrus.peopleinspace.ui
import androidx.compose.getValue
import androidx.compose.mutableStateOf
import androidx.compose.setValue
import com.surrus.common.remote.Assignment
sealed class Screen {
object Home : Screen()
data class PersonDetails(val person: Assignment) : Screen()
}
object PeopleInSpaceNavigation {
var currentScreen by mutableStateOf<Screen>(Screen.Home)
}
/**
* Temporary solution pending navigation support.
*/
fun navigateTo(destination: Screen) {
PeopleInSpaceNavigation.currentScreen = destination
}

View file

@ -8,8 +8,8 @@ buildscript {
}
dependencies {
classpath("com.android.tools.build:gradle:${Versions.androidBuildToolsVersion}")
classpath(kotlin("gradle-plugin", version = "${Versions.kotlin}"))
classpath(kotlin("serialization", version = "${Versions.kotlin}"))
classpath(kotlin("gradle-plugin", version = Versions.kotlin))
classpath(kotlin("serialization", version = Versions.kotlin))
classpath("com.squareup.sqldelight:gradle-plugin:${Versions.sqlDelight}")
}
}

View file

@ -1,6 +1,6 @@
object Versions {
const val androidBuildToolsVersion = "4.2.0-alpha01"
const val androidBuildToolsVersion = "4.2.0-alpha02"
const val kotlin = "1.3.72"
const val kotlinCoroutines = "1.3.5-native-mt"
@ -17,7 +17,7 @@ object Versions {
const val lifecycle = "2.2.0-alpha01"
const val fragment = "1.1.0-alpha09"
const val compose = "0.1.0-dev13"
const val coliVersion = "0.1.5"
const val coilVersion = "0.1.5"
const val junit = "4.12"
const val coreTesting = "2.0.0"