mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 08:45:27 +02:00
Fixed network loading states
This commit is contained in:
parent
28935f31e0
commit
ce050de877
@ -115,7 +115,7 @@ private fun MainScreenContent(
|
||||
outputValue = mainScreenUIState.resultValue,
|
||||
unitFrom = mainScreenUIState.unitFrom,
|
||||
unitTo = mainScreenUIState.unitTo,
|
||||
loadingNetwork = mainScreenUIState.showLoading,
|
||||
networkLoading = mainScreenUIState.showLoading,
|
||||
networkError = mainScreenUIState.showError,
|
||||
onUnitSelectionClick = navControllerAction,
|
||||
swapUnits = swapMeasurements,
|
||||
|
@ -169,9 +169,6 @@ class MainViewModel @Inject constructor(
|
||||
* @param symbolToAdd Use 'ugly' version of symbols.
|
||||
*/
|
||||
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 lastSymbol: String = lastTwoSymbols.getOrNull(1) ?: lastTwoSymbols[0]
|
||||
val lastSecondSymbol: String? = lastTwoSymbols.getOrNull(0)
|
||||
@ -362,6 +359,7 @@ class MainViewModel @Inject constructor(
|
||||
_unitFrom
|
||||
.getAndUpdate { _unitTo.value }
|
||||
.also { oldUnitFrom -> _unitTo.update { oldUnitFrom } }
|
||||
updateCurrenciesRatesIfNeeded()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -559,6 +557,10 @@ class MainViewModel @Inject constructor(
|
||||
}
|
||||
_showError.update { true }
|
||||
} 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 }
|
||||
}
|
||||
}
|
||||
@ -575,7 +577,9 @@ class MainViewModel @Inject constructor(
|
||||
|
||||
private fun startObserving() {
|
||||
viewModelScope.launch(Dispatchers.Default) {
|
||||
merge(_input, _unitFrom, _unitTo, _userPrefs).collectLatest { convertInput() }
|
||||
merge(_input, _unitFrom, _unitTo, _showLoading, _userPrefs).collectLatest {
|
||||
convertInput()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,7 +58,7 @@ import com.sadellie.unitto.screens.main.ConverterMode
|
||||
* @param outputValue Current output value (like big decimal).
|
||||
* @param unitFrom [AbstractUnit] on the left.
|
||||
* @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 onUnitSelectionClick Function that is called when clicking unit selection buttons.
|
||||
* @param swapUnits Method to swap units.
|
||||
@ -72,7 +72,7 @@ fun TopScreenPart(
|
||||
outputValue: String,
|
||||
unitFrom: AbstractUnit?,
|
||||
unitTo: AbstractUnit?,
|
||||
loadingNetwork: Boolean,
|
||||
networkLoading: Boolean,
|
||||
networkError: Boolean,
|
||||
onUnitSelectionClick: (String) -> Unit,
|
||||
swapUnits: () -> Unit,
|
||||
@ -92,8 +92,6 @@ fun TopScreenPart(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
primaryText = {
|
||||
when {
|
||||
loadingNetwork -> stringResource(R.string.loading_label)
|
||||
networkError -> stringResource(R.string.error_label)
|
||||
converterMode == ConverterMode.BASE -> inputValue.uppercase()
|
||||
else -> Formatter.format(inputValue)
|
||||
}
|
||||
@ -106,7 +104,7 @@ fun TopScreenPart(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
primaryText = {
|
||||
when {
|
||||
loadingNetwork -> stringResource(R.string.loading_label)
|
||||
networkLoading -> stringResource(R.string.loading_label)
|
||||
networkError -> stringResource(R.string.error_label)
|
||||
converterMode == ConverterMode.BASE -> outputValue.uppercase()
|
||||
else -> Formatter.format(outputValue)
|
||||
|
Loading…
x
Reference in New Issue
Block a user