From da477c9188a31d96424e8af6a741732d1e84d948 Mon Sep 17 00:00:00 2001 From: Dinar Khakimov <85668474+mdrlzy@users.noreply.github.com> Date: Mon, 14 Oct 2024 22:40:45 +0500 Subject: [PATCH] Replace Double with BigDecimal (#120) * Replace Double with BigDecimal * rename String.toDoubleSafe --- .../13.json | 22 +- .../14.json | 272 ++++++++++++++++++ .../dev/arkbuilders/rate/data/CurrUtils.kt | 25 +- .../dev/arkbuilders/rate/data/db/Database.kt | 9 +- .../rate/data/db/entity/RoomAsset.kt | 3 +- .../rate/data/db/entity/RoomCurrencyRate.kt | 3 +- .../rate/data/db/entity/RoomPairAlert.kt | 5 +- .../rate/data/db/entity/RoomQuickPair.kt | 3 +- .../typeconverters/BigDecimalTypeConverter.kt | 12 + .../typeconverters/ListAmountTypeConverter.kt | 12 +- .../repo/currency/CryptoCurrencyDataSource.kt | 9 +- .../repo/currency/FiatCurrencyDataSource.kt | 4 +- .../rate/data/worker/CurrencyMonitorWorker.kt | 5 +- .../arkbuilders/rate/domain/model/Amount.kt | 9 +- .../arkbuilders/rate/domain/model/Asset.kt | 5 +- .../rate/domain/model/CurrencyRate.kt | 4 +- .../rate/domain/model/PairAlert.kt | 5 +- .../rate/domain/model/QuickPair.kt | 3 +- .../domain/usecase/ConvertWithRateUseCase.kt | 13 +- .../usecase/HandlePairAlertCheckUseCase.kt | 13 +- .../pairalert/AddPairAlertViewModel.kt | 44 +-- .../pairalert/PairAlertConditionScreen.kt | 4 +- .../portfolio/AddAssetViewModel.kt | 4 +- .../portfolio/EditAssetViewModel.kt | 6 +- .../presentation/portfolio/PortfolioScreen.kt | 9 +- .../portfolio/PortfolioViewModel.kt | 3 +- .../rate/presentation/quick/AddQuickScreen.kt | 4 +- .../presentation/quick/AddQuickViewModel.kt | 15 +- .../rate/presentation/quick/QuickScreen.kt | 4 +- .../presentation/utils/NotificationUtils.kt | 3 +- 30 files changed, 437 insertions(+), 95 deletions(-) create mode 100644 app/schemas/dev.arkbuilders.rate.data.db.Database/14.json create mode 100644 app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/BigDecimalTypeConverter.kt diff --git a/app/schemas/dev.arkbuilders.rate.data.db.Database/13.json b/app/schemas/dev.arkbuilders.rate.data.db.Database/13.json index 15ee5bfd4..969f47b1c 100644 --- a/app/schemas/dev.arkbuilders.rate.data.db.Database/13.json +++ b/app/schemas/dev.arkbuilders.rate.data.db.Database/13.json @@ -2,11 +2,11 @@ "formatVersion": 1, "database": { "version": 13, - "identityHash": "2b3d1230ba4b3c84901e7ce337754efe", + "identityHash": "4309cb10f28a58d6c6611b5b58732a82", "entities": [ { "tableName": "RoomAsset", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `code` TEXT NOT NULL, `amount` REAL NOT NULL, `group` TEXT)", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `code` TEXT NOT NULL, `amount` TEXT NOT NULL, `group` TEXT)", "fields": [ { "fieldPath": "id", @@ -23,7 +23,7 @@ { "fieldPath": "amount", "columnName": "amount", - "affinity": "REAL", + "affinity": "TEXT", "notNull": true }, { @@ -44,7 +44,7 @@ }, { "tableName": "RoomCurrencyRate", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`code` TEXT NOT NULL, `currencyType` TEXT NOT NULL, `rate` REAL NOT NULL, PRIMARY KEY(`code`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`code` TEXT NOT NULL, `currencyType` TEXT NOT NULL, `rate` TEXT NOT NULL, PRIMARY KEY(`code`))", "fields": [ { "fieldPath": "code", @@ -61,7 +61,7 @@ { "fieldPath": "rate", "columnName": "rate", - "affinity": "REAL", + "affinity": "TEXT", "notNull": true } ], @@ -102,7 +102,7 @@ }, { "tableName": "RoomPairAlert", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `targetCode` TEXT NOT NULL, `baseCode` TEXT NOT NULL, `targetPrice` REAL NOT NULL, `startPrice` REAL NOT NULL, `alertPercent` REAL, `oneTimeNotRecurrent` INTEGER NOT NULL, `enabled` INTEGER NOT NULL, `lastDateTriggered` TEXT, `group` TEXT)", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `targetCode` TEXT NOT NULL, `baseCode` TEXT NOT NULL, `targetPrice` TEXT NOT NULL, `startPrice` TEXT NOT NULL, `alertPercent` REAL, `oneTimeNotRecurrent` INTEGER NOT NULL, `enabled` INTEGER NOT NULL, `lastDateTriggered` TEXT, `group` TEXT)", "fields": [ { "fieldPath": "id", @@ -125,13 +125,13 @@ { "fieldPath": "targetPrice", "columnName": "targetPrice", - "affinity": "REAL", + "affinity": "TEXT", "notNull": true }, { "fieldPath": "startPrice", "columnName": "startPrice", - "affinity": "REAL", + "affinity": "TEXT", "notNull": true }, { @@ -176,7 +176,7 @@ }, { "tableName": "RoomQuickPair", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `from` TEXT NOT NULL, `amount` REAL NOT NULL, `to` TEXT NOT NULL, `calculatedDate` TEXT NOT NULL, `pinnedDate` TEXT, `group` TEXT)", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `from` TEXT NOT NULL, `amount` TEXT NOT NULL, `to` TEXT NOT NULL, `calculatedDate` TEXT NOT NULL, `pinnedDate` TEXT, `group` TEXT)", "fields": [ { "fieldPath": "id", @@ -193,7 +193,7 @@ { "fieldPath": "amount", "columnName": "amount", - "affinity": "REAL", + "affinity": "TEXT", "notNull": true }, { @@ -266,7 +266,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '2b3d1230ba4b3c84901e7ce337754efe')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4309cb10f28a58d6c6611b5b58732a82')" ] } } \ No newline at end of file diff --git a/app/schemas/dev.arkbuilders.rate.data.db.Database/14.json b/app/schemas/dev.arkbuilders.rate.data.db.Database/14.json new file mode 100644 index 000000000..b71d294f0 --- /dev/null +++ b/app/schemas/dev.arkbuilders.rate.data.db.Database/14.json @@ -0,0 +1,272 @@ +{ + "formatVersion": 1, + "database": { + "version": 14, + "identityHash": "4309cb10f28a58d6c6611b5b58732a82", + "entities": [ + { + "tableName": "RoomAsset", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `code` TEXT NOT NULL, `amount` TEXT NOT NULL, `group` TEXT)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "code", + "columnName": "code", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "group", + "columnName": "group", + "affinity": "TEXT", + "notNull": false + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "RoomCurrencyRate", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`code` TEXT NOT NULL, `currencyType` TEXT NOT NULL, `rate` TEXT NOT NULL, PRIMARY KEY(`code`))", + "fields": [ + { + "fieldPath": "code", + "columnName": "code", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "currencyType", + "columnName": "currencyType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "rate", + "columnName": "rate", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "code" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "RoomFetchTimestamp", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`type` TEXT NOT NULL, `timestamp` TEXT NOT NULL, PRIMARY KEY(`type`))", + "fields": [ + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "timestamp", + "columnName": "timestamp", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "type" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "RoomPairAlert", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `targetCode` TEXT NOT NULL, `baseCode` TEXT NOT NULL, `targetPrice` TEXT NOT NULL, `startPrice` TEXT NOT NULL, `alertPercent` REAL, `oneTimeNotRecurrent` INTEGER NOT NULL, `enabled` INTEGER NOT NULL, `lastDateTriggered` TEXT, `group` TEXT)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "targetCode", + "columnName": "targetCode", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "baseCode", + "columnName": "baseCode", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "targetPrice", + "columnName": "targetPrice", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "startPrice", + "columnName": "startPrice", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "alertPercent", + "columnName": "alertPercent", + "affinity": "REAL", + "notNull": false + }, + { + "fieldPath": "oneTimeNotRecurrent", + "columnName": "oneTimeNotRecurrent", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "enabled", + "columnName": "enabled", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastDateTriggered", + "columnName": "lastDateTriggered", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "group", + "columnName": "group", + "affinity": "TEXT", + "notNull": false + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "RoomQuickPair", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `from` TEXT NOT NULL, `amount` TEXT NOT NULL, `to` TEXT NOT NULL, `calculatedDate` TEXT NOT NULL, `pinnedDate` TEXT, `group` TEXT)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "from", + "columnName": "from", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "to", + "columnName": "to", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "calculatedDate", + "columnName": "calculatedDate", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "pinnedDate", + "columnName": "pinnedDate", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "group", + "columnName": "group", + "affinity": "TEXT", + "notNull": false + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "RoomCodeUseStat", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`code` TEXT NOT NULL, `count` INTEGER NOT NULL, `lastUsedDate` TEXT NOT NULL, PRIMARY KEY(`code`))", + "fields": [ + { + "fieldPath": "code", + "columnName": "code", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "count", + "columnName": "count", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUsedDate", + "columnName": "lastUsedDate", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "code" + ] + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4309cb10f28a58d6c6611b5b58732a82')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/rate/data/CurrUtils.kt b/app/src/main/java/dev/arkbuilders/rate/data/CurrUtils.kt index d2d33a424..cf4f4a535 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/CurrUtils.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/CurrUtils.kt @@ -2,6 +2,7 @@ package dev.arkbuilders.rate.data import android.icu.util.Currency import dev.arkbuilders.rate.domain.model.CurrencyCode +import java.math.BigDecimal import java.math.RoundingMode import java.text.DecimalFormat import java.text.DecimalFormatSymbols @@ -34,11 +35,11 @@ object CurrUtils { return newInput.replace(leadingZeros, "") } - fun prepareToDisplay(value: Double): String { - var fractionSize = if (value > 10) 2 else 8 + fun prepareToDisplay(value: BigDecimal): String { + var fractionSize = if (value.toDouble() > 10) 2 else 8 - val fractionalPart = value.toLong() - value - if (fractionalPart == 0.0) { + val fractionalPart = value.remainder(BigDecimal.ONE) + if (fractionalPart == BigDecimal.ZERO) { fractionSize = 0 } val fractionPattern = @@ -54,9 +55,9 @@ object CurrUtils { return numberFormatter.format(value) } - fun roundOff(number: Double): String { + fun roundOff(number: BigDecimal): String { val fractionSize = - if (number > 10) 2 else 8 + if (number.toDouble() > 10) 2 else 8 val df = DecimalFormat( @@ -76,7 +77,17 @@ object CurrUtils { } } -fun String.toDoubleSafe() = +fun BigDecimal.divideArk(divisor: BigDecimal) = this.divide(divisor, 50, RoundingMode.HALF_EVEN) + +fun String.toBigDecimalArk() = + when { + this == "" -> BigDecimal.ZERO + this == "-" -> BigDecimal.ZERO + this == "." -> BigDecimal.ZERO + else -> this.toBigDecimal() + } + +fun String.toDoubleArk() = when { this == "" -> 0.0 this == "-" -> 0.0 diff --git a/app/src/main/java/dev/arkbuilders/rate/data/db/Database.kt b/app/src/main/java/dev/arkbuilders/rate/data/db/Database.kt index f1ac80773..04a7dd071 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/db/Database.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/db/Database.kt @@ -14,6 +14,7 @@ import dev.arkbuilders.rate.data.db.entity.RoomCurrencyRate import dev.arkbuilders.rate.data.db.entity.RoomFetchTimestamp import dev.arkbuilders.rate.data.db.entity.RoomPairAlert import dev.arkbuilders.rate.data.db.entity.RoomQuickPair +import dev.arkbuilders.rate.data.db.typeconverters.BigDecimalTypeConverter import dev.arkbuilders.rate.data.db.typeconverters.ListAmountTypeConverter import dev.arkbuilders.rate.data.db.typeconverters.OffsetDateTimeTypeConverter @@ -26,10 +27,14 @@ import dev.arkbuilders.rate.data.db.typeconverters.OffsetDateTimeTypeConverter RoomQuickPair::class, RoomCodeUseStat::class, ], - version = 13, + version = 14, exportSchema = true, ) -@TypeConverters(ListAmountTypeConverter::class, OffsetDateTimeTypeConverter::class) +@TypeConverters( + BigDecimalTypeConverter::class, + ListAmountTypeConverter::class, + OffsetDateTimeTypeConverter::class, +) abstract class Database : RoomDatabase() { abstract fun assetsDao(): PortfolioDao diff --git a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomAsset.kt b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomAsset.kt index 557abf419..56e8a59ea 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomAsset.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomAsset.kt @@ -3,12 +3,13 @@ package dev.arkbuilders.rate.data.db.entity import androidx.room.Entity import androidx.room.PrimaryKey import dev.arkbuilders.rate.domain.model.CurrencyCode +import java.math.BigDecimal @Entity data class RoomAsset( @PrimaryKey(autoGenerate = true) val id: Long = 0, val code: CurrencyCode, - val amount: Double, + val amount: BigDecimal, val group: String?, ) diff --git a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomCurrencyRate.kt b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomCurrencyRate.kt index d86064b6b..dbb03f6af 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomCurrencyRate.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomCurrencyRate.kt @@ -3,11 +3,12 @@ package dev.arkbuilders.rate.data.db.entity import androidx.room.Entity import androidx.room.PrimaryKey import dev.arkbuilders.rate.domain.model.CurrencyCode +import java.math.BigDecimal @Entity data class RoomCurrencyRate( @PrimaryKey val code: CurrencyCode, val currencyType: String, - val rate: Double, + val rate: BigDecimal, ) diff --git a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomPairAlert.kt b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomPairAlert.kt index 96bf641ce..3e9970304 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomPairAlert.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomPairAlert.kt @@ -3,6 +3,7 @@ package dev.arkbuilders.rate.data.db.entity import androidx.room.Entity import androidx.room.PrimaryKey import dev.arkbuilders.rate.domain.model.CurrencyCode +import java.math.BigDecimal import java.time.OffsetDateTime @Entity @@ -11,8 +12,8 @@ data class RoomPairAlert( val id: Long, val targetCode: CurrencyCode, val baseCode: CurrencyCode, - val targetPrice: Double, - val startPrice: Double, + val targetPrice: BigDecimal, + val startPrice: BigDecimal, val alertPercent: Double?, val oneTimeNotRecurrent: Boolean, val enabled: Boolean, diff --git a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomQuickPair.kt b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomQuickPair.kt index 028cb0d1a..9cdeeedd9 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomQuickPair.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/db/entity/RoomQuickPair.kt @@ -4,6 +4,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey import dev.arkbuilders.rate.domain.model.Amount import dev.arkbuilders.rate.domain.model.CurrencyCode +import java.math.BigDecimal import java.time.OffsetDateTime @Entity @@ -11,7 +12,7 @@ data class RoomQuickPair( @PrimaryKey(autoGenerate = true) val id: Long, val from: CurrencyCode, - val amount: Double, + val amount: BigDecimal, val to: List, val calculatedDate: OffsetDateTime, val pinnedDate: OffsetDateTime?, diff --git a/app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/BigDecimalTypeConverter.kt b/app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/BigDecimalTypeConverter.kt new file mode 100644 index 000000000..499bde510 --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/BigDecimalTypeConverter.kt @@ -0,0 +1,12 @@ +package dev.arkbuilders.rate.data.db.typeconverters + +import androidx.room.TypeConverter +import java.math.BigDecimal + +class BigDecimalTypeConverter { + @TypeConverter + fun fromBigDecimal(bigDecimal: BigDecimal): String = bigDecimal.toPlainString() + + @TypeConverter + fun toBigDecimal(bigDecimal: String): BigDecimal = BigDecimal(bigDecimal) +} diff --git a/app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/ListAmountTypeConverter.kt b/app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/ListAmountTypeConverter.kt index 3ceb98225..efb2f598e 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/ListAmountTypeConverter.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/db/typeconverters/ListAmountTypeConverter.kt @@ -4,17 +4,21 @@ import androidx.room.TypeConverter import com.google.gson.Gson import com.google.gson.reflect.TypeToken import dev.arkbuilders.rate.domain.model.Amount +import dev.arkbuilders.rate.domain.model.AmountStr +import dev.arkbuilders.rate.domain.model.toAmount +import dev.arkbuilders.rate.domain.model.toStrAmount class ListAmountTypeConverter { @TypeConverter fun fromListAmount(list: List): String { - val type = object : TypeToken>() {}.type - return Gson().toJson(list, type) + val type = object : TypeToken>() {}.type + val listStr = list.map { it.toStrAmount() } + return Gson().toJson(listStr, type) } @TypeConverter fun toListAmount(list: String): List { - val type = object : TypeToken>() {}.type - return Gson().fromJson(list, type) + val type = object : TypeToken>() {}.type + return Gson().fromJson>(list, type).map { it.toAmount() } } } diff --git a/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/CryptoCurrencyDataSource.kt b/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/CryptoCurrencyDataSource.kt index 1e8cec38c..06a3b64cf 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/CryptoCurrencyDataSource.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/CryptoCurrencyDataSource.kt @@ -8,6 +8,7 @@ import dev.arkbuilders.rate.domain.model.CurrencyCode import dev.arkbuilders.rate.domain.model.CurrencyName import dev.arkbuilders.rate.domain.model.CurrencyRate import dev.arkbuilders.rate.domain.model.CurrencyType +import java.math.BigDecimal import javax.inject.Inject import javax.inject.Singleton @@ -20,7 +21,13 @@ class CryptoCurrencyDataSource @Inject constructor( override suspend fun fetchRemote(): Either> { return try { cryptoAPI.getCryptoRates() - .map { CurrencyRate(currencyType, it.symbol.uppercase(), it.current_price) } + .map { + CurrencyRate( + currencyType, + it.symbol.uppercase(), + BigDecimal.valueOf(it.current_price), + ) + } .right() } catch (e: Exception) { e.left() diff --git a/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/FiatCurrencyDataSource.kt b/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/FiatCurrencyDataSource.kt index 99269cf7a..e90cfd36a 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/FiatCurrencyDataSource.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/repo/currency/FiatCurrencyDataSource.kt @@ -3,11 +3,13 @@ package dev.arkbuilders.rate.data.repo.currency import arrow.core.Either import arrow.core.left import arrow.core.right +import dev.arkbuilders.rate.data.divideArk import dev.arkbuilders.rate.data.network.api.FiatAPI import dev.arkbuilders.rate.domain.model.CurrencyCode import dev.arkbuilders.rate.domain.model.CurrencyName import dev.arkbuilders.rate.domain.model.CurrencyRate import dev.arkbuilders.rate.domain.model.CurrencyType +import java.math.BigDecimal import javax.inject.Inject import javax.inject.Singleton @@ -23,7 +25,7 @@ class FiatCurrencyDataSource @Inject constructor( CurrencyRate( currencyType, code, - 1.0 / rate, + BigDecimal.ONE.divideArk(BigDecimal.valueOf(rate)), ) }.right() } catch (e: Throwable) { diff --git a/app/src/main/java/dev/arkbuilders/rate/data/worker/CurrencyMonitorWorker.kt b/app/src/main/java/dev/arkbuilders/rate/data/worker/CurrencyMonitorWorker.kt index cd3dd811b..8acc56593 100644 --- a/app/src/main/java/dev/arkbuilders/rate/data/worker/CurrencyMonitorWorker.kt +++ b/app/src/main/java/dev/arkbuilders/rate/data/worker/CurrencyMonitorWorker.kt @@ -8,6 +8,7 @@ import dev.arkbuilders.rate.domain.model.TimestampType import dev.arkbuilders.rate.domain.repo.TimestampRepo import dev.arkbuilders.rate.domain.usecase.HandlePairAlertCheckUseCase import dev.arkbuilders.rate.presentation.utils.NotificationUtils +import java.math.BigDecimal class CurrencyMonitorWorker( private val context: Context, @@ -30,9 +31,9 @@ class CurrencyMonitorWorker( private fun notifyPair( pairAlert: PairAlert, - curRatio: Double, + curRatio: BigDecimal, ) { - NotificationUtils.showPairAlert(pairAlert, curRatio, context) + NotificationUtils.showPairAlert(pairAlert, context) } companion object { diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/model/Amount.kt b/app/src/main/java/dev/arkbuilders/rate/domain/model/Amount.kt index 646941719..9f064dcc5 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/model/Amount.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/model/Amount.kt @@ -1,11 +1,12 @@ package dev.arkbuilders.rate.domain.model -import dev.arkbuilders.rate.data.toDoubleSafe +import dev.arkbuilders.rate.data.toBigDecimalArk +import java.math.BigDecimal -data class Amount(val code: CurrencyCode, val value: Double) +data class Amount(val code: CurrencyCode, val value: BigDecimal) data class AmountStr(val code: CurrencyCode, val value: String) -fun Amount.toStrAmount() = AmountStr(code, value.toString()) +fun Amount.toStrAmount() = AmountStr(code, value.toPlainString()) -fun AmountStr.toDAmount() = Amount(code, value.toDoubleSafe()) +fun AmountStr.toAmount() = Amount(code, value.toBigDecimalArk()) diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/model/Asset.kt b/app/src/main/java/dev/arkbuilders/rate/domain/model/Asset.kt index 3b6f380cf..003d2b240 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/model/Asset.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/model/Asset.kt @@ -2,15 +2,16 @@ package dev.arkbuilders.rate.domain.model import android.os.Parcelable import kotlinx.parcelize.Parcelize +import java.math.BigDecimal @Parcelize data class Asset( val id: Long = 0, val code: CurrencyCode, - var value: Double, + var value: BigDecimal, val group: String? = null, ) : Parcelable { companion object { - val EMPTY = Asset(0, "", 0.0) + val EMPTY = Asset(0, "", BigDecimal.ZERO) } } diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/model/CurrencyRate.kt b/app/src/main/java/dev/arkbuilders/rate/domain/model/CurrencyRate.kt index 3ce4fcc5e..f5a7dbff9 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/model/CurrencyRate.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/model/CurrencyRate.kt @@ -1,5 +1,7 @@ package dev.arkbuilders.rate.domain.model +import java.math.BigDecimal + enum class CurrencyType { FIAT, CRYPTO, @@ -8,5 +10,5 @@ enum class CurrencyType { data class CurrencyRate( val type: CurrencyType, val code: CurrencyCode, - val rate: Double, + val rate: BigDecimal, ) diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/model/PairAlert.kt b/app/src/main/java/dev/arkbuilders/rate/domain/model/PairAlert.kt index 6d225e26c..8b2b2afff 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/model/PairAlert.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/model/PairAlert.kt @@ -1,13 +1,14 @@ package dev.arkbuilders.rate.domain.model +import java.math.BigDecimal import java.time.OffsetDateTime data class PairAlert( val id: Long, val targetCode: CurrencyCode, val baseCode: CurrencyCode, - val targetPrice: Double, - val startPrice: Double, + val targetPrice: BigDecimal, + val startPrice: BigDecimal, val percent: Double?, val oneTimeNotRecurrent: Boolean, val enabled: Boolean, diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/model/QuickPair.kt b/app/src/main/java/dev/arkbuilders/rate/domain/model/QuickPair.kt index e38a76951..3b49191ab 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/model/QuickPair.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/model/QuickPair.kt @@ -1,11 +1,12 @@ package dev.arkbuilders.rate.domain.model +import java.math.BigDecimal import java.time.OffsetDateTime data class QuickPair( val id: Long, val from: CurrencyCode, - val amount: Double, + val amount: BigDecimal, val to: List, val calculatedDate: OffsetDateTime, val pinnedDate: OffsetDateTime?, diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/usecase/ConvertWithRateUseCase.kt b/app/src/main/java/dev/arkbuilders/rate/domain/usecase/ConvertWithRateUseCase.kt index 7bbb0ce19..743f786e9 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/usecase/ConvertWithRateUseCase.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/usecase/ConvertWithRateUseCase.kt @@ -1,9 +1,11 @@ package dev.arkbuilders.rate.domain.usecase +import dev.arkbuilders.rate.data.divideArk import dev.arkbuilders.rate.domain.model.Amount import dev.arkbuilders.rate.domain.model.CurrencyCode import dev.arkbuilders.rate.domain.model.CurrencyRate import dev.arkbuilders.rate.domain.repo.CurrencyRepo +import java.math.BigDecimal import javax.inject.Inject import javax.inject.Singleton @@ -19,15 +21,16 @@ class ConvertWithRateUseCase @Inject constructor( ) { suspend operator fun invoke( fromCode: CurrencyCode, - fromValue: Double, + fromValue: BigDecimal = BigDecimal.ONE, toCode: CurrencyCode, _rates: Map? = null, - ): Pair { + ): Pair { val rates = _rates ?: currencyRepo.getCodeToCurrencyRate().getOrNull()!! - val toRate = - rates[fromCode]!!.rate / rates[toCode]!!.rate + val fromRate = rates[fromCode]!!.rate + val toRate = rates[toCode]!!.rate + val rate = fromRate.divideArk(toRate) - return Amount(toCode, fromValue * toRate) to toRate + return Amount(toCode, fromValue * rate) to rate } suspend operator fun invoke( diff --git a/app/src/main/java/dev/arkbuilders/rate/domain/usecase/HandlePairAlertCheckUseCase.kt b/app/src/main/java/dev/arkbuilders/rate/domain/usecase/HandlePairAlertCheckUseCase.kt index c0d5ff237..0ec162cfb 100644 --- a/app/src/main/java/dev/arkbuilders/rate/domain/usecase/HandlePairAlertCheckUseCase.kt +++ b/app/src/main/java/dev/arkbuilders/rate/domain/usecase/HandlePairAlertCheckUseCase.kt @@ -4,12 +4,14 @@ import arrow.core.Either import arrow.core.getOrElse import arrow.core.left import arrow.core.right +import dev.arkbuilders.rate.data.divideArk import dev.arkbuilders.rate.domain.model.Amount import dev.arkbuilders.rate.domain.model.CurrencyCode import dev.arkbuilders.rate.domain.model.CurrencyRate import dev.arkbuilders.rate.domain.model.PairAlert import dev.arkbuilders.rate.domain.repo.CurrencyRepo import dev.arkbuilders.rate.domain.repo.PairAlertRepo +import java.math.BigDecimal import java.time.OffsetDateTime import javax.inject.Inject import javax.inject.Singleton @@ -21,7 +23,7 @@ class HandlePairAlertCheckUseCase @Inject constructor( private val convertUseCase: ConvertWithRateUseCase, ) { // PairAlert to current rate - suspend operator fun invoke(): Either>> { + suspend operator fun invoke(): Either>> { val rates = currencyRepo.getCodeToCurrencyRate().getOrElse { return it.left() @@ -59,7 +61,10 @@ class HandlePairAlertCheckUseCase @Inject constructor( private suspend fun handleRecurrentPair(pairAlert: PairAlert) { val updatedTargetPrice = pairAlert.percent?.let { percent -> - (1 + percent / 100) * pairAlert.targetPrice + val percentFactor = + BigDecimal.ONE + + BigDecimal.valueOf(percent).divideArk(BigDecimal.valueOf(100)) + pairAlert.targetPrice * percentFactor } ?: let { val diff = (pairAlert.targetPrice - pairAlert.startPrice) pairAlert.targetPrice + diff @@ -76,10 +81,10 @@ class HandlePairAlertCheckUseCase @Inject constructor( private suspend fun isConditionMet( rates: Map, pairAlert: PairAlert, - ): Pair { + ): Pair { val (_, rate) = convertUseCase.invoke( - Amount(pairAlert.baseCode, 1.0), + Amount(pairAlert.baseCode, BigDecimal.ONE), pairAlert.targetCode, rates, ) diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/AddPairAlertViewModel.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/AddPairAlertViewModel.kt index 79bb0d53d..3ce7842aa 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/AddPairAlertViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/AddPairAlertViewModel.kt @@ -8,7 +8,9 @@ import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import dev.arkbuilders.rate.data.CurrUtils -import dev.arkbuilders.rate.data.toDoubleSafe +import dev.arkbuilders.rate.data.divideArk +import dev.arkbuilders.rate.data.toBigDecimalArk +import dev.arkbuilders.rate.data.toDoubleArk import dev.arkbuilders.rate.domain.model.CurrencyCode import dev.arkbuilders.rate.domain.model.PairAlert import dev.arkbuilders.rate.domain.repo.AnalyticsManager @@ -26,12 +28,13 @@ import org.orbitmvi.orbit.syntax.simple.intent import org.orbitmvi.orbit.syntax.simple.postSideEffect import org.orbitmvi.orbit.syntax.simple.reduce import org.orbitmvi.orbit.viewmodel.container +import java.math.BigDecimal data class AddPairAlertScreenState( val targetCode: CurrencyCode = "BTC", val baseCode: CurrencyCode = "USD", val priceOrPercent: Either = Either.Left(""), - val currentPrice: Double = 0.0, + val currentPrice: BigDecimal = BigDecimal.ZERO, val aboveNotBelow: Boolean = true, val group: String? = null, val oneTimeNotRecurrent: Boolean = true, @@ -94,7 +97,6 @@ class AddPairAlertViewModel( val (_, currentPrice) = convertUseCase( fromCode = target, - fromValue = 1.0, toCode = base, ) val newState = @@ -175,17 +177,19 @@ class AddPairAlertViewModel( state.priceOrPercent.fold( ifLeft = { price -> if (state.oneTimeNotRecurrent) - price.toDoubleSafe() + price.toBigDecimalArk() else - (state.currentPrice + price.toDoubleSafe()) + (state.currentPrice + price.toBigDecimalArk()) }, - ifRight = { - percent -> - (state.currentPrice * (1.0 + percent.toDoubleSafe() / 100)) + ifRight = { percent -> + val percentFactor = + BigDecimal.ONE + + percent.toBigDecimalArk().divideArk(BigDecimal.valueOf(100)) + state.currentPrice * percentFactor }, ) - val percent = state.priceOrPercent.getOrNull()?.toDoubleSafe() + val percent = state.priceOrPercent.getOrNull()?.toDoubleArk() val id = if (state.editExisting) pairAlertId!! else 0 @@ -262,13 +266,13 @@ class AddPairAlertViewModel( state.priceOrPercent.fold( ifLeft = { price -> if (state.oneTimeNotRecurrent) { - price.toDoubleSafe() > state.currentPrice + price.toBigDecimalArk() > state.currentPrice } else { - price.toDoubleSafe() > 0 + price.toDoubleArk() > 0 } }, ifRight = { percent -> - percent.toDoubleSafe() > 0 + percent.toDoubleArk() > 0 }, ) reduce { @@ -280,9 +284,9 @@ class AddPairAlertViewModel( return state.priceOrPercent .mapLeft { price -> if (state.oneTimeNotRecurrent) { - CurrUtils.roundOff(state.currentPrice * 1.1) + CurrUtils.roundOff(state.currentPrice * INITIAL_ONE_TIME_SCALE) } else { - CurrUtils.roundOff(state.currentPrice / 10) + CurrUtils.roundOff(state.currentPrice / INITIAL_RECURRENT_SCALE) } } .map { percent -> @@ -296,7 +300,7 @@ class AddPairAlertViewModel( val priceOrPercent = pair.percent?.let { percent -> - Either.Right(CurrUtils.roundOff(percent)) + Either.Right(CurrUtils.roundOff(percent.toBigDecimal())) } ?: let { Either.Left( if (pair.oneTimeNotRecurrent) @@ -308,7 +312,6 @@ class AddPairAlertViewModel( val (_, currentPrice) = convertUseCase( fromCode = pair.targetCode, - fromValue = 1.0, toCode = pair.baseCode, ) val state = @@ -331,8 +334,8 @@ class AddPairAlertViewModel( val priceOrPercentNotSuit = state.priceOrPercent.fold( - ifLeft = { it.toDoubleSafe() == 0.0 }, - ifRight = { it.toDoubleSafe() == 0.0 }, + ifLeft = { it.toDoubleArk() == 0.0 }, + ifRight = { it.toDoubleArk() == 0.0 }, ) if (priceOrPercentNotSuit) enabled = false @@ -342,6 +345,11 @@ class AddPairAlertViewModel( reduce { state.copy(finishEnabled = enabled) } } + + companion object { + private val INITIAL_ONE_TIME_SCALE = BigDecimal.valueOf(1.1) + private val INITIAL_RECURRENT_SCALE = BigDecimal.valueOf(10) + } } class AddPairAlertViewModelFactory @AssistedInject constructor( diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/PairAlertConditionScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/PairAlertConditionScreen.kt index 66f5e34f1..7ad0ff91f 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/PairAlertConditionScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/pairalert/PairAlertConditionScreen.kt @@ -414,8 +414,8 @@ private val previewPairAlert = id = 0, targetCode = "USD", baseCode = "EUR", - targetPrice = 2.0, - startPrice = 1.0, + targetPrice = 2.0.toBigDecimal(), + startPrice = 1.0.toBigDecimal(), percent = null, oneTimeNotRecurrent = true, enabled = true, diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/AddAssetViewModel.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/AddAssetViewModel.kt index 49cadd7db..f26bb122b 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/AddAssetViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/AddAssetViewModel.kt @@ -4,7 +4,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope import dev.arkbuilders.rate.data.CurrUtils -import dev.arkbuilders.rate.data.toDoubleSafe +import dev.arkbuilders.rate.data.toBigDecimalArk import dev.arkbuilders.rate.domain.model.AmountStr import dev.arkbuilders.rate.domain.model.Asset import dev.arkbuilders.rate.domain.repo.AnalyticsManager @@ -119,7 +119,7 @@ class AddAssetViewModel( state.currencies.map { Asset( code = it.code, - value = it.value.toDoubleSafe(), + value = it.value.toBigDecimalArk(), group = state.group, ) } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/EditAssetViewModel.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/EditAssetViewModel.kt index 56794dd9f..7a70511c6 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/EditAssetViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/EditAssetViewModel.kt @@ -7,7 +7,7 @@ import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import dev.arkbuilders.rate.data.CurrUtils -import dev.arkbuilders.rate.data.toDoubleSafe +import dev.arkbuilders.rate.data.toBigDecimalArk import dev.arkbuilders.rate.domain.model.Asset import dev.arkbuilders.rate.domain.model.CurrencyName import dev.arkbuilders.rate.domain.repo.AnalyticsManager @@ -58,11 +58,11 @@ class EditAssetViewModel( val name = currencyRepo.nameByCodeUnsafe(asset!!.code) inputFlow.debounce(PERSIST_AMOUNT_DEBOUNCE).onEach { - assetsRepo.setAsset(asset.copy(value = it.toDoubleSafe())) + assetsRepo.setAsset(asset.copy(value = it.toBigDecimalArk())) }.launchIn(viewModelScope) reduce { - state.copy(asset, name, asset.value.toString(), initialized = true) + state.copy(asset, name, asset.value.toPlainString(), initialized = true) } } diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioScreen.kt index 239a58e15..ee1787886 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioScreen.kt @@ -62,6 +62,7 @@ import dev.arkbuilders.rate.presentation.ui.RateSnackbarHost import dev.arkbuilders.rate.presentation.ui.SearchTextField import org.orbitmvi.orbit.compose.collectAsState import org.orbitmvi.orbit.compose.collectSideEffect +import java.math.BigDecimal @Destination @Composable @@ -145,9 +146,9 @@ fun PortfolioScreen(navigator: DestinationsNavigator) { private val previewPortfolioAmount = PortfolioDisplayAsset( - Asset(code = "EUR", value = 1100.2), - Amount(code = "USD", value = 1200.0), - ratioToBase = 1.1, + Asset(code = "EUR", value = 1100.2.toBigDecimal()), + Amount(code = "USD", value = 1200.0.toBigDecimal()), + ratioToBase = 1.1.toBigDecimal(), ) private val previewState = @@ -220,7 +221,7 @@ private fun GroupPage( onDelete: (Asset) -> Unit, ) { val total = - amounts.fold(0.0) { acc, amount -> + amounts.fold(BigDecimal.ZERO) { acc, amount -> acc + amount.baseAmount.value } val filtered = diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioViewModel.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioViewModel.kt index bdf0ab7f4..6d60b473e 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/portfolio/PortfolioViewModel.kt @@ -24,6 +24,7 @@ import org.orbitmvi.orbit.syntax.simple.intent import org.orbitmvi.orbit.syntax.simple.postSideEffect import org.orbitmvi.orbit.syntax.simple.reduce import org.orbitmvi.orbit.viewmodel.container +import java.math.BigDecimal import javax.inject.Inject import javax.inject.Singleton @@ -43,7 +44,7 @@ data class PortfolioScreenPage( data class PortfolioDisplayAsset( val asset: Asset, val baseAmount: Amount, - val ratioToBase: Double, + val ratioToBase: BigDecimal, ) sealed class PortfolioScreenEffect { diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt index cf6b458c9..7d72097c4 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickScreen.kt @@ -53,7 +53,7 @@ import com.ramcosta.composedestinations.navigation.DestinationsNavigator import com.ramcosta.composedestinations.navigation.EmptyDestinationsNavigator import dev.arkbuilders.rate.R import dev.arkbuilders.rate.data.CurrUtils -import dev.arkbuilders.rate.data.toDoubleSafe +import dev.arkbuilders.rate.data.toBigDecimalArk import dev.arkbuilders.rate.di.DIManager import dev.arkbuilders.rate.domain.model.CurrencyCode import dev.arkbuilders.rate.presentation.destinations.SearchCurrencyScreenDestination @@ -421,7 +421,7 @@ private fun ToResult( } else { Text( modifier = Modifier.padding(start = 12.dp), - text = CurrUtils.prepareToDisplay(amount.toDoubleSafe()), + text = CurrUtils.prepareToDisplay(amount.toBigDecimalArk()), color = ArkColor.TextPrimary, fontSize = 16.sp, ) diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt index 617b4e401..6c9188f9c 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/AddQuickViewModel.kt @@ -7,11 +7,12 @@ import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import dev.arkbuilders.rate.data.CurrUtils -import dev.arkbuilders.rate.data.toDoubleSafe +import dev.arkbuilders.rate.data.toBigDecimalArk +import dev.arkbuilders.rate.data.toDoubleArk import dev.arkbuilders.rate.domain.model.AmountStr import dev.arkbuilders.rate.domain.model.CurrencyCode import dev.arkbuilders.rate.domain.model.QuickPair -import dev.arkbuilders.rate.domain.model.toDAmount +import dev.arkbuilders.rate.domain.model.toAmount import dev.arkbuilders.rate.domain.model.toStrAmount import dev.arkbuilders.rate.domain.repo.AnalyticsManager import dev.arkbuilders.rate.domain.repo.CodeUseStatRepo @@ -87,7 +88,7 @@ class AddQuickViewModel( listOf( AmountStr( quickPair.from, - quickPair.amount.toString(), + quickPair.amount.toPlainString(), ), ) + quickPair.to.map { AmountStr(it.code, "") } val calc = calcToResult(currencies) @@ -151,8 +152,8 @@ class AddQuickViewModel( QuickPair( id = id, from = from.code, - amount = from.value.toDouble(), - to = state.currencies.drop(1).map { it.toDAmount() }, + amount = from.value.toBigDecimalArk(), + to = state.currencies.drop(1).map { it.toAmount() }, calculatedDate = OffsetDateTime.now(), pinnedDate = null, group = state.group, @@ -174,7 +175,7 @@ class AddQuickViewModel( if (from.value == "") { it.copy(value = "") } else { - val (amount, _) = convertUseCase.invoke(from.toDAmount(), it.code) + val (amount, _) = convertUseCase.invoke(from.toAmount(), it.code) amount.toStrAmount() } } @@ -188,7 +189,7 @@ class AddQuickViewModel( var finishEnabled = true - if (from.value.toDoubleSafe() == 0.0) + if (from.value.toDoubleArk() == 0.0) finishEnabled = false if (to.isEmpty()) diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt index 78813e1b1..f34aac172 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/quick/QuickScreen.kt @@ -525,8 +525,8 @@ private fun QuickItem( @Composable private fun PreviewItem() { QuickItem( - from = Amount("BTC", 1.0), - to = listOf(Amount("USD", 30.0)), + from = Amount("BTC", 1.0.toBigDecimal()), + to = listOf(Amount("USD", 30.0.toBigDecimal())), dateText = "Calculated on", onClick = {}, ) diff --git a/app/src/main/java/dev/arkbuilders/rate/presentation/utils/NotificationUtils.kt b/app/src/main/java/dev/arkbuilders/rate/presentation/utils/NotificationUtils.kt index ae819495d..d9fbe1e42 100644 --- a/app/src/main/java/dev/arkbuilders/rate/presentation/utils/NotificationUtils.kt +++ b/app/src/main/java/dev/arkbuilders/rate/presentation/utils/NotificationUtils.kt @@ -19,7 +19,6 @@ import dev.arkbuilders.rate.presentation.MainActivity object NotificationUtils { fun showPairAlert( pairAlert: PairAlert, - curRatio: Double, ctx: Context, ) { val pair = pairAlert @@ -39,7 +38,7 @@ object NotificationUtils { R.string.alert_notification_desc, pair.targetCode, aboveOrBelow, - pair.targetPrice.toString(), + pair.targetPrice.toPlainString(), pair.baseCode, ), )