Share
Embed
Transcript
145 slides
-
@dev_brian Composing an API the way Brian Wangi Senior Software
Engineer @intellectsoft -
Agenda › Naming › Parameters › Default values › Modifiers
› Slots › State handling -
Constants const val DefaultKeyName = " defaultKey" val StructurallyEqual: ComparisonPolicy
= StructurallyEqualsImpl() -
Constants const val DefaultKeyName = " defaultKey" val StructurallyEqual: ComparisonPolicy
= StructurallyEqualsImpl() object ReferenceEqual : ComparisonPolicy {} sealed class LoadResult<T> { object Loading : LoadResult<Nothing>() class Done<T>(val result: T) : LoadResult<T>() class Error(val cause: Throwable) : LoadResult<Nothing>() } -
Constants const val DefaultKeyName = " defaultKey" val StructurallyEqual: ComparisonPolicy
= StructurallyEqualsImpl() object ReferenceEqual : ComparisonPolicy {} sealed class LoadResult<T> { object Loading : LoadResult<Nothing>() class Done<T>(val result: T) : LoadResult<T>() class Error(val cause: Throwable) : LoadResult<Nothing>() } enum class Status { Idle, Busy, } -
Functions Return a value Emit content @Composable fun Button(...) @Composable
fun Column(...) @Composable fun LaunchedEffect(...) -
Functions Return a value @Composable fun stringResource( @StringRes id: Int
): String @Composable fun rememberScrollState( initial: Int = 0 ): ScrollState Emit content @Composable fun Button(...) @Composable fun Column(...) @Composable fun LaunchedEffect(...) -
Functions @Composable fun ColorScheme(darkTheme: Boolean): ColorScheme @Composable fun colorScheme(darkTheme: Boolean):
ColorScheme @Composable fun CoroutineScope(): CoroutineScope @Composable fun rememberCoroutineScope(): CoroutineScope -
Component /kəmˈpəʊnənt/ noun a @Composable function that returns Unit and
emits exactly one Compose UI tree node. -
Component /kəmˈpəʊnənt/ noun a @Composable function that returns Unit and
exactly one emits Compose UI tree node. -
Components @Composable fun Header() { Column { Text("Kotlin programming language")
Text("Concise. Cross-platform. Fun.") Row { Button { Text("Docs") } Button { Text("Blog") } } } } -
Components @Composable fun Header() { Column { Text("Kotlin programming language")
Text("Concise. Cross-platform. Fun.") Row { Button { Text("Docs") } Button { Text("Blog") } } } } -
Components @Composable fun Header() { Column { Text("Kotlin programming language")
Text("Concise. Cross-platform. Fun.") Row { Button { Text("Docs") } Button { Text("Blog") } } } } -
Components Button { Text("Docs") } Button { Text("Blog") } @Composable
fun Header() { Column { Row { } } } @Composable fun Title() { Text("Kotlin programming language") Text("Concise. Cross-platform. Fun.") } -
Components @Composable fun Header() { Column { Title() Row {
Button { Text("Docs") } Button { Text("Blog") } } } } @Composable fun Title() { Text("Kotlin programming language") Text("Concise. Cross-platform. Fun.") } -
Components @Composable fun Header() { Column { Title() Row {
Button { Text("Docs") } Button { Text("Blog") } } } } @Composable fun Title() { Text("Kotlin programming language") Text("Concise. Cross-platform. Fun.") } -
Components CKoť⭲lci⭲ispcí.oCgííoasms m-pil⭲agťrolaí⭲mg."Ia"g⭲c. @Composable fun Title() { Text("Kotlin programming language")
Text("Concise. Cross-platform. Fun.") } -
Components CKoť⭲lci⭲ispcí.oCgííoasms m-pil⭲agťrolaí⭲mg."Ia"g⭲c. @Composable fun Title() { Column { Text("Kotlin
programming language") Text("Concise. Cross-platform. Fun.") } } -
Components Koťli⭲ píogíammi⭲g la⭲g"agc Co⭲cisc. Cíoss-plaťroím. I"⭲. @Composable fun Title()
{ Column { Text("Kotlin programming language") Text("Concise. Cross-platform. Fun.") } } -
Component /kəmˈpəʊnənt/ noun a @Composable function that returns Unit and
emits exactly one Compose UI tree node. -
Component /kəmˈpəʊnənt/ noun a @Composable function that returns Unit and
emits exactly one Compose UI tree node. -
Components @Composable fun InputField(): UserInputState { // ... } val
inputState = InputField() Button(onClick = { inputState.value = "" }) { Text("Clear input") } -
Components @Composable fun InputField(): UserInputState { // ... } Button(onClick
= { inputState.value = "" }) { Text("Clear input") } val inputState = InputField() -
Components @Composable fun InputField(): UserInputState { // ... } val
inputState = InputField() Button(onClick = { inputState.value = "" }) { Text("Clear input") } -
Components @Composable fun InputField(inputState: UserInputState) { // ... } val
inputState = remember { UserInputState() } InputField ( inputState ) Button(onClick = { inputState.value = "" }) { Text("Clear input") } -
Components @Composable fun InputField(inputState: UserInputState) { // ... } val
inputState = remember { UserInputState() } InputField(inputState) Button(onClick = { inputState.value = "" }) { Text("Clear input") } -
Components @Composable fun InputField(inputState: UserInputState) { // ... } val
inputState = remember { UserInputState() } InputField(inputState) Button(onClick = { inputState.value = "" }) { Text("Clear input") } -
Components @Composable fun InputField(inputState: UserInputState) { // ... } val
inputState = remember { UserInputState() } Button(onClick = { inputState.value = "" }) { Text("Clear input") } InputField(inputState) -
Default arguments @Composable fun Text( text: String, modifier: Modifier =
Modifier, color: Color = Color.Unspecified, lineHeight: TextUnit = TextUnit.Unspecified, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, maxLines: Int = Int.MAX_VALUE, onTextLayout: (TextLayoutResult) -> Unit = {}, style: TextStyle = LocalTextStyle.current, ) -
Default arguments @Composable fun Text( text: String, modifier: Modifier =
Modifier, color: Color = Color.Unspecified, lineHeight: TextUnit = TextUnit.Unspecified, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, maxLines: Int = Int.MAX_VALUE, onTextLayout: (TextLayoutResult) -> Unit = {}, style: TextStyle = LocalTextStyle.current, ) -
Default arguments @Composable fun Text( text: String, modifier: Modifier =
Modifier, color: Color = Color.Unspecified, lineHeight: TextUnit = TextUnit.Unspecified, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, maxLines: Int = Int.MAX_VALUE, onTextLayout: (TextLayoutResult) -> Unit = {}, style: TextStyle = LocalTextStyle.current, ) -
Default arguments @Composable fun Text( text: String, modifier: Modifier =
Modifier, color: Color = Color.Unspecified, lineHeight: TextUnit = TextUnit.Unspecified, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, maxLines: Int = Int.MAX_VALUE, onTextLayout: (TextLayoutResult) -> Unit = {}, style: TextStyle? = null, ) { val actualStyle = style ?: LocalTextStyle.current // ... } -
Default arguments @Composable fun Button( onClick: () -> Unit, modifier:
Modifier = Modifier, enabled: Boolean = true, shape: Shape = ButtonDefaults.shape, colors: ButtonColors = ButtonDefaults.buttonColors(), elevation: ButtonElevation? = ButtonDefaults.buttonElevation(), border: BorderStroke? = null, contentPadding: PaddingValues = ButtonDefaults.ContentPadding, content: @Composable RowScope.() -> Unit, ) -
Default arguments @Composable fun Button( onClick: () -> Unit, modifier:
Modifier = Modifier, enabled: Boolean = true, shape: Shape = ButtonDefaults.shape, colors: ButtonColors = ButtonDefaults.buttonColors(), elevation: ButtonElevation? = ButtonDefaults.buttonElevation(), border: BorderStroke? = null, contentPadding: PaddingValues = ButtonDefaults.ContentPadding, content: @Composable RowScope.() -> Unit, ) -
Default arguments object ButtonDefaults { val ContentPadding: PaddingValues val MinWidth:
Dp val MinHeight: Dp val IconSize: Dp val IconSpacing: Dp val shape: Shape @Composable get @Composable fun buttonColors(...): ButtonColors = ButtonColors(...) @Composable fun buttonElevation(...): ButtonElevation = ButtonElevation(...) } -
Default arguments object ButtonDefaults { @Composable fun buttonColors(...): ButtonColors =
ButtonColors(...) } Button( onClick = { viewModel.savePage() }, colors = customColors ?: ButtonDefaults.buttonColors(), ) { Text("Save") } -
Default arguments internal object ButtonDefaults { @Composable fun buttonColors(...): ButtonColors
= ButtonColors(...) } Button( onClick = { viewModel.savePage() }, colors = customColors ?: ButtonDefaults.buttonColors(), ) { Text("Save") } -
Modifiers @Composable fun PageTitle( text: String, modifier: Modifier = Modifier,
) PageTitle( text = "Home", modifier = Modifier .width(100.dp) .height(40.dp) .background(Color.LightGray) .padding(horizontal = 12.dp) ) -
Modifiers @Composable fun PageTitle( text: String, modifier: Modifier = Modifier,
padding: Dp = 0.dp, ) PageTitle( text = "Home", modifier = Modifier .width(100.dp) .height(40.dp) .background(Color.LightGray) .padding(horizontal = 12.dp) ) -
Modifiers @Composable fun PageTitle( text: String, ) Box( Modifier .padding(12.dp)
.background(Color.Blue) PageTitle("Home") ) { } -
Modifiers @Composable fun PageTitle( text: String, ) Box( Modifier .padding(12.dp)
.background(Color.Blue) PageTitle("Home") ) { } ) @Composable fun PageTitle( text: String, modifier: Modifier = Modifier, PageTitle( "Home", Modifier .padding(12.dp) .background(Color.Blue), ) -
Modifiers › Accept a Modifier parameter › Named modifier ›
First optional parameter -
Modifiers › Accept a Modifier parameter › Named modifier ›
First optional parameter › Applied to the root node -
Modifiers › Accept a Modifier parameter › Named modifier ›
First optional parameter › Applied to the root node › Default value Modifier -
Modifiers › Accept a Modifier parameter › Named modifier ›
First optional parameter › Applied to the root node › Default value Modifier -
Modifiers › Accept a Modifier parameter › Named modifier ›
First optional parameter › Applied to the root node › Default value Modifier -
Modifiers › Accept a Modifier parameter › Named modifier ›
First optional parameter › Applied to the root node › Default value Modifier -
Modifiers @Composable fun Header( modifier: Modifier = Modifier, ) {
Column(modifier) { Title() Row { Button(onClick = { ... }) { Text("Docs") } Button(onClick = { ... }) { Text("Blog") } } } } -
Modifiers @Composable fun Header( modifier: Modifier = Modifier, ) {
Column { Title() Row(modifier) { Button(onClick = { ... }) { Text("Docs") } Button(onClick = { ... }) { Text("Blog") } } } } -
Modifiers @Composable fun Header( modifier: Modifier = Modifier, ) {
Column(modifier) { Title() Row { Button(onClick = { ... }) { Text("Docs") } Button(onClick = { ... }) { Text("Blog") } } } } -
Modifiers @Composable fun MenuItems( smallScreen: Boolean, modifier: Modifier = Modifier,
) { if (smallScreen) { Column(modifier) { Text(...) Text(...) } } else { Row(modifier) { Text(...) Text(...) } } } -
Modifiers › Accept a Modifier parameter › Named modifier ›
First optional parameter › Applied to the root node › Default value Modifier -
Modifiers modifier: Modifier = Modifier interface Modifier { infix fun
then(other: Modifier): Modifier = CombinedModifier(this, other) companion object : Modifier { ... override infix fun then(other: Modifier): Modifier = other override fun toString() = "Modifier" } } -
Modifiers @Composable fun Topics( topic: List<Topic>, modifier: Modifier = Modifier,
) { Row( modifier = modifier.padding(12.dp), ) { // ... } } -
Modifiers @Composable fun Topics( topic: List<Topic>, modifier: Modifier = Modifier,
) { Row( modifier = Modifier.padding(12.dp).then(modifier), ) { // ... } } -
Slots @Composable fun Button( content: @Composable () -> Unit, )
Button { Row { Icon(Icons.Default.Build, "Build" ) Text("Build project" ) } } -
Slots Button { this: RowScope @Composable fun Button( content: @Composable
RowScope.() -> Unit, ) { Row { content() } } Icon(Icons.Default.Build, "Build") Text("Build project" ) } -
Slots Button { this: RowScope @Composable fun Button( content: @Composable
RowScope.() -> Unit, ) { Row { content() } } Icon(Icons.Default.Build, "Build") Text("Build project" ) } -
Slots Button { this: RowScope @Composable fun Button( content: @Composable
RowScope.() -> Unit, ) { Row { content() } } Icon(Icons.Default.Build, "Build", Modifier.weight(1f)) Text("Build project", Modifier.weight(4f) ) } -
Slots @Composable fun Button( content: @Composable RowScope.() -> Unit, )
{ Row { content() } } Button { this: RowScope Icon(Icons.Default.Build, "Build", Modifier.weight(1f)) Text("Build project", Modifier.weight(4f)) } -
Slot lifecycle @Composable fun PreferenceItem( checked: Boolean, content: @Composable ()
-> Unit, ) { if (checked) { Row { Text("Checked") content() } } else { Column { Text("Unchecked") content() } } } -
Slot lifecycle @Composable fun PreferenceItem( checked: Boolean, content: @Composable ()
-> Unit, ) { if (checked) { Row { Text("Checked") content() } } else { Column { Text("Unchecked") content() } } } -
Slot lifecycle @Composable fun PreferenceItem( checked: Boolean, content: @Composable ()
-> Unit, ) { if (checked) { Row { Text("Checked") content() } } else { Column { Text("Unchecked") content() } } } PreferenceItem -
Slot lifecycle @Composable fun PreferenceItem( checked: Boolean, content: @Composable ()
-> Unit, ) { if (checked) { Row { Text("Checked") content() } } else { Column { Text("Unchecked") content() } } } PreferenceItem content -
Slot lifecycle @Composable fun PreferenceItem( checked: Boolean, content: @Composable ()
-> Unit, ) { if (checked) { Row { Text("Checked") content() } } else { Column { Text("Unchecked") content() } } } checked = false checked = true checked = false PreferenceItem content -
Slot lifecycle @Composable fun PreferenceItem( checked: Boolean, content: @Composable ()
-> Unit, ) { val movableContent = remember(content) { movableContentOf(content) } if (checked) { Row { Text("Checked") movableContent() } } else { Column { Text("Unchecked") movableContent() -
Slot lifecycle @Composable fun PreferenceItem( checked: Boolean, content: @Composable ()
-> Unit, ) { val movableContent = remember(content) { movableContentOf(content) } if (checked) { Row { Text("Checked") movableContent() } } else { Column { Text("Unchecked") movableContent() jb.gg/movable-content-o f -
Ordering parameters › Required parameters › Optional parameters › First
one: Modifier › Trailing content lambda -
Ordering parameters › Required parameters › Optional parameters › First
one: Modifier › Trailing content lambda @Composable fun NiaTopicTag( modifier: Modifier = Modifier, followed: Boolean, onClick: () -> Unit, enabled: Boolean = true, text: @Composable () -> Unit, ) -
Ordering parameters › Required parameters › Optional parameters › First
one: Modifier › Trailing content lambda @Composable fun NiaTopicTag( modifier: Modifier = Modifier, followed: Boolean, onClick: () -> Unit, enabled: Boolean = true, text: @Composable () -> Unit, ) -
Ordering parameters NiaTopicTag(true, { toggleFollowed(id) }) { Text("Compose") } ›
Required parameters › Optional parameters › First one: Modifier › Trailing content lambda ) @Composable fun NiaTopicTag( modifier: Modifier = Modifier, followed: Boolean, onClick: () -> Unit, enabled: Boolean = true, text: @Composable () -> Unit, -
Ordering parameters › Required parameters › Optional parameters › First
one: Modifier › Trailing content lambda ) NiaTopicTag(true, { toggleFollowed(id) }) { Text("Compose") } @Composable fun NiaTopicTag( modifier: Modifier = Modifier, followed: Boolean, onClick: () -> Unit, enabled: Boolean = true, text: @Composable () -> Unit, -
Ordering parameters › Required parameters › Optional parameters › First
one: Modifier › Trailing content lambda ) NiaTopicTag(true, { toggleFollowed(id) }) { Text("Compose") } @Composable fun NiaTopicTag( followed: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, text: @Composable () -> Unit, -
Ordering parameters › Required parameters › Optional parameters › First
one: Modifier › Trailing content lambda ) NiaTopicTag(followed = true, onClick = { toggleFollowed(id) }) { Text("Compose") } @Composable fun NiaTopicTag( followed: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, text: @Composable () -> Unit, -
Ordering parameters › Required parameters › Optional parameters › First
one: Modifier › Trailing content lambda ) NiaTopicTag(true, { toggleFollowed(id) }) { Text("Compose") } @Composable fun NiaTopicTag( followed: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, text: @Composable () -> Unit, -
Ordering parameters ) › Required parameters › Optional parameters ›
First one: Modifier › Trailing content lambda NiaTopicTag(true, { toggleFollowed(id) }, Modifier.padding(8.dp)) { Text("Compose") } @Composable fun NiaTopicTag( followed: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, text: @Composable () -> Unit, -
Ordering parameters NiaTopicTag(true, { toggleFollowed(id) }, Modifier.padding(8.dp)) { Text("Compose") }
) › Required parameters › Optional parameters › First one: Modifier › Trailing content lambda @Composable fun NiaTopicTag( followed: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, text: @Composable () -> Unit, -
Ordering parameters NiaTopicTag(true, { toggleFollowed(id) }, Modifier.padding(8.dp)) { Text("Compose") }
) › Required parameters › Optional parameters › First one: Modifier › Trailing content lambda @Composable fun NiaTopicTag( followed: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, content: @Composable () -> Unit, -
@Composable fun Scroller( offset: State<Float>, onOffsetChange: (Float) -> Unit, )
@Composable fun ContextMenu(offset: Float, onOffsetChange: (Float) -> Unit) { Row { MenuItems() Scroller(offset, onOffsetChange) } } State -
@Composable fun Scroller( offset: State<Float>, onOffsetChange: (Float) -> Unit, )
@Composable fun ContextMenu(offset: Float, onOffsetChange: (Float) -> Unit) { val offsetState = remember { mutableStateOf(offset) } Row { MenuItems() Scroller(offsetState, onOffsetChange) } } State -
@Composable fun Scroller( offset: () -> Float, onOffsetChange: (Float) ->
Unit, ) // Constant Scroller(offset = { 0f }, ...) // Plain value Scroller(offset = { offset }, ...) // State Scroller(offset = { offsetState.value }, ...) // Value from an object Scroller(offset = { someObject.offset }, ...) State -
State @Composable fun Scroller( offset: () -> Float, onOffsetChange: (Float)
-> Unit, ) // Constant Scroller(offset = { 0f }, ...) // Plain value Scroller(offset = { offset }, ...) // State Scroller(offset = { offsetState.value }, ...) // Value from an object Scroller(offset = { someObject.offset }, ...) jb.gg/debugging-recompositio n -
State holders @Composable fun VerticalScroller( scrollPosition: Int, scrollRange: Int, onScrollPositionChange:
(Int) -> Unit, onScrollRangeChange: (Int) -> Unit, ) -
State holders @Composable fun VerticalScroller( ) @Stable interface VerticalScrollerState {
var scrollPosition: Int var scrollRange: Int } -
State holders @Composable fun VerticalScroller( verticalScrollerState: VerticalScrollerState ) @Stable interface
VerticalScrollerState { var scrollPosition: Int var scrollRange: Int } -
State holders @Composable fun VerticalScroller( verticalScrollerState: VerticalScrollerState ) @Stable interface
VerticalScrollerState { var scrollPosition: Int var scrollRange: Int } -
State holders jb.gg/compose-stabilit y @Composable fun VerticalScroller( verticalScrollerState: VerticalScrollerState )
@Stable interface VerticalScrollerState { var scrollPosition: Int var scrollRange: Int } -
State holders @Composable fun VerticalScroller( verticalScrollerState: VerticalScrollerState ) class VerticalScrollerStateImpl(
scrollPosition: Int = 0, scrollRange: Int = 0, ) : VerticalScrollerState { override var scrollPosition: Int by mutableStateOf(scrollPosition) override var scrollRange: Int by mutableStateOf(scrollRange) } -
State holders @Composable fun VerticalScroller( verticalScrollerState: VerticalScrollerState ) fun VerticalScrollerState():
VerticalScrollerState = VerticalScrollerStateImpl() private class VerticalScrollerStateImpl( scrollPosition: Int = 0, scrollRange: Int = 0, ) : VerticalScrollerState { override var scrollPosition: Int by mutableStateOf(scrollPosition) override var scrollRange: Int by mutableStateOf(scrollRange) } -
State holders @Composable fun VerticalScroller( verticalScrollerState: VerticalScrollerState = remember {
VerticalScrollerState() } ) fun VerticalScrollerState(): VerticalScrollerState = VerticalScrollerStateImpl() private class VerticalScrollerStateImpl( scrollPosition: Int = 0, scrollRange: Int = 0, ) : VerticalScrollerState { override var scrollPosition: Int by mutableStateOf(scrollPosition) override var scrollRange: Int by mutableStateOf(scrollRange) } -
State holders @Composable fun VerticalScroller( verticalScrollerState: VerticalScrollerState = remember {
VerticalScrollerState() } ) fun VerticalScrollerState(): VerticalScrollerState = VerticalScrollerStateImpl() @Stable interface VerticalScrollerState { var scrollPosition: Int var scrollRange: Int } private class VerticalScrollerStateImpl ( scrollPosition: Int = 0, scrollRange: Int = 0, ) : VerticalScrollerState { override var scrollPosition: Int by mutableStateOf(scrollPosition) -
State holders @Stable class VerticalScrollerState { var scrollPosition: Int by
mutableStateOf(0) var scrollRange: Int by mutableStateOf(0) } -
State holders @Stable class VerticalScrollerState { var scrollPosition: Int by
mutableStateOf(0) var scrollRange: Int by mutableStateOf(0) } -
Compose Multiplatform 🞄 jb.gg/compose Guidelines 🞄 jb.gg/compose-api-guidelines 🞄 jb.gg/compose-component-api- guidelines
In order of appearance 🞄 jb.gg/composition-locals 🞄 jb.gg/compose-modifiers 🞄 jb.gg/movable-content-of 🞄 jb.gg/debugging-recomposition 🞄 jb.gg/compose-stability 🞄 jb.gg/compose-rules Composing an API the Right way Brian Wangi @dev_brian