From 0cd8c6a87e836abfb49f5500031872a950ae41b7 Mon Sep 17 00:00:00 2001 From: WhiteScent Date: Fri, 19 Jan 2024 12:54:01 +0800 Subject: [PATCH] feat: text limit background (#16) * feat: text limit background * chore: refactor function location --- .../whitescent/mastify/screen/post/Post.kt | 28 ++++++++++++++++++- .../whitescent/mastify/ui/theme/AppTheme.kt | 1 - .../whitescent/mastify/ui/theme/Colors.kt | 4 +++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/whitescent/mastify/screen/post/Post.kt b/app/src/main/java/com/github/whitescent/mastify/screen/post/Post.kt index dda34311..302531b5 100644 --- a/app/src/main/java/com/github/whitescent/mastify/screen/post/Post.kt +++ b/app/src/main/java/com/github/whitescent/mastify/screen/post/Post.kt @@ -66,6 +66,7 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.hilt.navigation.compose.hiltViewModel @@ -198,7 +199,32 @@ fun Post( } } BasicTextField( - value = postTextField, + value = postTextField.copy( + annotatedString = buildAnnotatedString { + val text = postTextField.text + val maxInstanceText = state.instance?.maximumTootCharacters ?: DEFAULT_CHARACTER_LIMIT + withStyle( + style = SpanStyle(fontSize = 18.sp, color = AppTheme.colors.primaryContent) + ) { + append( + text = text.substring( + startIndex = 0, + endIndex = if (text.length <= maxInstanceText) text.length else maxInstanceText + ) + ) + } + if (text.length > maxInstanceText) { + withStyle( + style = SpanStyle( + color = AppTheme.colors.primaryContent, + background = AppTheme.colors.textLimitWarningBackground + ) + ) { + append(text.substring(startIndex = maxInstanceText, endIndex = text.length)) + } + } + } + ), onValueChange = viewModel::updateTextFieldValue, modifier = Modifier .fillMaxSize() diff --git a/app/src/main/java/com/github/whitescent/mastify/ui/theme/AppTheme.kt b/app/src/main/java/com/github/whitescent/mastify/ui/theme/AppTheme.kt index 88f44612..22d9605a 100644 --- a/app/src/main/java/com/github/whitescent/mastify/ui/theme/AppTheme.kt +++ b/app/src/main/java/com/github/whitescent/mastify/ui/theme/AppTheme.kt @@ -22,7 +22,6 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.ReadOnlyComposable object AppTheme { - val colors: MastifyColorScheme @Composable @ReadOnlyComposable diff --git a/app/src/main/java/com/github/whitescent/mastify/ui/theme/Colors.kt b/app/src/main/java/com/github/whitescent/mastify/ui/theme/Colors.kt index e2c59bde..782189b6 100644 --- a/app/src/main/java/com/github/whitescent/mastify/ui/theme/Colors.kt +++ b/app/src/main/java/com/github/whitescent/mastify/ui/theme/Colors.kt @@ -54,6 +54,7 @@ interface MastifyColorScheme { val exploreSearchBarBorder: Color val exploreSearchBarBackground: Color val pollOptionBackground: Color + val textLimitWarningBackground: Color val isLight: Boolean } @@ -88,6 +89,7 @@ open class ColorSchemeImpl( override val exploreSearchBarBorder: Color, override val exploreSearchBarBackground: Color, override val pollOptionBackground: Color, + override val textLimitWarningBackground: Color, override val isLight: Boolean, ) : MastifyColorScheme @@ -117,6 +119,7 @@ object LightColorScheme : ColorSchemeImpl( exploreSearchBarBorder = Color(0xFFF1F1F1), exploreSearchBarBackground = Color.White, pollOptionBackground = Color(0xFFECEEF0), + textLimitWarningBackground = Color(0xFFf8d3de), isLight = true ) @@ -146,6 +149,7 @@ object DarkColorScheme : ColorSchemeImpl( exploreSearchBarBorder = Color(0xFF222222), exploreSearchBarBackground = Color(0xFF1D1D1D), pollOptionBackground = Color(0xFF232323), + textLimitWarningBackground = Color(0xFFf3222d), isLight = false )