Bug fixes

Fixed ordering for units searcher, units that contain query go first now
Fixed method call order in init
This commit is contained in:
Sad Ellie 2022-04-22 20:42:01 +03:00
parent 3d6da2595f
commit f5faea804b

View File

@ -24,6 +24,7 @@ import com.sadellie.unitto.data.units.remote.CurrencyApi
import com.sadellie.unitto.data.units.remote.CurrencyUnitResponse import com.sadellie.unitto.data.units.remote.CurrencyUnitResponse
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -428,26 +429,28 @@ class MainViewModel @Inject constructor(
withContext(Dispatchers.Default) { withContext(Dispatchers.Default) {
// Basic filtering // Basic filtering
val basicFilteredUnits = var basicFilteredUnits = ALL_UNITS.asSequence()
ALL_UNITS.asSequence() basicFilteredUnits = when {
// Unit group and favorite // Both sides, Chip is selected, Only favorites
.filter { (filterGroup) and (favoritesOnly) -> {
// Decide which group of units to show basicFilteredUnits.filter { (it.group == chosenUnitGroup) and it.isFavorite }
when { }
// Both sides, Chip is selected, Only favorites // Both sides, Chip is selected, NOT Only favorites
(filterGroup) and (favoritesOnly) -> { (filterGroup) and (!favoritesOnly) -> {
(it.group == chosenUnitGroup) and it.isFavorite basicFilteredUnits.filter { it.group == chosenUnitGroup }
} }
// Both sides, Chip is selected, NOT Only favorites // Chip is NOT selected, Only favorites
(filterGroup) and (!favoritesOnly) -> it.group == chosenUnitGroup (!filterGroup) and (favoritesOnly) -> {
// Chip is NOT selected, Only favorites basicFilteredUnits.filter { it.isFavorite }
(!filterGroup) and (favoritesOnly) -> it.isFavorite }
// Chip is NOT selected, NOT Only favorites // Chip is NOT selected, NOT Only favorites
else -> true else -> basicFilteredUnits
} }
}
// Hiding broken currency units // Hiding broken currency units
.filter { if (leftSide) true else it.isEnabled } if (leftSide) {
basicFilteredUnits = basicFilteredUnits.filter { it.isEnabled }
}
unitsToShow = if (query.isEmpty()) { unitsToShow = if (query.isEmpty()) {
// Query is empty, i.e. we want to see all units and they need to be sorted by usage // Query is empty, i.e. we want to see all units and they need to be sorted by usage
@ -462,6 +465,7 @@ class MainViewModel @Inject constructor(
.substring(0, minOf(query.length, it.renderedName.length)) .substring(0, minOf(query.length, it.renderedName.length))
.lev(query) .lev(query)
} }
.sortedByDescending { it.renderedName.contains(query) }
} }
// Group by unit group // Group by unit group
.groupBy { it.group } .groupBy { it.group }
@ -470,52 +474,45 @@ class MainViewModel @Inject constructor(
init { init {
viewModelScope.launch { viewModelScope.launch {
val latestLeftSideUnitId = mySettingsPrefs.getItem(
UserPreferenceKeys.LATEST_LEFT_SIDE,
MyUnitIDS.kilometer
).first()
val latestRightSideUnitId = mySettingsPrefs.getItem(
UserPreferenceKeys.LATEST_RIGHT_SIDE,
MyUnitIDS.mile
).first()
// First we load latest pair of units // First we load latest pair of units
unitFrom = try { unitFrom = try {
ALL_UNITS.first { ALL_UNITS.first { it.unitId == latestLeftSideUnitId }
it.unitId == mySettingsPrefs.getItem(
UserPreferenceKeys.LATEST_LEFT_SIDE,
MyUnitIDS.kilometer
).first()
}
} catch (e: java.util.NoSuchElementException) { } catch (e: java.util.NoSuchElementException) {
Log.w("MainViewModel", "No unit with the given unitId") Log.w("MainViewModel", "No unit with the given unitId")
ALL_UNITS ALL_UNITS.first { it.unitId == MyUnitIDS.kilometer }
.first { it.unitId == MyUnitIDS.kilometer }
} }
unitTo = try { unitTo = try {
ALL_UNITS ALL_UNITS
.first { .first { it.unitId == latestRightSideUnitId }
it.unitId == mySettingsPrefs.getItem(
UserPreferenceKeys.LATEST_RIGHT_SIDE,
MyUnitIDS.mile
).first()
}
} catch (e: java.util.NoSuchElementException) { } catch (e: java.util.NoSuchElementException) {
Log.w("MainViewModel", "No unit with the given unitId") Log.w("MainViewModel", "No unit with the given unitId")
ALL_UNITS ALL_UNITS.first { it.unitId == MyUnitIDS.mile }
.first { it.unitId == MyUnitIDS.mile }
} }
// Now we get the precision so we can convert values // Now we get the precision so we can convert values
precision = mySettingsPrefs.getItem(UserPreferenceKeys.DIGITS_PRECISION, 3).first() precision = mySettingsPrefs.getItem(UserPreferenceKeys.DIGITS_PRECISION, 3).first()
// Getting separator and changing it in number formatter // Getting separator and changing it in number formatter
separator = separator =
mySettingsPrefs.getItem(UserPreferenceKeys.SEPARATOR, Separator.SPACES).first() mySettingsPrefs
.getItem(UserPreferenceKeys.SEPARATOR, Separator.SPACES).first()
.also { Formatter.setSeparator(it) } .also { Formatter.setSeparator(it) }
// Getting output format // Getting output format
outputFormat = outputFormat =
mySettingsPrefs.getItem(UserPreferenceKeys.OUTPUT_FORMAT, OutputFormat.PLAIN) mySettingsPrefs
.getItem(UserPreferenceKeys.OUTPUT_FORMAT, OutputFormat.PLAIN)
.first() .first()
// Basic data is loaded, user is free to convert values
// Set negate button state according to current group
mainUIState = mainUIState.copy(
isLoadingDataStore = false,
negateButtonEnabled = unitFrom.group.canNegate
)
updateCurrenciesBasicUnits()
convertValue() convertValue()
val allBasedUnits = myBasedUnitDao.getAll() val allBasedUnits = myBasedUnitDao.getAll()
@ -530,6 +527,20 @@ class MainViewModel @Inject constructor(
it.isFavorite = based?.isFavorite ?: false it.isFavorite = based?.isFavorite ?: false
it.counter = based?.frequency ?: 0 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.
* */
updateCurrenciesBasicUnits()
} }
} }
} }