From eb9b12da28677aab8f9bbb8d61418a2020c5e149 Mon Sep 17 00:00:00 2001 From: sadellie Date: Thu, 20 Jul 2023 22:52:53 +0300 Subject: [PATCH] Retry currency rates update relevant to #31 --- core/base/src/main/res/values/strings.xml | 1 + .../unitto/feature/converter/ConverterScreen.kt | 6 +++++- .../unitto/feature/converter/ConverterViewModel.kt | 2 +- .../feature/converter/components/TopScreen.kt | 14 +++++++++++--- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/base/src/main/res/values/strings.xml b/core/base/src/main/res/values/strings.xml index 451ab317..2b454edd 100644 --- a/core/base/src/main/res/values/strings.xml +++ b/core/base/src/main/res/values/strings.xml @@ -962,6 +962,7 @@ "Loading…" "Error" + "Click to try again" "Copied %1$s!" "Cancel" "OK" diff --git a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterScreen.kt b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterScreen.kt index be321efc..7f27e604 100644 --- a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterScreen.kt +++ b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterScreen.kt @@ -61,6 +61,7 @@ internal fun ConverterRoute( clearInput = viewModel::clearInput, onCursorChange = viewModel::onCursorChange, cutCallback = viewModel::deleteTokens, + onErrorClick = viewModel::updateCurrenciesRatesIfNeeded, ) } @@ -77,6 +78,7 @@ private fun ConverterScreen( clearInput: () -> Unit, onCursorChange: (TextRange) -> Unit, cutCallback: () -> Unit, + onErrorClick: () -> Unit, ) { UnittoScreenWithTopBar( title = { Text(stringResource(R.string.unit_converter)) }, @@ -107,6 +109,7 @@ private fun ConverterScreen( cutCallback = cutCallback, pasteCallback = processInput, formatterSymbols = uiState.formatterSymbols, + onErrorClick = onErrorClick ) }, content2 = { @@ -144,6 +147,7 @@ private fun PreviewConverterScreen() { deleteDigit = {}, clearInput = {}, onCursorChange = {}, - cutCallback = {} + cutCallback = {}, + onErrorClick = {}, ) } diff --git a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterViewModel.kt b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterViewModel.kt index 2d44929d..016bfe31 100644 --- a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterViewModel.kt +++ b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/ConverterViewModel.kt @@ -285,7 +285,7 @@ class ConverterViewModel @Inject constructor( } } - private fun updateCurrenciesRatesIfNeeded() { + fun updateCurrenciesRatesIfNeeded() { viewModelScope.launch(Dispatchers.IO) { _showError.update { false } _showLoading.update { false } diff --git a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/components/TopScreen.kt b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/components/TopScreen.kt index 31507ee5..d2cc56d5 100644 --- a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/components/TopScreen.kt +++ b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/components/TopScreen.kt @@ -98,6 +98,7 @@ internal fun TopScreenPart( cutCallback: () -> Unit, pasteCallback: (String) -> Unit, formatterSymbols: FormatterSymbols, + onErrorClick: () -> Unit, ) { var swapped by remember { mutableStateOf(false) } val swapButtonRotation: Float by animateFloatAsState( @@ -148,7 +149,8 @@ internal fun TopScreenPart( modifier = Modifier, value = calculatedTextFieldValue, onCursorChange = { newSelection -> - calculatedTextFieldValue = calculatedTextFieldValue.copy(selection = newSelection) + calculatedTextFieldValue = + calculatedTextFieldValue.copy(selection = newSelection) }, formatterSymbols = formatterSymbols, textColor = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f), @@ -237,7 +239,7 @@ internal fun TopScreenPart( UnformattedTextField( modifier = Modifier.weight(2f), value = TextFieldValue(stringResource(R.string.error_label)), - onCursorChange = {}, + onCursorChange = { onErrorClick() }, minRatio = 0.7f, readOnly = true, textColor = MaterialTheme.colorScheme.error @@ -245,9 +247,15 @@ internal fun TopScreenPart( } } + val supportLabelTo = when { + outputValue is ConversionResult.Error -> R.string.try_again_label + (unitTo?.shortName != null) -> unitTo.shortName + else -> R.string.loading_label + } + AnimatedContent( modifier = Modifier.fillMaxWidth(), - targetState = stringResource(unitTo?.shortName ?: R.string.loading_label), + targetState = stringResource(supportLabelTo), transitionSpec = { // Enter animation (expandHorizontally(clip = false, expandFrom = Alignment.Start) + fadeIn()