Created
August 27, 2020 21:11
-
-
Save lelandrichardson/d66859ebfbe3f7c380bb5dfee9f7d30c to your computer and use it in GitHub Desktop.
Understanding Compose - Part 2
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 Address( | |
$composer: Composer, | |
$static: Int, | |
number: Int, street: String, | |
city: String, state: String, zip: String | |
) { | |
Text($composer, ($static and 0b11) and (($static and 0b10) shr 1), "$number $street") | |
Text($composer, ($static and 0b100) shr 2, city) | |
Text($composer, 0b1, ", ") | |
Text($composer, ($static and 0b1000) shr 3, state) | |
Text($composer, 0b1, " ") | |
Text($composer, ($static and 0b10000) shr 4, zip) | |
} |
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 App($composer: Composer) { | |
val result = getData() | |
if (result == null) { | |
$composer.start(123) | |
Loading(...) | |
$composer.end() | |
} else { | |
$composer.start(456) | |
Header(result) | |
Body(result) | |
$composer.end() | |
} | |
} |
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 App() { | |
val result = getData() | |
if (result == null) { | |
Loading(...) | |
} else { | |
Header(result) | |
Body(result) | |
} | |
} |
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 Example(a: () -> Unit, b: @Composable () -> Unit) { | |
a() // allowed | |
b() // NOT allowed | |
} | |
@Composable | |
fun Example(a: () -> Unit, b: @Composable () -> Unit) { | |
a() // allowed | |
b() // allowed | |
} |
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
// function declaration | |
@Composable fun MyFun() { … } | |
// lambda declaration | |
val myLambda = @Composable { … } | |
// function type | |
fun MyFun(myParam: @Composable () -> Unit) { … } |
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 Counter($composer: Composer) { | |
$composer.start(123) | |
var count by remember { mutableStateOf(0) } | |
Button( | |
text="Count: $count", | |
onPress={ count += 1 } | |
) | |
$composer.end() | |
} |
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 Counter($composer: Composer) { | |
$composer.start(123) | |
var count by remember($composer) { mutableStateOf(0) } | |
Button( | |
$composer, | |
text="Count: $count", | |
onPress={ count += 1 }, | |
) | |
$composer.end() | |
} |
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 Counter($composer: Composer) { | |
$composer.start(123) | |
var count = remember($composer) { mutableStateOf(0) } | |
Button( | |
$composer, | |
text="Count: ${count.value}", | |
onPress={ count.value += 1 }, | |
) | |
$composer.end() | |
} |
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 Counter() { | |
var count by remember { mutableStateOf(0) } | |
Button( | |
text="Count: $count", | |
onPress={ count += 1 } | |
) | |
} |
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 App(items: List<String>, query: String) { | |
val results = items.filter { it.matches(query) } | |
// ... | |
} |
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 Google( | |
$composer: Composer, | |
number: Int | |
) { | |
if (number == $composer.next()) { | |
Address( | |
$composer, | |
number=number, | |
street="Amphitheatre Pkwy", | |
city="Mountain View", | |
state="CA" | |
zip="94043" | |
) | |
} else { | |
$composer.skip() | |
} | |
} |
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 Google( | |
$composer: Composer, | |
$static: Int, | |
number: Int | |
) { | |
Address( | |
$composer, | |
0b11110 or ($static and 0b1), | |
number=number, | |
street="Amphitheatre Pkwy", | |
city="Mountain View", | |
state="CA" | |
zip="94043" | |
) | |
} |
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 Google(number: Int) { | |
Address( | |
number=number, | |
street="Amphitheatre Pkwy", | |
city="Mountain View", | |
state="CA" | |
zip="94043" | |
) | |
} | |
@Composable fun Address( | |
number: Int, | |
street: String, | |
city: String, | |
state: String, | |
zip: String | |
) { | |
Text("$number $street") | |
Text(city) | |
Text(", ") | |
Text(state) | |
Text(" ") | |
Text(zip) | |
} |
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 App() { | |
val x = remember { Math.random() } | |
// ... | |
} |
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 <T> remember(vararg inputs: Any?, calculation: () -> T): T |
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 Example(a: () -> Unit, b: suspend () -> Unit) { | |
a() // allowed | |
b() // NOT allowed | |
} | |
suspend | |
fun Example(a: () -> Unit, b: suspend () -> Unit) { | |
a() // allowed | |
b() // allowed | |
} |
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
// function declaration | |
suspend fun MyFun() { … } | |
// lambda declaration | |
val myLambda = suspend { … } | |
// function type | |
fun MyFun(myParam: suspend () -> Unit) { … } |
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
$composer.end()?.updateScope { nextComposer -> | |
Counter(nextComposer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment