Last active
August 27, 2020 21:04
-
-
Save lelandrichardson/99a5d021f4412104fe567612cc44cbfc to your computer and use it in GitHub Desktop.
Understanding Compose - Part 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(appData: AppData) { | |
val derivedData = compute(appData) | |
Header() | |
if (appData.isOwner) { | |
EditButton() | |
} | |
Body { | |
for (item in derivedData.items) { | |
Item(item) | |
} | |
} | |
} |
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 BadgedEnvelope(count: Int) { | |
Envelope(fire=count > 99, paper=count > 0) { | |
if (count > 0) { | |
Badge(text="$count") | |
} | |
} | |
} |
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 bind(liveMsgs: LiveData<MessageData>) { | |
liveMsgs.observe(this) { msgs -> | |
updateBody(msgs) | |
} | |
} |
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 FancyBox(children: @Composable () -> Unit) { | |
Box(fancy) { children() } | |
} | |
@Composable fun Story(…) { /* ... */ } | |
@Composable fun EditForm(...) { /* ... */ } | |
@Composable fun FancyStory(...) { | |
FancyBox { Story(…) } | |
} | |
@Composable fun FancyEditForm(...) { | |
FancyBox { EditForm(...) } | |
} |
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 DateInput(value: DateTime, onChange: (DateTime) -> Unit) { | |
ValidatedInput( | |
value, | |
onChange = { ... onChange(...) }, | |
isValid = isValidDate(value) | |
) | |
} |
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 DateRangeInput(value: DateRange, onChange: (DateRange) -> Unit) { | |
DateInput(value=value.start, ...) | |
DateInput(value=value.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
class Input : View() { /* ... */ } | |
class ValidatedInput : Input() { /* ... */ } | |
class DateInput : ValidatedInput() { /* ... */ } | |
class DateRangeInput : ??? { /* ... */ } |
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
class FancyBox : View() { /* ... */ } | |
class Story : View() { /* ... */ } | |
class EditForm : FormView() { /* ... */ } | |
class FancyStory : ??? { /* ... */ } | |
class FancyEditForm : ??? { /* ... */ } |
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> Input(value: T, onChange: (T) -> 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
@Composable | |
fun Messages(liveMsgs: LiveData<MessageData>) { | |
val msgs by liveMsgs.observeAsState() | |
for (msg in msgs) { | |
Message(msg) | |
} | |
} |
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 updateCount(count: Int) { | |
if (count > 0 && !hasBadge()) { | |
addBadge() | |
} else if (count == 0 && hasBadge()) { | |
removeBadge() | |
} | |
if (count > 99 && !hasFire()) { | |
addFire() | |
setBadgeText("99+") | |
} else if (count <= 99 && hasFire()) { | |
removeFire() | |
} | |
if (count > 0 && !hasPaper()) { | |
addPaper() | |
} else if (count == 0 && hasPaper()) { | |
removePaper() | |
} | |
if (count <= 99) { | |
setBadgeText("$count") | |
} | |
} |
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 ValidatedInput(value: T, onChange: (T) -> Unit, isValid: Boolean) { | |
InputDecoration(color=if(isValid) blue else red) { | |
Input(value, onChange) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment