Reorganize and group modules

This commit is contained in:
MM20
2022-12-13 17:37:26 +01:00
parent bac24baad2
commit 3f8880a90a
995 changed files with 501 additions and 298 deletions
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
/
</manifest>
@@ -0,0 +1,65 @@
package de.mm20.launcher2.calculator
import de.mm20.launcher2.search.data.Calculator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.mariuszgromada.math.mxparser.Expression
interface CalculatorRepository {
fun search(query: String): Flow<Calculator?>
}
class CalculatorRepositoryImpl : CalculatorRepository, KoinComponent {
override fun search(query: String): Flow<Calculator?> = channelFlow {
if (query.isBlank()) {
send(null)
return@channelFlow
}
send(queryCalculator(query))
}
private suspend fun queryCalculator(query: String): Calculator? {
return when {
query.matches(Regex("0x[0-9a-fA-F]+")) -> {
val solution = query.substring(2).toIntOrNull(16) ?: run {
return null
}
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)
if (exp.checkSyntax()) {
Calculator(term = query, solution = exp.calculate())
} else {
val exp2 = Expression(query.replace(',', '.').replace(';', ','))
if (exp2.checkSyntax()) {
Calculator(term = query, solution = exp2.calculate())
} else null
}
}
}
}
}
}
@@ -0,0 +1,7 @@
package de.mm20.launcher2.calculator
import org.koin.dsl.module
val calculatorModule = module {
single<CalculatorRepository> { CalculatorRepositoryImpl() }
}
@@ -0,0 +1,90 @@
package de.mm20.launcher2.search.data
import de.mm20.launcher2.search.Searchable
import java.text.DecimalFormat
import java.util.*
import kotlin.math.abs
import kotlin.math.roundToInt
data class Calculator(
val term: String,
val solution: Double
): Searchable {
val formattedString: String
val formattedBinaryString: String
val formattedHexString: String
val formattedOctString: String
init {
if (solution.isNaN()) {
formattedString = "NaN"
formattedOctString = "NaN"
formattedBinaryString = "NaN"
formattedHexString = "NaN"
} else {
val nf =
if ((abs(solution) > 1e12 || abs(solution) < 1e-5) && solution != 0.0) DecimalFormat(
"#.######E0"
)
else DecimalFormat("#,###.######")
formattedString = nf.format(solution)
var s = StringBuffer(solution.roundToInt().toString(2))
while (s.length % 4 != 0) {
s = s.insert(0, '0')
}
for (i in s.length - 4 downTo 4 step 4) {
s.insert(i, ' ')
}
formattedBinaryString = s.toString()
s = StringBuffer(solution.roundToInt().toString(8))
while (s.length % 3 != 0) {
s = s.insert(0, '0')
}
for (i in s.length - 3 downTo 3 step 3) {
s.insert(i, ' ')
}
formattedOctString = s.toString()
s = StringBuffer(solution.roundToInt().toString(16).uppercase(Locale.getDefault()))
while (s.length % 2 != 0) {
s = s.insert(0, '0')
}
for (i in s.length - 2 downTo 2 step 2) {
s.insert(i, ' ')
}
formattedHexString = s.toString()
}
}
fun getBeatifiedTerm(): String {
if(term.matches(Regex("0x[0-9a-fA-F]+"))) {
return term.substring(2).uppercase(Locale.ROOT) + "₁₆"
}
if(term.matches(Regex("0b[01]+"))) {
return term.substring(2) + ""
}
if(term.matches(Regex("0[0-7]+"))) {
return term.substring(1) + ""
}
return term.replace(Regex("\\s+"), "")
.replace("pi", " \u03C0 ", ignoreCase = true)
.replace("*", " \u00D7 ")
.replace("-", " \u2212 ")
.replace("/", " \u2215 ")
.replace("+", " + ")
.replace(Regex("&{1,2}"), " \u2227 ")
.replace(Regex("\\|{1,2}"), " \u2228 ")
.replace("!=", " \u2260 ")
.replace("<>", " \u2260 ")
.replace(">=", " \u2265 ")
.replace("<=", " \u2264 ")
.replace("=", " = ")
.replace("<", " < ")
.replace(">", " > ")
}
}