mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Moved business logic from MainViewModel to AllUnitRepository
This commit is contained in:
parent
d758712413
commit
c72f2960cf
@ -150,6 +150,29 @@ class AllUnitsRepository @Inject constructor() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update [AbstractUnit.basicUnit] properties for currncies from [currencyCollection].
|
||||||
|
*
|
||||||
|
* @param conversions Map: [AbstractUnit.unitId] and [BigDecimal] that will replace current
|
||||||
|
* [AbstractUnit.basicUnit].
|
||||||
|
*/
|
||||||
|
fun updateBasicUnitsForCurrencies(
|
||||||
|
conversions: Map<String, BigDecimal>
|
||||||
|
) {
|
||||||
|
getCollectionByGroup(UnitGroup.CURRENCY)?.forEach {
|
||||||
|
// Getting rates from map. We set ZERO as default so that it can be skipped
|
||||||
|
val rate = conversions.getOrElse(it.unitId) { BigDecimal.ZERO }
|
||||||
|
// We make sure that we don't divide by zero
|
||||||
|
if (rate > BigDecimal.ZERO) {
|
||||||
|
it.isEnabled = true
|
||||||
|
it.basicUnit = BigDecimal.ONE.setScale(MAX_PRECISION).div(rate)
|
||||||
|
} else {
|
||||||
|
// Hiding broken currencies
|
||||||
|
it.isEnabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: Ignore formatting below it's easier to read this lines as table
|
// NOTE: Ignore formatting below it's easier to read this lines as table
|
||||||
private val lengthCollection: List<AbstractUnit> by lazy {
|
private val lengthCollection: List<AbstractUnit> by lazy {
|
||||||
listOf(
|
listOf(
|
||||||
|
@ -29,7 +29,6 @@ import com.sadellie.unitto.FirebaseHelper
|
|||||||
import com.sadellie.unitto.data.KEY_0
|
import com.sadellie.unitto.data.KEY_0
|
||||||
import com.sadellie.unitto.data.KEY_DOT
|
import com.sadellie.unitto.data.KEY_DOT
|
||||||
import com.sadellie.unitto.data.KEY_MINUS
|
import com.sadellie.unitto.data.KEY_MINUS
|
||||||
import com.sadellie.unitto.data.preferences.MAX_PRECISION
|
|
||||||
import com.sadellie.unitto.data.preferences.OutputFormat
|
import com.sadellie.unitto.data.preferences.OutputFormat
|
||||||
import com.sadellie.unitto.data.preferences.UserPreferences
|
import com.sadellie.unitto.data.preferences.UserPreferences
|
||||||
import com.sadellie.unitto.data.preferences.UserPreferencesRepository
|
import com.sadellie.unitto.data.preferences.UserPreferencesRepository
|
||||||
@ -225,18 +224,7 @@ class MainViewModel @Inject constructor(
|
|||||||
try {
|
try {
|
||||||
val pairs: CurrencyUnitResponse =
|
val pairs: CurrencyUnitResponse =
|
||||||
CurrencyApi.retrofitService.getCurrencyPairs(unitFrom.unitId)
|
CurrencyApi.retrofitService.getCurrencyPairs(unitFrom.unitId)
|
||||||
allUnitsRepository.getCollectionByGroup(UnitGroup.CURRENCY)?.forEach {
|
allUnitsRepository.updateBasicUnitsForCurrencies(pairs.currency)
|
||||||
// Getting rates from map. We set ZERO as default so that it can be skipped
|
|
||||||
val rate = pairs.currency.getOrElse(it.unitId) { BigDecimal.ZERO }
|
|
||||||
// We make sure that we don't divide by zero
|
|
||||||
if (rate > BigDecimal.ZERO) {
|
|
||||||
it.isEnabled = true
|
|
||||||
it.basicUnit = BigDecimal.ONE.setScale(MAX_PRECISION).div(rate)
|
|
||||||
} else {
|
|
||||||
// Hiding broken currencies
|
|
||||||
it.isEnabled = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
when (e) {
|
when (e) {
|
||||||
// 403, Network and Adapter exceptions can be ignored
|
// 403, Network and Adapter exceptions can be ignored
|
||||||
|
Loading…
x
Reference in New Issue
Block a user