From e6b9f6db3cee5068a438e72d534520b97db3fac0 Mon Sep 17 00:00:00 2001 From: PJ Date: Mon, 13 Jul 2026 12:55:38 +0530 Subject: [PATCH] feat(folio): show current balance on the add-transaction screen renders the account's balance (testTag TxnCurrentBalance) below the account name, above the credit/debit toggle, so before/after screenshots carry comparison data. --- .../folio/feature/ledger/AddTransactionScreen.kt | 14 ++++++++++++++ .../feature/ledger/AddTransactionViewModel.kt | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionScreen.kt b/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionScreen.kt index e319cec..347f071 100644 --- a/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionScreen.kt +++ b/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionScreen.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue @@ -15,10 +16,12 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import app.folio.core.data.TxnType import app.folio.di.LocalAppGraph +import app.folio.util.formatCents import app.folio.ui.BackHandler import app.folio.ui.component.AppButton import app.folio.ui.component.BackButton import app.folio.ui.component.ButtonStyle +import app.folio.ui.component.Card import app.folio.ui.component.EmptyState import app.folio.ui.component.ErrorText import app.folio.ui.component.FieldLabel @@ -26,6 +29,7 @@ import app.folio.ui.component.Header import app.folio.ui.component.Screen import app.folio.ui.component.Segmented import app.folio.ui.component.TextInput +import app.folio.ui.theme.LocalTokens import app.folio.ui.theme.Type @Composable @@ -78,10 +82,20 @@ fun AddTransactionScreen(state: AddTransactionUiState, onEvent: (AddTransactionE ) }, ) { + val t = LocalTokens.current Column( modifier = Modifier.fillMaxWidth().padding(vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(16.dp), ) { + Card { + Text("CURRENT BALANCE", style = Type.label, color = t.textMuted) + Text( + formatCents(state.balanceCents), + style = Type.balance, + color = t.text, + modifier = Modifier.testTag("TxnCurrentBalance"), + ) + } Segmented( selected = if (state.type == TxnType.credit) 0 else 1, labels = listOf("Credit", "Debit"), diff --git a/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionViewModel.kt b/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionViewModel.kt index 60bc0aa..fbb160d 100644 --- a/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionViewModel.kt +++ b/examples/folio/app/shared/src/commonMain/kotlin/app/folio/feature/ledger/AddTransactionViewModel.kt @@ -7,6 +7,7 @@ import app.folio.core.data.Repository import app.folio.core.data.TxnType import app.folio.navigation.Navigator import app.folio.navigation.Route +import app.folio.util.balanceOf import app.folio.util.parseCents import dev.zacsweers.metro.Assisted import dev.zacsweers.metro.AssistedFactory @@ -24,6 +25,7 @@ private val AMOUNT_REGEX = Regex("""^\d*(\.\d{0,2})?$""") data class AddTransactionUiState( val account: Account? = null, + val balanceCents: Long = 0, val type: TxnType = TxnType.credit, val amount: String = "", val note: String = "", @@ -50,8 +52,13 @@ class AddTransactionViewModel( val state: StateFlow = combine( form, repository.accounts.map { accounts -> accounts.firstOrNull { it.id == accountId } }, - ) { f, account -> f.copy(account = account) } - .stateIn(viewModelScope, SharingStarted.Eagerly, AddTransactionUiState()) + repository.transactions, + ) { f, account, transactions -> + f.copy( + account = account, + balanceCents = balanceOf(transactions.filter { it.accountId == accountId }), + ) + }.stateIn(viewModelScope, SharingStarted.Eagerly, AddTransactionUiState()) fun onEvent(event: AddTransactionEvent) { when (event) {