Fixed logic in MainViewModel's init method

This commit is contained in:
Sad Ellie 2022-06-05 16:33:28 +03:00
parent 63d943cbb9
commit e034e7bfb7
5 changed files with 24 additions and 32 deletions

View File

@ -10,7 +10,8 @@ import com.sadellie.unitto.data.KEY_0
* @property deleteButtonEnabled Delete last symbol from input button state * @property deleteButtonEnabled Delete last symbol from input button state
* @property dotButtonEnabled Add dot to input button state * @property dotButtonEnabled Add dot to input button state
* @property negateButtonEnabled Switch input between positive and negative button state * @property negateButtonEnabled Switch input between positive and negative button state
* @property isLoadingDataStore Whether we are loading data from DataStore. Need on app launch * @property isLoadingDatabase Whether we are loading data from Database. Need on app launch, once
* we are done loading list on Units list can be sorted by usage properly/
* @property isLoadingNetwork Whether we are loading data from network * @property isLoadingNetwork Whether we are loading data from network
* @property showError Whether there was an error while loading data from network * @property showError Whether there was an error while loading data from network
*/ */
@ -20,7 +21,7 @@ data class MainScreenUIState(
var deleteButtonEnabled: Boolean = false, var deleteButtonEnabled: Boolean = false,
var dotButtonEnabled: Boolean = true, var dotButtonEnabled: Boolean = true,
var negateButtonEnabled: Boolean = false, var negateButtonEnabled: Boolean = false,
var isLoadingDataStore: Boolean = true, var isLoadingDatabase: Boolean = true,
var isLoadingNetwork: Boolean = false, var isLoadingNetwork: Boolean = false,
var showError: Boolean = false, var showError: Boolean = false,
) )

View File

@ -135,8 +135,6 @@ class MainViewModel @Inject constructor(
* This function takes local variables, converts values and then causes the UI to update * This function takes local variables, converts values and then causes the UI to update
*/ */
private fun convertValue() { private fun convertValue() {
// We cannot convert values, as we are still user prefs from datastore (precision)
if (mainUIState.isLoadingDataStore) return
// Converting value using a specified precision // Converting value using a specified precision
val convertedValue: BigDecimal = val convertedValue: BigDecimal =
unitFrom.convert(unitTo, mainUIState.inputValue.toBigDecimal(), precision) unitFrom.convert(unitTo, mainUIState.inputValue.toBigDecimal(), precision)
@ -230,7 +228,8 @@ class MainViewModel @Inject constructor(
} }
/** /**
* Updates basic units properties for all currencies. Uses [unitFrom] * Updates basic units properties for all currencies, BUT only when [unitFrom]'s group is set
* to [UnitGroup.CURRENCY].
*/ */
private suspend fun updateCurrenciesBasicUnits() { private suspend fun updateCurrenciesBasicUnits() {
// Resetting error and network loading states in case we are not gonna do anything below // Resetting error and network loading states in case we are not gonna do anything below
@ -511,10 +510,10 @@ class MainViewModel @Inject constructor(
ALL_UNITS.first { it.unitId == MyUnitIDS.mile } ALL_UNITS.first { it.unitId == MyUnitIDS.mile }
} }
convertValue() mainUIState = mainUIState.copy(negateButtonEnabled = unitFrom.group.canNegate)
// Now we load units data from database
val allBasedUnits = basedUnitRepository.getAll() val allBasedUnits = basedUnitRepository.getAll()
ALL_UNITS.forEach { ALL_UNITS.forEach {
// Loading unit names so that we can search through them // Loading unit names so that we can search through them
it.renderedName = application.getString(it.displayName) it.renderedName = application.getString(it.displayName)
@ -526,19 +525,10 @@ class MainViewModel @Inject constructor(
it.counter = based?.frequency ?: 0 it.counter = based?.frequency ?: 0
} }
// User is free to convert values // User is free to convert values and units on units screen can be sorted properly
// Set negate button state according to current group mainUIState = mainUIState.copy(isLoadingDatabase = false)
mainUIState = mainUIState.copy(
isLoadingDataStore = false,
negateButtonEnabled = unitFrom.group.canNegate
)
/*
* This is at the bottom in case latest unit group was currency and user doesn't have
* network access.
* He can choose another unit group and doesn't need to wait for network to appear.
* */
updateCurrenciesBasicUnits() updateCurrenciesBasicUnits()
convertValue()
} }
} }
} }

View File

