From 25a730f86bf326ed51b19ca37e823dff004847db Mon Sep 17 00:00:00 2001 From: Acclorite <140836141+Acclorite@users.noreply.github.com> Date: Thu, 15 Aug 2024 18:04:38 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Add=20option=20to=20display=20cu?= =?UTF-8?q?stom=20placeholder=20for=20Slider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added option to display custom placeholder value for SliderWithTitle. You can use it to specify default values like "Default" when value is "1". --- .../settings/components/SliderWithTitle.kt | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ua/acclorite/book_story/presentation/screens/settings/components/SliderWithTitle.kt b/app/src/main/java/ua/acclorite/book_story/presentation/screens/settings/components/SliderWithTitle.kt index 665ad0bd..69246694 100644 --- a/app/src/main/java/ua/acclorite/book_story/presentation/screens/settings/components/SliderWithTitle.kt +++ b/app/src/main/java/ua/acclorite/book_story/presentation/screens/settings/components/SliderWithTitle.kt @@ -14,6 +14,7 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import ua.acclorite.book_story.presentation.components.CategoryTitle @@ -21,11 +22,24 @@ import kotlin.math.roundToInt /** * Slider with title. + * + * @param modifier Modifier. + * @param value Value to be displayed, in pair with [String] which is the type of value(e.g. "pt") + * @param valuePlaceholder Placeholder of the value(e.g. "Default", instead of the "1pt") + * @param showPlaceholder Whether placeholder should be shown. + * @param fromValue From where user can pick value. + * @param toValue To where user can pick value. + * @param title Title of this slider. + * @param horizontalPadding Horizontal item padding. + * @param verticalPadding Vertical item padding. + * @param onValueChange Callback when value changes. */ @Composable fun SliderWithTitle( modifier: Modifier = Modifier, value: Pair, + valuePlaceholder: String = "", + showPlaceholder: Boolean = false, fromValue: Int, toValue: Int, title: String, @@ -43,9 +57,12 @@ fun SliderWithTitle( CategoryTitle(title = title, padding = 0.dp) Spacer(modifier = Modifier.height(4.dp)) Text( - text = "${value.first}${value.second}", + text = if (!showPlaceholder) "${value.first}${value.second}" + else valuePlaceholder, style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis ) } Spacer(modifier = Modifier.width(8.dp)) @@ -64,10 +81,25 @@ fun SliderWithTitle( } } +/** + * Slider with title. + * + * @param modifier Modifier. + * @param value Value to be displayed, in pair with [String] which is the type of value(e.g. "pt") + * @param valuePlaceholder Placeholder of the value(e.g. "Default", instead of the "1pt") + * @param showPlaceholder Whether placeholder should be shown. + * @param toValue To where user can pick value. + * @param title Title of this slider. + * @param horizontalPadding Horizontal item padding. + * @param verticalPadding Vertical item padding. + * @param onValueChange Callback when value changes. + */ @Composable fun SliderWithTitle( modifier: Modifier = Modifier, value: Pair, + valuePlaceholder: String = "", + showPlaceholder: Boolean = false, title: String, toValue: Int, horizontalPadding: Dp = 18.dp, @@ -84,9 +116,12 @@ fun SliderWithTitle( CategoryTitle(title = title, padding = 0.dp) Spacer(modifier = Modifier.height(4.dp)) Text( - text = "${(value.first * toValue).roundToInt()}${value.second}", + text = if (!showPlaceholder) "${(value.first * toValue).roundToInt()}${value.second}" + else valuePlaceholder, style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis ) } Spacer(modifier = Modifier.width(8.dp))