From 6004793e15a1183773ce86215f337e004b620275 Mon Sep 17 00:00:00 2001 From: Olivier Patry Date: Wed, 15 May 2024 13:30:25 +0200 Subject: [PATCH] Simpler code for rating display in editor app UI --- .../net/opatry/book/editor/component/rating.kt | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/scrapper/book-reading-editor-app/src/main/java/net/opatry/book/editor/component/rating.kt b/scrapper/book-reading-editor-app/src/main/java/net/opatry/book/editor/component/rating.kt index cd2c76a..db02316 100644 --- a/scrapper/book-reading-editor-app/src/main/java/net/opatry/book/editor/component/rating.kt +++ b/scrapper/book-reading-editor-app/src/main/java/net/opatry/book/editor/component/rating.kt @@ -29,27 +29,23 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Star import androidx.compose.material.icons.outlined.StarOutline import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @Composable fun RatingBar(rating: Int, onClick: ((rating: Int) -> Unit)? = null) { + val starOn = remember { Icons.Filled.Star to Color(0xff_ff_dd_33) } + val starOff = remember { Icons.Outlined.StarOutline to MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled) } Row { - repeat(rating) { + repeat(10) { + val (icon, tint) = if (it < rating) starOn else starOff Icon( - Icons.Filled.Star, + icon, null, Modifier.clickable { onClick?.invoke(if (rating == it + 1) 0 else it + 1) }, - tint = Color(0xff_ff_dd_33) - ) - } - repeat(10 - rating) { - Icon( - Icons.Outlined.StarOutline, - null, - Modifier.clickable { onClick?.invoke(rating + it + 1) }, - tint = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled) + tint = tint ) } }