diff --git a/app/src/main/java/com/tangem/tap/features/home/compose/AutoSizeText.kt b/app/src/main/java/com/tangem/tap/features/home/compose/AutoSizeText.kt index a7d9b2b606..2bb6c5b8d5 100644 --- a/app/src/main/java/com/tangem/tap/features/home/compose/AutoSizeText.kt +++ b/app/src/main/java/com/tangem/tap/features/home/compose/AutoSizeText.kt @@ -1,45 +1,68 @@ package com.tangem.tap.features.home.compose +import androidx.compose.animation.ExperimentalAnimationApi +import androidx.compose.material.LocalTextStyle import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawWithContent -import androidx.compose.ui.text.TextLayoutResult import androidx.compose.ui.text.TextStyle -import androidx.compose.ui.unit.ExperimentalUnitApi +import androidx.compose.ui.unit.TextUnit +import androidx.compose.ui.unit.sp -@OptIn(ExperimentalUnitApi::class) +@OptIn(ExperimentalAnimationApi::class) @Composable -fun AutoSizeText( +fun TextAutoSize( + modifier: Modifier = Modifier, text: String, - textStyle: TextStyle, - modifier: Modifier = Modifier + textStyle: TextStyle = LocalTextStyle.current, + fontSizeRange: FontSizeRange, ) { - val scaledTextStyle = remember { mutableStateOf(textStyle) } + val fontSizeValue = remember { mutableStateOf(fontSizeRange.max.value) } val readyToDraw = remember { mutableStateOf(false) } - val onTextLayout: (TextLayoutResult) -> Unit = { result -> - if (result.didOverflowWidth) { - val fontSize = result.size.width / result.multiParagraph.width - scaledTextStyle.value = -// scaledTextStyle.value.copy(fontSize = TextUnit(fontSize, TextUnitType.Sp)) - scaledTextStyle.value.copy(fontSize = scaledTextStyle.value.fontSize * 0.9) - } else { - readyToDraw.value = true - } + val textState = remember { mutableStateOf(text) } + if (textState.value != text) { + readyToDraw.value = false + fontSizeValue.value = fontSizeRange.max.value + textState.value = text } Text( + modifier = modifier.drawWithContent { if (readyToDraw.value) drawContent() }, text = text, - modifier = modifier.drawWithContent { - if (readyToDraw.value) { - drawContent() - } - }, softWrap = false, - onTextLayout = onTextLayout, - style = scaledTextStyle.value, - ) + style = textStyle, + fontSize = fontSizeValue.value.sp, + onTextLayout = { + if (it.hasVisualOverflow) { + val nextFontSizeValue = fontSizeValue.value - fontSizeRange.step.value + if (nextFontSizeValue <= fontSizeRange.min.value) { + fontSizeValue.value = fontSizeRange.min.value + readyToDraw.value = true + } else { + fontSizeValue.value = nextFontSizeValue * 0.8f + } + } else { + readyToDraw.value = true + } + } + ) +} + +data class FontSizeRange( + val min: TextUnit, + val max: TextUnit, + val step: TextUnit = DEFAULT_TEXT_STEP, +) { + init { + require(min < max) { "min should be less than max, $this" } + require(step.value > 0) { "step should be greater than 0, $this" } + } + + companion object { + private val DEFAULT_TEXT_STEP = 1.sp + } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/home/compose/FirstStoriesContent.kt b/app/src/main/java/com/tangem/tap/features/home/compose/FirstStoriesContent.kt index 92929c0027..dfe41cc2c4 100644 --- a/app/src/main/java/com/tangem/tap/features/home/compose/FirstStoriesContent.kt +++ b/app/src/main/java/com/tangem/tap/features/home/compose/FirstStoriesContent.kt @@ -96,25 +96,33 @@ fun FirstStoriesContent( ) if (screenState.value != StartingScreenState.MEET_TANGEM) { - AutoSizeText(text = text?.let { stringResource(text) } ?: "", textStyle = style, + TextAutoSize( modifier = Modifier - .padding(start = 40.dp, end = 40.dp) - .alpha(if (screenState.value == StartingScreenState.SHOW_CARD) 0f else 1f),) + .padding(start = 40.dp, end = 40.dp, bottom = 100.dp) + .alpha(if (screenState.value == StartingScreenState.SHOW_CARD) 0f else 1f), + text = text?.let { stringResource(text) } ?: "", + textStyle = style, + fontSizeRange = FontSizeRange(40.sp, 60.sp) + ) } AnimatedVisibility( visible = screenState.value == StartingScreenState.MEET_TANGEM, enter = slideInVertically() { it / 2 } ) { - AutoSizeText(text = text?.let { stringResource(text) } ?: "", textStyle = style, + TextAutoSize( modifier = Modifier - .padding(start = 40.dp, end = 40.dp) + .fillMaxWidth() + .padding(start = 40.dp, end = 40.dp), + text = text?.let { stringResource(text) } ?: "", + textStyle = style, + fontSizeRange = FontSizeRange(20.sp, 60.sp) ) } AnimatedVisibility( visible = screenState.value == StartingScreenState.SHOW_CARD || - screenState.value == StartingScreenState.MEET_TANGEM, + screenState.value == StartingScreenState.MEET_TANGEM, enter = scaleIn(initialScale = 3f) ) { Image( diff --git a/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensState.kt b/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensState.kt index 5476447c70..0bf2afeb58 100644 --- a/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensState.kt +++ b/app/src/main/java/com/tangem/tap/features/tokens/redux/TokensState.kt @@ -61,7 +61,8 @@ fun List.filter(supportedBlockchains: Set?): List hideWarning() + BalanceStatus.VerifiedOnline, BalanceStatus.SameCurrencyTransactionInProgress -> hideWarning(isCustom) BalanceStatus.Loading -> { - hideWarning() + hideWarning(isCustom) if (wallet.currencyData.amountFormatted == null) { tvExchangeRate.text = root.getString(R.string.wallet_balance_loading) } } BalanceStatus.TransactionInProgress -> - showWarning(root.getString(R.string.wallet_balance_tx_in_progress)) + showWarning(root.getString(R.string.wallet_balance_tx_in_progress), isCustom) BalanceStatus.Unreachable -> - showWarning(root.getString(R.string.wallet_balance_blockchain_unreachable)) + showWarning(root.getString(R.string.wallet_balance_blockchain_unreachable), isCustom) BalanceStatus.NoAccount -> - showWarning(root.getString(R.string.wallet_error_no_account)) + showWarning(root.getString(R.string.wallet_error_no_account), isCustom) else -> { } } } - private fun showWarning(message: String) { - toggleWarning(true) + private fun showWarning(message: String, isCustom: Boolean = false) { + toggleWarning(true, isCustom) binding.tvStatusErrorMessage.text = message } - private fun hideWarning() { - toggleWarning(false) + private fun hideWarning(isCustom: Boolean = false) { + toggleWarning(false, isCustom) } - private fun toggleWarning(show: Boolean) { - binding.tvExchangeRate.show(!show) - binding.tvCustomCurrency.show(!show) + private fun toggleWarning(show: Boolean, isCustom: Boolean = false) { + if (!show) { + binding.tvExchangeRate.show(!isCustom) + binding.tvCustomCurrency.show(isCustom) + } else { + binding.tvExchangeRate.hide() + binding.tvCustomCurrency.hide() + } binding.tvStatusErrorMessage.show(show) } }