From e41edd392ab9b32ccc3ca7e2e0c23c69fe46bced Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 29 Aug 2024 18:20:02 +0200 Subject: [PATCH] Showing balance of accounts in side menu --- .../banking/ui/appskeleton/SideMenu.kt | 2 +- .../ui/composables/NavigationMenuItem.kt | 22 ++++++++++++++++++- .../ui/composables/TransactionsList.kt | 3 +-- .../net/codinux/banking/ui/config/DI.kt | 2 ++ .../banking/ui/service/CalculatorService.kt | 6 ++++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/appskeleton/SideMenu.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/appskeleton/SideMenu.kt index fad16c4..27600d9 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/appskeleton/SideMenu.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/appskeleton/SideMenu.kt @@ -43,7 +43,7 @@ private val ItemHeight = 48.dp private val ItemHorizontalPadding = 8.dp -private val itemModifier = Modifier.height(ItemHeight).widthIn(min = 300.dp) +private val itemModifier = Modifier.height(ItemHeight).widthIn(min = 350.dp) private val iconSize = 24.dp diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/NavigationMenuItem.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/NavigationMenuItem.kt index 96ed576..0f57007 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/NavigationMenuItem.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/NavigationMenuItem.kt @@ -14,6 +14,7 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import net.codinux.banking.dataaccess.entities.BankAccountEntity @@ -24,6 +25,10 @@ import org.jetbrains.compose.resources.DrawableResource private val filterService = DI.accountTransactionsFilterService +private val calculator = DI.calculator + +private val formatUtil = DI.formatUtil + @Composable fun NavigationMenuItem( modifier: Modifier = Modifier, @@ -62,6 +67,21 @@ fun NavigationMenuItem( BankIcon(userAccount, Modifier.padding(end = iconTextSpacing), Modifier.size(iconSize), iconResource = iconResource) } - Text(text, color = textColor) + Text(text, color = textColor, modifier = Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis) + + val balance = if (bankAccount != null) { + bankAccount.balance + } else if (userAccount != null) { + calculator.calculateBalanceOfUserAccount(userAccount) + } else { + null + } + + if (balance != null) { + Text( + formatUtil.formatAmount(balance, calculator.getTransactionsCurrency(emptyList())), + color = formatUtil.getColorForAmount(balance) + ) + } } } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt index 4908367..584a779 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/composables/TransactionsList.kt @@ -19,13 +19,12 @@ import net.codinux.banking.client.model.Amount import net.codinux.banking.ui.config.Colors import net.codinux.banking.ui.config.DI import net.codinux.banking.ui.forms.RoundedCornersCard -import net.codinux.banking.ui.service.CalculatorService import net.codinux.banking.ui.state.UiState import org.jetbrains.compose.ui.tooling.preview.Preview private val filterService = DI.accountTransactionsFilterService -private val calculator = CalculatorService() +private val calculator = DI.calculator private val formatUtil = DI.formatUtil diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt index fde6540..8e4b898 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/config/DI.kt @@ -24,6 +24,8 @@ object DI { val bankIconService = BankIconService() + val calculator = CalculatorService() + val accountTransactionsFilterService = AccountTransactionsFilterService() diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/CalculatorService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/CalculatorService.kt index a516961..4183f38 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/CalculatorService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/CalculatorService.kt @@ -1,6 +1,7 @@ package net.codinux.banking.ui.service import net.codinux.banking.client.model.Amount +import net.codinux.banking.client.model.UserAccount import net.codinux.banking.dataaccess.entities.UserAccountEntity import net.codinux.banking.ui.extensions.toBigDecimal import net.codinux.banking.ui.model.AccountTransactionViewModel @@ -12,6 +13,9 @@ class CalculatorService { // TODO: find a better solution Amount(transactions.sumOf { it.amount.toBigDecimal() }.toString()) + fun calculateBalanceOfUserAccount(userAccount: UserAccount): Amount = + sumAmounts(userAccount.accounts.map { it.balance }) + fun sumAmounts(amounts: Collection): Amount = // TODO: find a better solution Amount(amounts.sumOf { it.toBigDecimal() }.toString()) @@ -35,7 +39,7 @@ class CalculatorService { if (selectedAccount.bankAccount != null) { selectedAccount.bankAccount.balance } else { - sumAmounts(selectedAccount.userAccount.accounts.map { it.balance }) + calculateBalanceOfUserAccount(selectedAccount.userAccount) } } else { sumTransactions(transactions)