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 dotButtonEnabled Add dot to input 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 showError Whether there was an error while loading data from network
*/
@ -20,7 +21,7 @@ data class MainScreenUIState(
var deleteButtonEnabled: Boolean = false,
var dotButtonEnabled: Boolean = true,
var negateButtonEnabled: Boolean = false,
var isLoadingDataStore: Boolean = true,
var isLoadingDatabase: Boolean = true,
var isLoadingNetwork: 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
*/
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
val convertedValue: BigDecimal =
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() {
// 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 }
}
convertValue()
mainUIState = mainUIState.copy(negateButtonEnabled = unitFrom.group.canNegate)
// Now we load units data from database
val allBasedUnits = basedUnitRepository.getAll()
ALL_UNITS.forEach {
// Loading unit names so that we can search through them
it.renderedName = application.getString(it.displayName)
@ -526,19 +525,10 @@ class MainViewModel @Inject constructor(
it.counter = based?.frequency ?: 0
}
// User is free to convert values
// Set negate button state according to current group
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.
* */
// User is free to convert values and units on units screen can be sorted properly
mainUIState = mainUIState.copy(isLoadingDatabase = false)
updateCurrenciesBasicUnits()
convertValue()
}
}
}

View File

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

View File

@ -25,7 +25,8 @@ import com.sadellie.unitto.data.units.AbstractUnit
* @param outputValue Current output value (like big decimal)
* @param unitFrom [AbstractUnit] on the left
* @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 networkError Did we got errors while trying to get data from network
* @param onUnitSelectionClick Function that is called when clicking unit selection buttons
@ -38,7 +39,7 @@ fun TopScreenPart(
outputValue: String,
unitFrom: AbstractUnit,
unitTo: AbstractUnit,
loadingDataStore: Boolean,
loadingDatabase: Boolean,
loadingNetwork: Boolean,
networkError: Boolean,
onUnitSelectionClick: (Boolean) -> Unit,
@ -51,14 +52,14 @@ fun TopScreenPart(
MyTextField(
Modifier.fillMaxWidth(),
inputValue,
stringResource(id = if (loadingDataStore) R.string.loading_label else unitFrom.shortName),
stringResource(id = if (loadingDatabase) R.string.loading_label else unitFrom.shortName),
loadingNetwork,
networkError
)
MyTextField(
Modifier.fillMaxWidth(),
outputValue,
stringResource(id = if (loadingDataStore) R.string.loading_label else unitTo.shortName),
stringResource(id = if (loadingDatabase) R.string.loading_label else unitTo.shortName),
loadingNetwork,
networkError
)
@ -73,9 +74,9 @@ fun TopScreenPart(
.weight(1f),
onClick = { onUnitSelectionClick(true) },
label = unitFrom.displayName,
loadingState = loadingDataStore
isLoading = loadingDatabase
)
IconButton({ swapUnits() }, enabled = !loadingDataStore) {
IconButton({ swapUnits() }, enabled = !loadingDatabase) {
Icon(
Icons.Outlined.SwapHoriz, contentDescription = stringResource(
id = R.string.swap_units_description
@ -88,7 +89,7 @@ fun TopScreenPart(
.weight(1f),
onClick = { onUnitSelectionClick(false) },
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 onClick Function to call when button is clicked (navigate to a unit selection screen)
* @param label Text on button
* @param loadingState Show "Loading" text and disable button
* @param isLoading Show "Loading" text and disable button
*/
@Composable
fun UnitSelectionButton(
modifier: Modifier = Modifier,
onClick: () -> Unit = {},
label: Int,
loadingState: Boolean
isLoading: Boolean
) {
Button(
modifier = modifier,
onClick = { onClick() },
enabled = !loadingState,
enabled = !isLoading,
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer
),
@ -59,7 +59,7 @@ fun UnitSelectionButton(
}
) {
Text(
text = stringResource(if (loadingState) R.string.loading_label else label),
text = stringResource(if (isLoading) R.string.loading_label else label),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
color = MaterialTheme.colorScheme.onSecondaryContainer