This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun Pokeball() { | |
Container(width = 200.dp, height = 200.dp) { | |
DrawImage( | |
image = +imageResource(R.drawable.pokeball) | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun PhotoAlbum() { | |
val photos: List<Photo> = emptyList() // TODO | |
TableRenderer(cols = 4, cellSpacing = 8.dp, items = photos) { cell -> | |
Text(cell.item.photoDescription) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Routing { | |
object ChildScreen1 : Routing() | |
object ChildScreen2 : Routing() | |
data class ChildScreen3(val foo: Bar) : Routing() | |
} | |
@Composable | |
fun MyParentComposable(defaultRouting: Routing) { | |
BackHandler(defaultRouting) { backStack -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun pushAndDropNested(element: T) { | |
elements.last().children.clear() | |
push(element) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun replace(element: T) { | |
elements = elements.dropLast(1) + Entry(element) | |
} | |
fun newRoot(element: T) { | |
elements = listOf(Entry(element)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |