mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 08:45:27 +02:00
Update UnitsRepository
This commit is contained in:
parent
f9d6018bde
commit
38da48049d
@ -56,11 +56,11 @@ import com.sadellie.unitto.data.units.collections.volumeCollection
|
|||||||
import com.sadellie.unitto.data.units.remote.CurrencyApi
|
import com.sadellie.unitto.data.units.remote.CurrencyApi
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import kotlinx.coroutines.flow.mapLatest
|
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
@ -99,37 +99,28 @@ class UnitsRepository @Inject constructor(
|
|||||||
fuelConsumptionCollection
|
fuelConsumptionCollection
|
||||||
)
|
)
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
val allUnits: Flow<List<AbstractUnit>> = combine(
|
||||||
val allUnits = combine(
|
|
||||||
unitsDao.getAllFlow(),
|
unitsDao.getAllFlow(),
|
||||||
myUnits
|
myUnits
|
||||||
) { based, _ ->
|
) { basedList, inMemoryList ->
|
||||||
based
|
return@combine inMemoryList.map { inMemoryUnit ->
|
||||||
}
|
val inBaseUnit = basedList.find { it.unitId == inMemoryUnit.id }
|
||||||
.mapLatest { basedList ->
|
?: return@map inMemoryUnit
|
||||||
basedList.forEach { based ->
|
inMemoryUnit.clone(
|
||||||
// Have to use a copy so that composable can detect changes
|
isFavorite = inBaseUnit.isFavorite,
|
||||||
val updatedUnit = getById(based.unitId)
|
counter = inBaseUnit.frequency,
|
||||||
.clone(
|
pairId = inBaseUnit.pairedUnitId
|
||||||
isFavorite = based.isFavorite,
|
|
||||||
counter = based.frequency,
|
|
||||||
pairId = based.pairedUnitId
|
|
||||||
)
|
)
|
||||||
|
|
||||||
myUnits.update { units ->
|
|
||||||
units.replace(updatedUnit) { it.id == updatedUnit.id }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
myUnits
|
|
||||||
}
|
|
||||||
.flowOn(Dispatchers.IO)
|
.flowOn(Dispatchers.IO)
|
||||||
|
|
||||||
fun getById(id: String): AbstractUnit {
|
suspend fun getById(id: String): AbstractUnit {
|
||||||
return myUnits.value.first { it.id == id }
|
return allUnits.first().first { it.id == id }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getCollection(group: UnitGroup): List<AbstractUnit> {
|
suspend fun getCollection(group: UnitGroup): List<AbstractUnit> {
|
||||||
return myUnits.value.filter { it.group == group }
|
return allUnits.first().filter { it.group == group }
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun favorite(unit: AbstractUnit) = withContext(Dispatchers.IO) {
|
suspend fun favorite(unit: AbstractUnit) = withContext(Dispatchers.IO) {
|
||||||
@ -244,7 +235,7 @@ class UnitsRepository @Inject constructor(
|
|||||||
?.let { LocalDate.ofEpochDay(it) }
|
?.let { LocalDate.ofEpochDay(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun filterUnits(
|
suspend fun filterUnits(
|
||||||
query: String,
|
query: String,
|
||||||
unitGroup: UnitGroup?,
|
unitGroup: UnitGroup?,
|
||||||
favoritesOnly: Boolean,
|
favoritesOnly: Boolean,
|
||||||
@ -254,7 +245,7 @@ class UnitsRepository @Inject constructor(
|
|||||||
): Map<UnitGroup, List<AbstractUnit>> {
|
): Map<UnitGroup, List<AbstractUnit>> {
|
||||||
// Leave only shown unit groups
|
// Leave only shown unit groups
|
||||||
var units: Sequence<AbstractUnit> = if (unitGroup == null) {
|
var units: Sequence<AbstractUnit> = if (unitGroup == null) {
|
||||||
myUnits.value.filter { it.group in shownUnitGroups }
|
allUnits.first().filter { it.group in shownUnitGroups }
|
||||||
} else {
|
} else {
|
||||||
getCollection(unitGroup)
|
getCollection(unitGroup)
|
||||||
}.asSequence()
|
}.asSequence()
|
||||||
@ -282,10 +273,4 @@ class UnitsRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
return units.groupBy { it.group }
|
return units.groupBy { it.group }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T> List<T>.replace(newValue: T, block: (T) -> Boolean): List<T> {
|
|
||||||
return map {
|
|
||||||
if (block(it)) newValue else it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ import com.sadellie.unitto.data.model.UnitGroup
|
|||||||
import com.sadellie.unitto.data.model.unit.DefaultUnit
|
import com.sadellie.unitto.data.model.unit.DefaultUnit
|
||||||
import com.sadellie.unitto.data.model.unit.NumberBaseUnit
|
import com.sadellie.unitto.data.model.unit.NumberBaseUnit
|
||||||
import com.sadellie.unitto.data.model.unit.ReverseUnit
|
import com.sadellie.unitto.data.model.unit.ReverseUnit
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.After
|
import org.junit.After
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
@ -541,8 +542,9 @@ class AllUnitsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun String.checkWith(checkingId: String, value: String, expected: String) {
|
private fun String.checkWith(checkingId: String, value: String, expected: String) {
|
||||||
val unitFrom = allUnitsRepository.getById(this)
|
val str = this
|
||||||
val unitTo = allUnitsRepository.getById(checkingId)
|
val unitFrom = runBlocking { allUnitsRepository.getById(str) }
|
||||||
|
val unitTo = runBlocking { allUnitsRepository.getById(checkingId) }
|
||||||
|
|
||||||
val actual = when (unitFrom.group) {
|
val actual = when (unitFrom.group) {
|
||||||
UnitGroup.NUMBER_BASE -> (unitFrom as NumberBaseUnit).convert((unitTo as NumberBaseUnit), value)
|
UnitGroup.NUMBER_BASE -> (unitFrom as NumberBaseUnit).convert((unitTo as NumberBaseUnit), value)
|
||||||
@ -564,7 +566,7 @@ class AllUnitsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
fun after() {
|
fun after() = runBlocking {
|
||||||
val unitGroup = history.keys.first()
|
val unitGroup = history.keys.first()
|
||||||
// GROUP : testedCount / totalCount
|
// GROUP : testedCount / totalCount
|
||||||
println("${unitGroup.name} : ${history[unitGroup]?.size} / ${allUnitsRepository.getCollection(unitGroup).size}")
|
println("${unitGroup.name} : ${history[unitGroup]?.size} / ${allUnitsRepository.getCollection(unitGroup).size}")
|
||||||
|
@ -52,6 +52,7 @@ import kotlinx.coroutines.flow.mapLatest
|
|||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.math.BigDecimal
|
import java.math.BigDecimal
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@ -293,12 +294,12 @@ internal class ConverterViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateUnitFrom(unit: AbstractUnit) {
|
fun updateUnitFrom(unit: AbstractUnit) = viewModelScope.launch {
|
||||||
val pair = unitsRepo.getById(
|
val pair = unitsRepo.getById(
|
||||||
unit.pairId ?: unitsRepo.getCollection(unit.group).first().id
|
unit.pairId ?: unitsRepo.getCollection(unit.group).first().id
|
||||||
)
|
)
|
||||||
|
|
||||||
viewModelScope.launch(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
unitsRepo.incrementCounter(unit)
|
unitsRepo.incrementCounter(unit)
|
||||||
userPrefsRepository.updateLatestPairOfUnits(unitFrom = unit, unitTo = pair)
|
userPrefsRepository.updateLatestPairOfUnits(unitFrom = unit, unitTo = pair)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user