Skip to content

Commit

Permalink
Add capability to define default icon color for entire list
Browse files Browse the repository at this point in the history
  • Loading branch information
shanio committed Jun 7, 2023
1 parent c79cd6e commit 7c2c191
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ private fun ListScreenInner() {
ListItem { Text("My wallet's too small for my fifties.") }
ListItem { Text("And my diamond shoes are too tight!") }
}

Text(
text = "List with custom icon colors",
style = OrbitTheme.typography.title2,
)

List(
defaultIconColor = OrbitTheme.colors.success.normal,
) {
ListItem(icon = Icons.CheckCircle) {
Text("This checks.")
}
ListItem(icon = Icons.Check) {
Text("This is also right.")
}
ListItem(
iconColor = OrbitTheme.colors.critical.normal,
icon = Icons.CloseCircle,
) {
Text("This is wrong!")
}
ListItem(
icon = { Icon(painter = Icons.QuestionCircle, contentDescription = null) },
) {
Text("This is uncertain.")
}
}
}

Column(
Expand Down
163 changes: 154 additions & 9 deletions ui/src/main/java/kiwi/orbit/compose/ui/controls/List.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,46 @@ import kiwi.orbit.compose.ui.foundation.LocalTextStyle
* content = { Text("Personal Item") },
* ),
* ListItem(
* icon = { Icon(painter = Icons.BaggageCabin, contentDescription = null) },
* icon = Icons.BaggageCabin,
* content = { Text("Cabin Baggage") },
* )
* ListItem(
* icon = { Icon(painter = Icons.BaggageChecked20, contentDescription = null) },
* icon = Icons.BaggageChecked20,
* content = { Text("Checked Baggage") },
* )
* }
*
* List with default icon and icon color, that can be overridden by ListItem properties:
*
* ```
* List(
* defaultIconColor = OrbitTheme.colors.primary.normal,
* defaultIcon = Icons.CircleEmpty,
* defaultContentDescription = null,
* ) {
* // Resolves to: default icon, default color, default description.
* ListItem { Text("First item") }
*
* // Resolves to: custom icon and description, default color.
* ListItem(
* icon = Icons.BaggageChecked20,
* contentDescription = null,
* content = { Text("Checked Baggage") },
* )
*
* // Resolves to: custom icon composable, defaults ignored.
* ListItem(
* icon = { Icon(painter = Icons.BaggagePersonal, contentDescription = null, tint = Color.Blue) },
* content = { Text("Personal Item") },
* )
* {
*/
@Composable
public fun List(
modifier: Modifier = Modifier,
contentColor: Color = OrbitTheme.colors.content.normal,
defaultContentDescription: String? = null,
defaultIconColor: Color? = null,
defaultIcon: Painter = Icons.CircleSmall,
verticalArrangement: Arrangement.Vertical = Arrangement.spacedBy(4.dp, Alignment.Top),
content: @Composable ListScope.() -> Unit,
Expand All @@ -70,6 +95,7 @@ public fun List(
ListPrimitive(
contentColor = contentColor,
defaultContentDescription = defaultContentDescription,
defaultIconColor = defaultIconColor,
defaultIcon = defaultIcon,
verticalArrangement = verticalArrangement,
content = content,
Expand Down Expand Up @@ -103,21 +129,47 @@ public fun List(
* content = { Text("Personal Item") },
* ),
* ListItem(
* icon = { Icon(painter = Icons.BaggageCabin, contentDescription = null) },
* icon = Icons.BaggageCabin,
* content = { Text("Cabin Baggage") },
* )
* ListItem(
* icon = { Icon(painter = Icons.BaggageChecked20, contentDescription = null) },
* content = { Text("Checked Baggage") },
* icon = Icons.BaggageChecked20,
* content = { Text("Checked Baggage") },
* )
* }
*
* List with default icon and icon color, that can be overridden by ListItem properties:
*
* ```
* ListLarge(
* defaultIconColor = OrbitTheme.colors.primary.normal,
* defaultIcon = Icons.CircleEmpty,
* defaultContentDescription = null,
* ) {
* // Resolves to: default icon, default color, default description.
* ListItem { Text("First item") }
*
* // Resolves to: custom icon and description, default color.
* ListItem(
* icon = Icons.BaggageChecked20,
* contentDescription = null,
* content = { Text("Checked Baggage") },
* )
*
* // Resolves to: custom icon composable, defaults ignored.
* ListItem(
* icon = { Icon(painter = Icons.BaggagePersonal, contentDescription = null, tint = Color.Blue) },
* content = { Text("Personal Item") },
* )
* {
* ```
*/
@Composable
public fun ListLarge(
modifier: Modifier = Modifier,
contentColor: Color = OrbitTheme.colors.content.normal,
defaultContentDescription: String? = null,
defaultIconColor: Color? = null,
defaultIcon: Painter = Icons.CircleSmall,
verticalArrangement: Arrangement.Vertical = Arrangement.spacedBy(4.dp, Alignment.Top),
content: @Composable ListScope.() -> Unit,
Expand All @@ -128,6 +180,7 @@ public fun ListLarge(
ListPrimitive(
contentColor = contentColor,
defaultContentDescription = defaultContentDescription,
defaultIconColor = defaultIconColor,
defaultIcon = defaultIcon,
verticalArrangement = verticalArrangement,
content = content,
Expand All @@ -145,10 +198,18 @@ public fun ListLarge(
public fun ListScope.ListItem(
modifier: Modifier = Modifier,
icon: @Composable () -> Unit = {
Icon(
painter = this.icon,
contentDescription = this.contentDescription,
)
if (this.iconColor != null) {
Icon(
painter = this.icon,
tint = this.iconColor,
contentDescription = this.contentDescription,
)
} else {
Icon(
painter = this.icon,
contentDescription = this.contentDescription,
)
}
},
content: @Composable () -> Unit,
) {
Expand All @@ -161,10 +222,46 @@ public fun ListScope.ListItem(
}
}

/**
* A single list item with separately customizable icon painter and icon tint.
*
* [icon] override default icon painter provided by [ListScope].
* [contentDescription] override default description provided by [ListScope].
* [iconColor] override default icon color provided by [ListScope]
*/
@Composable
public fun ListScope.ListItem(
modifier: Modifier = Modifier,
icon: Painter = this.icon,
contentDescription: String? = this.contentDescription,
iconColor: Color? = this.iconColor,
content: @Composable () -> Unit,
) {
Row(
modifier = modifier,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
if (iconColor != null) {
Icon(
painter = icon,
tint = iconColor,
contentDescription = contentDescription,
)
} else {
Icon(
painter = icon,
contentDescription = contentDescription,
)
}
content()
}
}

@Composable
private fun ListPrimitive(
contentColor: Color,
defaultContentDescription: String?,
defaultIconColor: Color?,
defaultIcon: Painter,
verticalArrangement: Arrangement.Vertical,
content: @Composable ListScope.() -> Unit,
Expand All @@ -180,6 +277,7 @@ private fun ListPrimitive(
val listScope = remember(defaultContentDescription, defaultIcon) {
ListScope(
contentDescription = defaultContentDescription,
iconColor = defaultIconColor,
icon = defaultIcon,
)
}
Expand All @@ -192,6 +290,7 @@ private fun ListPrimitive(
@Immutable
public class ListScope internal constructor(
public val contentDescription: String?,
public val iconColor: Color?,
public val icon: Painter,
)

Expand Down Expand Up @@ -232,6 +331,29 @@ internal fun ListPreview() {
ListItem { Text("Second thing that went wrong") }
ListItem { Text("Third thing that went wrong") }
}

List(
defaultIconColor = OrbitTheme.colors.success.normal,
defaultIcon = Icons.CheckCircle,
) {
ListItem {
Text("This checks.")
}
ListItem(icon = Icons.Check) {
Text("This is also right.")
}
ListItem(
iconColor = OrbitTheme.colors.critical.normal,
icon = Icons.CloseCircle,
) {
Text("This is wrong!")
}
ListItem(
icon = { Icon(painter = Icons.QuestionCircle, contentDescription = null) },
) {
Text("This is uncertain.")
}
}
}
}
}
Expand Down Expand Up @@ -273,6 +395,29 @@ internal fun ListLargePreview() {
ListItem { Text("Second thing that went well") }
ListItem { Text("Third thing that went well") }
}

ListLarge(
defaultIconColor = OrbitTheme.colors.success.normal,
defaultIcon = Icons.CheckCircle,
) {
ListItem {
Text("This checks.")
}
ListItem(icon = Icons.Check) {
Text("This is also right.")
}
ListItem(
iconColor = OrbitTheme.colors.critical.normal,
icon = Icons.CloseCircle,
) {
Text("This is wrong!")
}
ListItem(
icon = { Icon(painter = Icons.QuestionCircle, contentDescription = null) },
) {
Text("This is uncertain.")
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7c2c191

Please sign in to comment.