Skip to content

Instantly share code, notes, and snippets.

@Preview
@Composable
fun Pokeball() {
Container(width = 200.dp, height = 200.dp) {
val children: @Composable() () -> Unit = {
Opacity(opacity = 0.75f) {
DrawImage(
image = +imageResource(R.drawable.pokeball),
tint = +colorResource(R.color.poke_red)
)
@Composable
fun DrawSample() {
val paint = +memo { Paint() }
Container(width = 200.dp, height = 200.dp) {
Draw { canvas, parentSize ->
paint.color = Color.Black
canvas.drawRect(parentSize.toRect(), paint)
}
}
@Composable
fun Pokeball() {
Container(width = 200.dp, height = 200.dp) {
Opacity(opacity = 0.75f) {
DrawImage(
image = +imageResource(R.drawable.pokeball),
tint = +colorResource(R.color.poke_red)
)
}
}
@Composable
fun Pokeball() {
Container(width = 200.dp, height = 200.dp) {
DrawImage(
image = +imageResource(R.drawable.pokeball)
)
}
}
@Composable
fun PhotoAlbum() {
val photos: List<Photo> = emptyList() // TODO
TableRenderer(cols = 4, cellSpacing = 8.dp, items = photos) { cell ->
Text(cell.item.photoDescription)
}
}
sealed class Routing {
object ChildScreen1 : Routing()
object ChildScreen2 : Routing()
data class ChildScreen3(val foo: Bar) : Routing()
}
@Composable
fun MyParentComposable(defaultRouting: Routing) {
BackHandler(defaultRouting) { backStack ->
/**
* Based on https://medium.com/swlh/android-mvi-with-jetpack-compose-b0890f5156ac
* Adapted for 0.1.0-dev04
*/
@Composable
fun <T> observe(data: LiveData<T>): T? {
var result by state { data.value }
val observer = remember { Observer<T> { result = it } }
onCommit(data) {
fun pushAndDropNested(element: T) {
elements.last().children.clear()
push(element)
}
fun replace(element: T) {
elements = elements.dropLast(1) + Entry(element)
}
fun newRoot(element: T) {
elements = listOf(Entry(element))
}
@Composable
// id is a new parameter we'll use for the key in the map:
fun <T> BackHandler(id: Any, routing: T, children: @Composable() (BackStack<T>) -> Unit) {
// grab the parent handler, wherever it's up the tree
val upstream = +ambient(backPressHandler)
// is there a back stack in the upstream? then let's grab the map of the last entry
val saveRestorePoint = upstream.backStack?.lastEntry()?.children
// use existing or fall back to new instance
val backStack = saveRestorePoint?.get(id) ?: BackStack(routing).also {
saveRestorePoint?.set(id, it)