Fixed network loading states

This commit is contained in:
Sad Ellie 2023-01-04 17:52:45 +04:00
parent 28935f31e0
commit ce050de877
3 changed files with 12 additions and 10 deletions

View File

@ -115,7 +115,7 @@ private fun MainScreenContent(
outputValue = mainScreenUIState.resultValue, outputValue = mainScreenUIState.resultValue,
unitFrom = mainScreenUIState.unitFrom, unitFrom = mainScreenUIState.unitFrom,
unitTo = mainScreenUIState.unitTo, unitTo = mainScreenUIState.unitTo,
loadingNetwork = mainScreenUIState.showLoading, networkLoading = mainScreenUIState.showLoading,
networkError = mainScreenUIState.showError, networkError = mainScreenUIState.showError,
onUnitSelectionClick = navControllerAction, onUnitSelectionClick = navControllerAction,
swapUnits = swapMeasurements, swapUnits = swapMeasurements,

View File

@ -169,9 +169,6 @@ class MainViewModel @Inject constructor(
* @param symbolToAdd Use 'ugly' version of symbols. * @param symbolToAdd Use 'ugly' version of symbols.
*/ */
fun processInput(symbolToAdd: String) { fun processInput(symbolToAdd: String) {
// We are still loading data from network, don't accept any input yet
if (_showLoading.value) return
val lastTwoSymbols = _latestInputStack.takeLast(2) val lastTwoSymbols = _latestInputStack.takeLast(2)
val lastSymbol: String = lastTwoSymbols.getOrNull(1) ?: lastTwoSymbols[0] val lastSymbol: String = lastTwoSymbols.getOrNull(1) ?: lastTwoSymbols[0]
val lastSecondSymbol: String? = lastTwoSymbols.getOrNull(0) val lastSecondSymbol: String? = lastTwoSymbols.getOrNull(0)
@ -362,6 +359,7 @@ class MainViewModel @Inject constructor(
_unitFrom _unitFrom
.getAndUpdate { _unitTo.value } .getAndUpdate { _unitTo.value }
.also { oldUnitFrom -> _unitTo.update { oldUnitFrom } } .also { oldUnitFrom -> _unitTo.update { oldUnitFrom } }
updateCurrenciesRatesIfNeeded()
} }
/** /**
@ -559,6 +557,10 @@ class MainViewModel @Inject constructor(
} }
_showError.update { true } _showError.update { true }
} finally { } finally {
/**
* Loaded, convert (this will trigger flow to call convertInput). Even if there was
* an error, it's OK and user will not see conversion result in that case.
*/
_showLoading.update { false } _showLoading.update { false }
} }
} }
@ -575,7 +577,9 @@ class MainViewModel @Inject constructor(
private fun startObserving() { private fun startObserving() {
viewModelScope.launch(Dispatchers.Default) { viewModelScope.launch(Dispatchers.Default) {
merge(_input, _unitFrom, _unitTo, _userPrefs).collectLatest { convertInput() } merge(_input, _unitFrom, _unitTo, _showLoading, _userPrefs).collectLatest {
convertInput()
}
} }
} }

View File

@ -58,7 +58,7 @@ import com.sadellie.unitto.screens.main.ConverterMode
* @param outputValue Current output value (like big decimal). * @param outputValue Current output value (like big decimal).
* @param unitFrom [AbstractUnit] on the left. * @param unitFrom [AbstractUnit] on the left.
* @param unitTo [AbstractUnit] on the right. * @param unitTo [AbstractUnit] on the right.
* @param loadingNetwork Are we loading data from network? Shows loading text in TextFields. * @param networkLoading Are we loading data from network? Shows loading text in TextFields.
* @param networkError Did we got errors while trying to get data from network. * @param networkError Did we got errors while trying to get data from network.
* @param onUnitSelectionClick Function that is called when clicking unit selection buttons. * @param onUnitSelectionClick Function that is called when clicking unit selection buttons.
* @param swapUnits Method to swap units. * @param swapUnits Method to swap units.
@ -72,7 +72,7 @@ fun TopScreenPart(
outputValue: String, outputValue: String,
unitFrom: AbstractUnit?, unitFrom: AbstractUnit?,
unitTo: AbstractUnit?, unitTo: AbstractUnit?,
loadingNetwork: Boolean, networkLoading: Boolean,
networkError: Boolean, networkError: Boolean,
onUnitSelectionClick: (String) -> Unit, onUnitSelectionClick: (String) -> Unit,
swapUnits: () -> Unit, swapUnits: () -> Unit,
@ -92,8 +92,6 @@ fun TopScreenPart(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
primaryText = { primaryText = {
when { when {
loadingNetwork -> stringResource(R.string.loading_label)
networkError -> stringResource(R.string.error_label)
converterMode == ConverterMode.BASE -> inputValue.uppercase() converterMode == ConverterMode.BASE -> inputValue.uppercase()
else -> Formatter.format(inputValue) else -> Formatter.format(inputValue)
} }
@ -106,7 +104,7 @@ fun TopScreenPart(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
primaryText = { primaryText = {
when { when {
loadingNetwork -> stringResource(R.string.loading_label) networkLoading -> stringResource(R.string.loading_label)
networkError -> stringResource(R.string.error_label) networkError -> stringResource(R.string.error_label)
converterMode == ConverterMode.BASE -> outputValue.uppercase() converterMode == ConverterMode.BASE -> outputValue.uppercase()
else -> Formatter.format(outputValue) else -> Formatter.format(outputValue)