@ -109,7 +109,7 @@ private fun PortraitMainScreenContent(
outputValue = mainScreenUIState.resultValue, outputValue = mainScreenUIState.resultValue,
unitFrom = unitFrom, unitFrom = unitFrom,
unitTo = unitTo, unitTo = unitTo,
loadingDataStore = mainScreenUIState.isLoadingDataStore, loadingDatabase = mainScreenUIState.isLoadingDatabase,
loadingNetwork = mainScreenUIState.isLoadingNetwork, loadingNetwork = mainScreenUIState.isLoadingNetwork,
networkError = mainScreenUIState.showError, networkError = mainScreenUIState.showError,
onUnitSelectionClick = navControllerAction, onUnitSelectionClick = navControllerAction,
@ -143,7 +143,7 @@ private fun PortraitMainScreenContent(
outputValue = mainScreenUIState.resultValue, outputValue = mainScreenUIState.resultValue,
unitFrom = unitFrom, unitFrom = unitFrom,
unitTo = unitTo, unitTo = unitTo,
loadingDataStore = mainScreenUIState.isLoadingDataStore, loadingDatabase = mainScreenUIState.isLoadingDatabase,
loadingNetwork = mainScreenUIState.isLoadingNetwork, loadingNetwork = mainScreenUIState.isLoadingNetwork,
networkError = mainScreenUIState.showError, networkError = mainScreenUIState.showError,
onUnitSelectionClick = navControllerAction, onUnitSelectionClick = navControllerAction,

View File

@ -25,7 +25,8 @@ import com.sadellie.unitto.data.units.AbstractUnit
* @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 loadingDataStore Are we still loading settings from data store? Disables unit selection buttons * @param loadingDatabase Are we still loading units usage stats from database? Disables unit
* selection buttons.
* @param loadingNetwork Are we loading data from network? Shows loading text in TextFields * @param loadingNetwork 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
@ -38,7 +39,7 @@ fun TopScreenPart(
outputValue: String, outputValue: String,
unitFrom: AbstractUnit, unitFrom: AbstractUnit,
unitTo: AbstractUnit, unitTo: AbstractUnit,
loadingDataStore: Boolean, loadingDatabase: Boolean,
loadingNetwork: Boolean, loadingNetwork: Boolean,
networkError: Boolean, networkError: Boolean,
onUnitSelectionClick: (Boolean) -> Unit, onUnitSelectionClick: (Boolean) -> Unit,
@ -51,14 +52,14 @@ fun TopScreenPart(
MyTextField( MyTextField(
Modifier.fillMaxWidth(), Modifier.fillMaxWidth(),
inputValue, inputValue,
stringResource(id = if (loadingDataStore) R.string.loading_label else unitFrom.shortName), stringResource(id = if (loadingDatabase) R.string.loading_label else unitFrom.shortName),
loadingNetwork, loadingNetwork,
networkError networkError
) )
MyTextField( MyTextField(
Modifier.fillMaxWidth(), Modifier.fillMaxWidth(),
outputValue, outputValue,
stringResource(id = if (loadingDataStore) R.string.loading_label else unitTo.shortName), stringResource(id = if (loadingDatabase) R.string.loading_label else unitTo.shortName),
loadingNetwork, loadingNetwork,
networkError networkError
) )
@ -73,9 +74,9 @@ fun TopScreenPart(
.weight(1f), .weight(1f),
onClick = { onUnitSelectionClick(true) }, onClick = { onUnitSelectionClick(true) },
label = unitFrom.displayName, label = unitFrom.displayName,
loadingState = loadingDataStore isLoading = loadingDatabase
) )
IconButton({ swapUnits() }, enabled = !loadingDataStore) { IconButton({ swapUnits() }, enabled = !loadingDatabase) {
Icon( Icon(
Icons.Outlined.SwapHoriz, contentDescription = stringResource( Icons.Outlined.SwapHoriz, contentDescription = stringResource(
id = R.string.swap_units_description id = R.string.swap_units_description
@ -88,7 +89,7 @@ fun TopScreenPart(
.weight(1f), .weight(1f),
onClick = { onUnitSelectionClick(false) }, onClick = { onUnitSelectionClick(false) },
label = unitTo.displayName, label = unitTo.displayName,
loadingState = loadingDataStore isLoading = loadingDatabase
) )
} }
} }

View File

@ -26,19 +26,19 @@ import com.sadellie.unitto.R
* @param modifier Modifier that is applied to a [Button] * @param modifier Modifier that is applied to a [Button]
* @param onClick Function to call when button is clicked (navigate to a unit selection screen) * @param onClick Function to call when button is clicked (navigate to a unit selection screen)
* @param label Text on button * @param label Text on button
* @param loadingState Show "Loading" text and disable button * @param isLoading Show "Loading" text and disable button
*/ */
@Composable @Composable
fun UnitSelectionButton( fun UnitSelectionButton(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
onClick: () -> Unit = {}, onClick: () -> Unit = {},
label: Int, label: Int,
loadingState: Boolean isLoading: Boolean
) { ) {
Button( Button(
modifier = modifier, modifier = modifier,
onClick = { onClick() }, onClick = { onClick() },
enabled = !loadingState, enabled = !isLoading,
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer containerColor = MaterialTheme.colorScheme.secondaryContainer
), ),
@ -59,7 +59,7 @@ fun UnitSelectionButton(
} }
) { ) {
Text( Text(
text = stringResource(if (loadingState) R.string.loading_label else label), text = stringResource(if (isLoading) R.string.loading_label else label),
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
color = MaterialTheme.colorScheme.onSecondaryContainer color = MaterialTheme.colorScheme.onSecondaryContainer