Refactor search
This commit is contained in:
@@ -45,7 +45,6 @@ dependencies {
|
||||
|
||||
implementation(libs.koin.android)
|
||||
|
||||
implementation(project(":preferences"))
|
||||
implementation(project(":base"))
|
||||
|
||||
}
|
||||
@@ -1,15 +1,11 @@
|
||||
package de.mm20.launcher2.calculator
|
||||
|
||||
import de.mm20.launcher2.preferences.LauncherDataStore
|
||||
import de.mm20.launcher2.search.data.Calculator
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import org.mariuszgromada.math.mxparser.Expression
|
||||
|
||||
interface CalculatorRepository {
|
||||
@@ -18,21 +14,14 @@ interface CalculatorRepository {
|
||||
|
||||
class CalculatorRepositoryImpl : CalculatorRepository, KoinComponent {
|
||||
|
||||
private val dataStore: LauncherDataStore by inject()
|
||||
|
||||
override fun search(query: String): Flow<Calculator?> = channelFlow {
|
||||
if (query.isBlank()) {
|
||||
send(null)
|
||||
return@channelFlow
|
||||
}
|
||||
val searchCalculator = dataStore.data.map { it.calculatorSearch.enabled }
|
||||
searchCalculator.collectLatest {
|
||||
if (it) {
|
||||
send(queryCalculator(query))
|
||||
} else {
|
||||
send(null)
|
||||
}
|
||||
}
|
||||
|
||||
send(queryCalculator(query))
|
||||
}
|
||||
|
||||
private suspend fun queryCalculator(query: String): Calculator? {
|
||||
@@ -43,18 +32,21 @@ class CalculatorRepositoryImpl : CalculatorRepository, KoinComponent {
|
||||
}
|
||||
Calculator(term = query, solution = solution.toDouble())
|
||||
}
|
||||
|
||||
query.matches(Regex("0b[01]+")) -> {
|
||||
val solution = query.substring(2).toIntOrNull(2) ?: run {
|
||||
return null
|
||||
}
|
||||
Calculator(term = query, solution = solution.toDouble())
|
||||
}
|
||||
|
||||
query.matches(Regex("0[0-7]+")) -> {
|
||||
val solution = query.substring(1).toIntOrNull(8) ?: run {
|
||||
return null
|
||||
}
|
||||
Calculator(term = query, solution = solution.toDouble())
|
||||
}
|
||||
|
||||
else -> {
|
||||
withContext(Dispatchers.IO) {
|
||||
val exp = Expression(query)
|
||||
|
||||
@@ -11,12 +11,6 @@ data class Calculator(
|
||||
val solution: Double
|
||||
): Searchable {
|
||||
|
||||
override val domain: String
|
||||
get() = "calculator"
|
||||
|
||||
override val key: String
|
||||
get() = "calculator://$term"
|
||||
|
||||
val formattedString: String
|
||||
val formattedBinaryString: String
|
||||
val formattedHexString: String
|
||||
|
||||
Reference in New Issue
Block a user