Skip to content

Instantly share code, notes, and snippets.

@lucanicoletti
Created September 1, 2019 10:01
Show Gist options
  • Save lucanicoletti/039d2fe76e19e859582e32625ac411e7 to your computer and use it in GitHub Desktop.
Save lucanicoletti/039d2fe76e19e859582e32625ac411e7 to your computer and use it in GitHub Desktop.
Working Example
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.ui.material.studies.rally
import android.app.Activity
import android.os.Bundle
import androidx.compose.Composable
import androidx.compose.composer
import androidx.compose.Model
import androidx.compose.memo
import androidx.compose.state
import androidx.compose.unaryPlus
import androidx.ui.core.Text
import androidx.ui.core.dp
import androidx.ui.core.setContent
import androidx.ui.foundation.VerticalScroller
import androidx.ui.input.EditorModel
import androidx.ui.layout.Column
import androidx.ui.layout.FlexSize
import androidx.ui.layout.HeightSpacer
import androidx.ui.layout.MainAxisAlignment
import androidx.ui.layout.Padding
import androidx.ui.layout.Row
import androidx.ui.material.Button
import androidx.ui.material.Checkbox
import androidx.ui.material.studies.Scaffold
import androidx.ui.material.themeTextStyle
/**
* This Activity recreates the Rally Material Study from
* https://material.io/design/material-studies/rally.html
*/
class RallyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
RallyApp()
}
}
@Composable
fun RallyApp() {
RallyTheme {
Scaffold(appBar = { RallyAppBar() }) {
TaskList()
}
}
}
@Composable
fun RallyAppBar() {
// TODO: Transform to tabs
Row {
// Icon()
Text(text = "Overview", style = +themeTextStyle{ h4 })
// TODO: Other items
}
}
}
@Model
class TodoList(var counter: Int = 0, var todoList: MutableList<TaskModel>)
@Composable
fun TaskList() {
val count = +memo { TodoList(0, mutableListOf()) }
VerticalScroller {
Column(crossAxisSize = FlexSize.Max) {
Button(text = "Add", onClick = {
count.counter++
count.todoList.add(TaskModel(false, "1"))
})
count.todoList.sortedBy {
it.isDone
}.forEach {
Task(it)
}
}
}
}
@Composable
fun Task(taskModel: TaskModel) {
Row(mainAxisAlignment = MainAxisAlignment.SpaceEvenly, mainAxisSize = FlexSize.Max) {
Checkbox(taskModel.isDone, onCheckedChange = { taskModel.isDone = it })
Text(text = taskModel.description)
}
}
@Model
data class TaskModel(var isDone: Boolean, val description: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment