Updated to new Amount class which now has arithmetic operations
This commit is contained in:
parent
9f7a276cf2
commit
802a96e6dd
|
@ -328,13 +328,15 @@ open class SqliteBankingRepository(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@JvmName("mapAmount")
|
@JvmName("mapAmountNullable")
|
||||||
@JsName("mapAmount")
|
@JsName("mapAmountNullable")
|
||||||
private fun mapAmount(amount: Amount?): String? =
|
private fun mapAmount(amount: Amount?): String? =
|
||||||
amount?.let { mapAmount(it) }
|
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? =
|
private fun mapToAmount(serializedAmount: String?): Amount? =
|
||||||
serializedAmount?.let { mapToAmount(it) }
|
serializedAmount?.let { mapToAmount(it) }
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ fun HoldingListItem(holding: Holding, isOddItem: Boolean = false, isNotLastItem:
|
||||||
if (performance != null) {
|
if (performance != null) {
|
||||||
Text(
|
Text(
|
||||||
text = formatUtil.formatPercentage(performance),
|
text = formatUtil.formatPercentage(performance),
|
||||||
color = formatUtil.getColorForAmount(Amount(performance.toDouble()), showColoredAmounts)
|
color = formatUtil.getColorForAmount(Amount(performance.toString()), showColoredAmounts)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ fun TransferMoneyDialog(
|
||||||
|
|
||||||
var recipientName by remember { mutableStateOf(data.recipientName ?: "") }
|
var recipientName by remember { mutableStateOf(data.recipientName ?: "") }
|
||||||
var recipientAccountIdentifier by remember { mutableStateOf(data.recipientAccountIdentifier ?: "") }
|
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 ?: "") }
|
var paymentReference by remember { mutableStateOf(data.reference ?: "") }
|
||||||
val accountSupportsInstantTransfer by remember(senderAccount) { derivedStateOf { senderAccount.supportsInstantTransfer } }
|
val accountSupportsInstantTransfer by remember(senderAccount) { derivedStateOf { senderAccount.supportsInstantTransfer } }
|
||||||
var instantTransfer by remember { mutableStateOf(false) }
|
var instantTransfer by remember { mutableStateOf(false) }
|
||||||
|
@ -202,10 +202,10 @@ fun TransferMoneyDialog(
|
||||||
minTextLengthForSearch = 0,
|
minTextLengthForSearch = 0,
|
||||||
modifier = Modifier.weight(1f).focusRequester(amountFocus),
|
modifier = Modifier.weight(1f).focusRequester(amountFocus),
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal, imeAction = ImeAction.Next),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal, imeAction = ImeAction.Next),
|
||||||
getItemTitle = { suggestion -> suggestion.amount.amount },
|
getItemTitle = { suggestion -> suggestion.amount.toString() },
|
||||||
onEnteredTextChanged = { amount = it },
|
onEnteredTextChanged = { amount = it },
|
||||||
onSelectedItemChanged = {
|
onSelectedItemChanged = {
|
||||||
amount = it?.amount?.amount ?: ""
|
amount = it?.amount.toString()
|
||||||
if (it != null) {
|
if (it != null) {
|
||||||
paymentReference = it.reference
|
paymentReference = it.reference
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package net.codinux.banking.ui.extensions
|
|
||||||
|
|
||||||
import net.codinux.banking.client.model.Amount
|
|
||||||
|
|
||||||
fun Amount.toBigDecimal(): Double = this.amount.toDouble()
|
|
|
@ -32,9 +32,9 @@ class BankDataImporterAndExporter {
|
||||||
|
|
||||||
private fun formatAmount(amount: Amount, decimalSeparator: Char): String =
|
private fun formatAmount(amount: Amount, decimalSeparator: Char): String =
|
||||||
if (decimalSeparator == '.') {
|
if (decimalSeparator == '.') {
|
||||||
amount.amount
|
amount.toString()
|
||||||
} else {
|
} else {
|
||||||
amount.amount.replace('.', decimalSeparator)
|
amount.toString().replace('.', decimalSeparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,32 +1,26 @@
|
||||||
package net.codinux.banking.ui.service
|
package net.codinux.banking.ui.service
|
||||||
|
|
||||||
import net.codinux.banking.client.model.Amount
|
import net.codinux.banking.client.model.*
|
||||||
import net.codinux.banking.client.model.User
|
|
||||||
import net.codinux.banking.dataaccess.entities.UserEntity
|
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.AccountTransactionViewModel
|
||||||
import net.codinux.banking.ui.model.AccountTransactionsFilter
|
import net.codinux.banking.ui.model.AccountTransactionsFilter
|
||||||
|
|
||||||
class CalculatorService {
|
class CalculatorService {
|
||||||
|
|
||||||
fun sumTransactions(transactions: Collection<AccountTransactionViewModel>): Amount =
|
fun sumTransactions(transactions: Collection<AccountTransactionViewModel>): Amount =
|
||||||
// TODO: find a better solution
|
transactions.map { it.amount }.sum()
|
||||||
Amount(transactions.sumOf { it.amount.toBigDecimal() }.toString())
|
|
||||||
|
|
||||||
fun calculateBalanceOfUser(user: User): Amount =
|
fun calculateBalanceOfUser(user: User): Amount =
|
||||||
sumAmounts(user.accounts.map { it.balance })
|
sumAmounts(user.accounts.map { it.balance })
|
||||||
|
|
||||||
fun sumAmounts(amounts: Collection<Amount>): Amount =
|
fun sumAmounts(amounts: Collection<Amount>): Amount =
|
||||||
// TODO: find a better solution
|
amounts.sum()
|
||||||
Amount(amounts.sumOf { it.toBigDecimal() }.toString())
|
|
||||||
|
|
||||||
fun sumIncome(transactions: Collection<AccountTransactionViewModel>): Amount =
|
fun sumIncome(transactions: Collection<AccountTransactionViewModel>): Amount =
|
||||||
// TODO: find a better solution
|
sumAmounts(transactions.map { it.amount }.filterNot { it.isNegative })
|
||||||
Amount(transactions.map { it.amount.toBigDecimal() }.filter { it > 0 }.sum().toString())
|
|
||||||
|
|
||||||
fun sumExpenses(transactions: Collection<AccountTransactionViewModel>): Amount =
|
fun sumExpenses(transactions: Collection<AccountTransactionViewModel>): Amount =
|
||||||
// TODO: find a better solution
|
sumAmounts(transactions.map { it.amount }.filter { it.isNegative })
|
||||||
Amount(transactions.map { it.amount.toBigDecimal() }.filter { it < 0 }.sum().toString())
|
|
||||||
|
|
||||||
fun calculateBalanceOfDisplayedTransactions(transactions: Collection<AccountTransactionViewModel>, users: Collection<UserEntity>, filter: AccountTransactionsFilter): Amount {
|
fun calculateBalanceOfDisplayedTransactions(transactions: Collection<AccountTransactionViewModel>, users: Collection<UserEntity>, filter: AccountTransactionsFilter): Amount {
|
||||||
if (filter.noFiltersApplied) {
|
if (filter.noFiltersApplied) {
|
||||||
|
|
|
@ -56,7 +56,7 @@ class FormatUtil {
|
||||||
|
|
||||||
|
|
||||||
fun formatAmount(amount: Amount, currency: String): String {
|
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) {
|
fun formatCurrency(currency: String): String = when (currency) {
|
||||||
|
@ -66,7 +66,7 @@ class FormatUtil {
|
||||||
|
|
||||||
fun getColorForAmount(amount: Amount, showColoredAmounts: Boolean = true): Color = when {
|
fun getColorForAmount(amount: Amount, showColoredAmounts: Boolean = true): Color = when {
|
||||||
showColoredAmounts == false -> Color.Unspecified
|
showColoredAmounts == false -> Color.Unspecified
|
||||||
amount.amount.startsWith("-") -> Colors.Red600
|
amount.toString().startsWith("-") -> Colors.Red600
|
||||||
else -> Colors.Green600
|
else -> Colors.Green600
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ class RecipientFinder(private val bankFinder: BankFinder) {
|
||||||
|
|
||||||
transactionsByIban = transactions.filter { it.otherPartyAccountId != null }.groupBy { it.otherPartyAccountId!! }
|
transactionsByIban = transactions.filter { it.otherPartyAccountId != null }.groupBy { it.otherPartyAccountId!! }
|
||||||
.mapValues { it.value.map {
|
.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 } }
|
}.toSet().sortedByDescending { it.valueDate } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue