Updated to new Amount class which now has arithmetic operations

This commit is contained in:
dankito 2024-09-12 04:16:29 +02:00
parent 9f7a276cf2
commit 802a96e6dd
8 changed files with 19 additions and 28 deletions

View File

@ -328,13 +328,15 @@ open class SqliteBankingRepository(
)
@JvmName("mapAmount")
@JsName("mapAmount")
@JvmName("mapAmountNullable")
@JsName("mapAmountNullable")
private fun mapAmount(amount: Amount?): String? =
amount?.let { mapAmount(it) }
private fun mapAmount(amount: Amount): String = amount.amount
private fun mapAmount(amount: Amount): String = amount.toString()
@JvmName("mapToAmountNullable")
@JsName("mapToAmountNullable")
private fun mapToAmount(serializedAmount: String?): Amount? =
serializedAmount?.let { mapToAmount(it) }

View File

@ -59,7 +59,7 @@ fun HoldingListItem(holding: Holding, isOddItem: Boolean = false, isNotLastItem:
if (performance != null) {
Text(
text = formatUtil.formatPercentage(performance),
color = formatUtil.getColorForAmount(Amount(performance.toDouble()), showColoredAmounts)
color = formatUtil.getColorForAmount(Amount(performance.toString()), showColoredAmounts)
)
}
}

View File

@ -56,7 +56,7 @@ fun TransferMoneyDialog(
var recipientName by remember { mutableStateOf(data.recipientName ?: "") }
var recipientAccountIdentifier by remember { mutableStateOf(data.recipientAccountIdentifier ?: "") }
var amount by remember { mutableStateOf(data.amount?.amount ?: "") }
var amount by remember { mutableStateOf(data.amount.toString()) }
var paymentReference by remember { mutableStateOf(data.reference ?: "") }
val accountSupportsInstantTransfer by remember(senderAccount) { derivedStateOf { senderAccount.supportsInstantTransfer } }
var instantTransfer by remember { mutableStateOf(false) }
@ -202,10 +202,10 @@ fun TransferMoneyDialog(
minTextLengthForSearch = 0,
modifier = Modifier.weight(1f).focusRequester(amountFocus),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal, imeAction = ImeAction.Next),
getItemTitle = { suggestion -> suggestion.amount.amount },
getItemTitle = { suggestion -> suggestion.amount.toString() },
onEnteredTextChanged = { amount = it },
onSelectedItemChanged = {
amount = it?.amount?.amount ?: ""
amount = it?.amount.toString()
if (it != null) {
paymentReference = it.reference
}

View File

@ -1,5 +0,0 @@
package net.codinux.banking.ui.extensions
import net.codinux.banking.client.model.Amount
fun Amount.toBigDecimal(): Double = this.amount.toDouble()

View File

@ -32,9 +32,9 @@ class BankDataImporterAndExporter {
private fun formatAmount(amount: Amount, decimalSeparator: Char): String =
if (decimalSeparator == '.') {
amount.amount
amount.toString()
} else {
amount.amount.replace('.', decimalSeparator)
amount.toString().replace('.', decimalSeparator)
}
}

View File

@ -1,32 +1,26 @@
package net.codinux.banking.ui.service
import net.codinux.banking.client.model.Amount
import net.codinux.banking.client.model.User
import net.codinux.banking.client.model.*
import net.codinux.banking.dataaccess.entities.UserEntity
import net.codinux.banking.ui.extensions.toBigDecimal
import net.codinux.banking.ui.model.AccountTransactionViewModel
import net.codinux.banking.ui.model.AccountTransactionsFilter
class CalculatorService {
fun sumTransactions(transactions: Collection<AccountTransactionViewModel>): Amount =
// TODO: find a better solution
Amount(transactions.sumOf { it.amount.toBigDecimal() }.toString())
transactions.map { it.amount }.sum()
fun calculateBalanceOfUser(user: User): Amount =
sumAmounts(user.accounts.map { it.balance })
fun sumAmounts(amounts: Collection<Amount>): Amount =
// TODO: find a better solution
Amount(amounts.sumOf { it.toBigDecimal() }.toString())
amounts.sum()
fun sumIncome(transactions: Collection<AccountTransactionViewModel>): Amount =
// TODO: find a better solution
Amount(transactions.map { it.amount.toBigDecimal() }.filter { it > 0 }.sum().toString())
sumAmounts(transactions.map { it.amount }.filterNot { it.isNegative })
fun sumExpenses(transactions: Collection<AccountTransactionViewModel>): Amount =
// TODO: find a better solution
Amount(transactions.map { it.amount.toBigDecimal() }.filter { it < 0 }.sum().toString())
sumAmounts(transactions.map { it.amount }.filter { it.isNegative })
fun calculateBalanceOfDisplayedTransactions(transactions: Collection<AccountTransactionViewModel>, users: Collection<UserEntity>, filter: AccountTransactionsFilter): Amount {
if (filter.noFiltersApplied) {

View File

@ -56,7 +56,7 @@ class FormatUtil {
fun formatAmount(amount: Amount, currency: String): String {
return "${formatToTwoDecimalPlaces(amount.amount)} ${formatCurrency(currency)}"
return "${formatToTwoDecimalPlaces(amount.toString())} ${formatCurrency(currency)}"
}
fun formatCurrency(currency: String): String = when (currency) {
@ -66,7 +66,7 @@ class FormatUtil {
fun getColorForAmount(amount: Amount, showColoredAmounts: Boolean = true): Color = when {
showColoredAmounts == false -> Color.Unspecified
amount.amount.startsWith("-") -> Colors.Red600
amount.toString().startsWith("-") -> Colors.Red600
else -> Colors.Green600
}

View File

@ -45,7 +45,7 @@ class RecipientFinder(private val bankFinder: BankFinder) {
transactionsByIban = transactions.filter { it.otherPartyAccountId != null }.groupBy { it.otherPartyAccountId!! }
.mapValues { it.value.map {
PaymentDataSuggestion(it.reference ?: "", Amount(it.amount.amount.replace("-", "")), it.currency, it.valueDate)
PaymentDataSuggestion(it.reference ?: "", Amount(it.amount.toString().replace("-", "")), it.currency, it.valueDate)
}.toSet().sortedByDescending { it.valueDate } }
}