Add preference to disable currency converter
This commit is contained in:
@@ -7,5 +7,5 @@ import org.koin.dsl.module
|
||||
|
||||
val unitConverterModule = module {
|
||||
single { CurrencyRepository(androidContext()) }
|
||||
single<UnitConverterRepository> { UnitConverterRepositoryImpl(androidContext()) }
|
||||
single<UnitConverterRepository> { UnitConverterRepositoryImpl(androidContext(), get()) }
|
||||
}
|
||||
+41
-21
@@ -2,40 +2,59 @@ package de.mm20.launcher2.unitconverter
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import de.mm20.launcher2.currencies.CurrencyRepository
|
||||
import de.mm20.launcher2.preferences.LauncherDataStore
|
||||
import de.mm20.launcher2.search.data.UnitConverter
|
||||
import de.mm20.launcher2.unitconverter.converters.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
|
||||
interface UnitConverterRepository {
|
||||
fun search(query:String): Flow<UnitConverter?>
|
||||
fun search(query: String): Flow<UnitConverter?>
|
||||
}
|
||||
|
||||
internal class UnitConverterRepositoryImpl(val context: Context) : UnitConverterRepository, KoinComponent {
|
||||
internal class UnitConverterRepositoryImpl(
|
||||
private val context: Context,
|
||||
private val currencyRepository: CurrencyRepository,
|
||||
) : UnitConverterRepository, KoinComponent {
|
||||
private val dataStore: LauncherDataStore by inject()
|
||||
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.Default)
|
||||
|
||||
val unitConverter = MutableLiveData<UnitConverter?>()
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
dataStore.data.map { it.unitConverterSearch }.distinctUntilChanged().collectLatest {
|
||||
if (it.enabled && it.currencies) currencyRepository.enableCurrencyUpdateWorker()
|
||||
else currencyRepository.disableCurrencyUpdateWorker()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun search(query: String): Flow<UnitConverter?> = channelFlow {
|
||||
if (query.isBlank()) {
|
||||
send(null)
|
||||
return@channelFlow
|
||||
}
|
||||
dataStore.data.map { it.unitConverterSearch.enabled }.collectLatest {
|
||||
if (it) {
|
||||
send(queryUnitConverter(query))
|
||||
dataStore.data.map { it.unitConverterSearch }.collectLatest {
|
||||
if (it.enabled) {
|
||||
send(queryUnitConverter(query, it.currencies))
|
||||
} else {
|
||||
send(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun queryUnitConverter(query: String): UnitConverter? {
|
||||
private suspend fun queryUnitConverter(
|
||||
query: String,
|
||||
includeCurrencies: Boolean
|
||||
): UnitConverter? {
|
||||
if (!query.matches(Regex("[0-9,.:]+ [^\\s]+")) && !query.matches(Regex("[0-9,.:]+ [^\\s]+ >> [^\\s]+"))) return null
|
||||
val valueStr: String
|
||||
val unitStr: String
|
||||
@@ -49,18 +68,19 @@ internal class UnitConverterRepositoryImpl(val context: Context) : UnitConverter
|
||||
val value = valueStr.toDoubleOrNull() ?: valueStr.replace(',', '.').toDoubleOrNull()
|
||||
?: return null
|
||||
|
||||
val converters = listOf(
|
||||
lazy { MassConverter(context) },
|
||||
lazy { LengthConverter(context) },
|
||||
lazy { CurrencyConverter() },
|
||||
lazy { DataConverter(context) },
|
||||
lazy { TimeConverter(context) },
|
||||
lazy { VelocityConverter(context) },
|
||||
lazy { AreaConverter(context) },
|
||||
lazy { TemperatureConverter(context) }
|
||||
val converters = mutableListOf(
|
||||
MassConverter(context),
|
||||
LengthConverter(context),
|
||||
DataConverter(context),
|
||||
TimeConverter(context),
|
||||
VelocityConverter(context),
|
||||
AreaConverter(context),
|
||||
TemperatureConverter(context)
|
||||
)
|
||||
for (conv in converters) {
|
||||
val converter = conv.value
|
||||
|
||||
if (includeCurrencies) converters.add(CurrencyConverter(currencyRepository))
|
||||
|
||||
for (converter in converters) {
|
||||
if (!converter.isValidUnit(unitStr)) continue
|
||||
if (targetUnitStr != null && !converter.isValidUnit(targetUnitStr)) continue
|
||||
return converter.convert(context, unitStr, value, targetUnitStr)
|
||||
|
||||
+3
-4
@@ -12,14 +12,13 @@ import java.text.DecimalFormat
|
||||
import java.util.Locale
|
||||
import java.util.Currency as JCurrency
|
||||
import kotlin.math.abs
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
|
||||
class CurrencyConverter : Converter, KoinComponent {
|
||||
class CurrencyConverter(
|
||||
private val repository: CurrencyRepository,
|
||||
) : Converter {
|
||||
|
||||
override val dimension: Dimension = Dimension.Currency
|
||||
|
||||
private val repository: CurrencyRepository by inject()
|
||||
|
||||
private val topCurrencies = arrayOf("USD", "EUR", "JPY", "GBP", "AUD")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user