mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-20 09:15:26 +02:00
AllUnitsRepository is now singleton
Also moved mapping function from init of MainViewModel to repository.
This commit is contained in:
parent
c4b42c764b
commit
dc2fdf8fa7
@ -1,17 +1,20 @@
|
|||||||
package com.sadellie.unitto.data.units
|
package com.sadellie.unitto.data.units
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import com.sadellie.unitto.R
|
import com.sadellie.unitto.R
|
||||||
import com.sadellie.unitto.data.preferences.MAX_PRECISION
|
import com.sadellie.unitto.data.preferences.MAX_PRECISION
|
||||||
|
import com.sadellie.unitto.data.units.database.MyBasedUnit
|
||||||
import com.sadellie.unitto.screens.setMinimumRequiredScale
|
import com.sadellie.unitto.screens.setMinimumRequiredScale
|
||||||
import com.sadellie.unitto.screens.sortByLev
|
import com.sadellie.unitto.screens.sortByLev
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
import java.math.RoundingMode
|
import java.math.RoundingMode
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This repository provides access to all collection of units in the app.
|
* This repository provides access to all collection of units in the app.
|
||||||
*/
|
*/
|
||||||
|
@Singleton
|
||||||
class AllUnitsRepository @Inject constructor() {
|
class AllUnitsRepository @Inject constructor() {
|
||||||
/**
|
/**
|
||||||
* This is a collection of all available units.
|
* This is a collection of all available units.
|
||||||
@ -104,6 +107,26 @@ class AllUnitsRepository @Inject constructor() {
|
|||||||
return unitsToShow.groupBy { it.group }
|
return unitsToShow.groupBy { it.group }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps data from database to [allUnits] item: favorites, counters, renderedNames and etc.
|
||||||
|
*
|
||||||
|
* @param context [Context] that is used to fill [AbstractUnit.renderedName]. Rendered names are used when
|
||||||
|
* searching.
|
||||||
|
* @param allBasedUnits List from database. See: [MyBasedUnit].
|
||||||
|
*/
|
||||||
|
fun loadFromDatabase(context: Context, allBasedUnits: List<MyBasedUnit>) {
|
||||||
|
allUnits.forEach {
|
||||||
|
// Loading unit names so that we can search through them
|
||||||
|
it.renderedName = context.getString(it.displayName)
|
||||||
|
val based = allBasedUnits.firstOrNull { based -> based.unitId == it.unitId }
|
||||||
|
// Loading paired units
|
||||||
|
it.pairedUnit = based?.pairedUnitId
|
||||||
|
// Loading favorite state
|
||||||
|
it.isFavorite = based?.isFavorite ?: false
|
||||||
|
it.counter = based?.frequency ?: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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(
|
||||||
|
@ -420,16 +420,7 @@ class MainViewModel @Inject constructor(
|
|||||||
|
|
||||||
// Now we load units data from database
|
// Now we load units data from database
|
||||||
val allBasedUnits = basedUnitRepository.getAll()
|
val allBasedUnits = basedUnitRepository.getAll()
|
||||||
allUnitsRepository.allUnits.forEach {
|
allUnitsRepository.loadFromDatabase(application, allBasedUnits)
|
||||||
// Loading unit names so that we can search through them
|
|
||||||
it.renderedName = application.getString(it.displayName)
|
|
||||||
val based = allBasedUnits.firstOrNull { based -> based.unitId == it.unitId }
|
|
||||||
// Loading paired units
|
|
||||||
it.pairedUnit = based?.pairedUnitId
|
|
||||||
// Loading favorite state
|
|
||||||
it.isFavorite = based?.isFavorite ?: false
|
|
||||||
it.counter = based?.frequency ?: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// User is free to convert values and units on units screen can be sorted properly
|
// User is free to convert values and units on units screen can be sorted properly
|
||||||
mainUIState = mainUIState.copy(isLoadingDatabase = false)
|
mainUIState = mainUIState.copy(isLoadingDatabase = false)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user