mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-20 01:05: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
|
||||
|
||||
import android.content.Context
|
||||
import com.sadellie.unitto.R
|
||||
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.sortByLev
|
||||
import java.math.BigDecimal
|
||||
import java.math.RoundingMode
|
||||
import javax.inject.Inject
|
||||
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* This repository provides access to all collection of units in the app.
|
||||
*/
|
||||
@Singleton
|
||||
class AllUnitsRepository @Inject constructor() {
|
||||
/**
|
||||
* This is a collection of all available units.
|
||||
@ -104,6 +107,26 @@ class AllUnitsRepository @Inject constructor() {
|
||||
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
|
||||
private val lengthCollection: List<AbstractUnit> by lazy {
|
||||
listOf(
|
||||
|
@ -420,16 +420,7 @@ class MainViewModel @Inject constructor(
|
||||
|
||||
// Now we load units data from database
|
||||
val allBasedUnits = basedUnitRepository.getAll()
|
||||
allUnitsRepository.allUnits.forEach {
|
||||
// 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
|
||||
}
|
||||
allUnitsRepository.loadFromDatabase(application, allBasedUnits)
|
||||
|
||||
// User is free to convert values and units on units screen can be sorted properly
|
||||
mainUIState = mainUIState.copy(isLoadingDatabase = false)
|
||||
|
Loading…
x
Reference in New Issue
Block a user