From 178be5161fea6dad19aca835f6128163b2626614 Mon Sep 17 00:00:00 2001 From: Vivek Bansal Date: Tue, 6 Feb 2024 11:36:10 -0500 Subject: [PATCH] feat: Paid off loans visibility toggle in Loan Screen (#2873) (#2922) --- .../java/com/ivy/loans/loan/LoanBottomBar.kt | 39 +++++++++++++++---- .../com/ivy/loans/loan/LoanScreenEvent.kt | 3 ++ .../com/ivy/loans/loan/LoanScreenState.kt | 3 +- .../java/com/ivy/loans/loan/LoanViewModel.kt | 35 +++++++++++++++-- .../java/com/ivy/loans/loan/LoansScreen.kt | 7 +++- 5 files changed, 75 insertions(+), 12 deletions(-) diff --git a/screen/loans/src/main/java/com/ivy/loans/loan/LoanBottomBar.kt b/screen/loans/src/main/java/com/ivy/loans/loan/LoanBottomBar.kt index 30575880ae..fcf6068025 100644 --- a/screen/loans/src/main/java/com/ivy/loans/loan/LoanBottomBar.kt +++ b/screen/loans/src/main/java/com/ivy/loans/loan/LoanBottomBar.kt @@ -3,28 +3,51 @@ package com.ivy.loans.loan import androidx.compose.foundation.background import androidx.compose.foundation.layout.BoxWithConstraintsScope import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp import com.ivy.legacy.IvyWalletPreview import com.ivy.resources.R import com.ivy.wallet.ui.theme.Blue import com.ivy.wallet.ui.theme.components.BackBottomBar import com.ivy.wallet.ui.theme.components.IvyButton +import com.ivy.wallet.ui.theme.components.IvyCircleButton @Composable internal fun BoxWithConstraintsScope.LoanBottomBar( + isPaidOffLoanVisible: Boolean, onClose: () -> Unit, - onAdd: () -> Unit + onAdd: () -> Unit, + onTogglePaidOffLoanVisibility: () -> Unit ) { BackBottomBar(onBack = onClose) { - IvyButton( - text = stringResource(R.string.add_loan), - iconStart = R.drawable.ic_plus - ) { - onAdd() + Row(verticalAlignment = Alignment.CenterVertically) { + // TODO: Add icon content description - need to update + IvyCircleButton( + icon = when (isPaidOffLoanVisible) { + true -> R.drawable.ic_visible + else -> R.drawable.ic_hidden + }, + backgroundPadding = 10.dp + ) { + onTogglePaidOffLoanVisibility() + } + + Spacer(Modifier.width(12.dp)) + + IvyButton( + text = stringResource(R.string.add_loan), + iconStart = R.drawable.ic_plus + ) { + onAdd() + } } } } @@ -41,8 +64,10 @@ private fun PreviewBottomBar() { } LoanBottomBar( + isPaidOffLoanVisible = false, onAdd = {}, - onClose = {} + onClose = {}, + onTogglePaidOffLoanVisibility = {} ) } } diff --git a/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenEvent.kt b/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenEvent.kt index 3d2d394525..b424268d84 100644 --- a/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenEvent.kt +++ b/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenEvent.kt @@ -11,4 +11,7 @@ sealed interface LoanScreenEvent { data class OnReOrderModalShow(val show: Boolean) : LoanScreenEvent data object OnAddLoan : LoanScreenEvent data object OnLoanModalDismiss : LoanScreenEvent + + /** Toggles paid off loans visibility */ + data object OnTogglePaidOffLoanVisibility : LoanScreenEvent } diff --git a/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenState.kt b/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenState.kt index 4c57b0351f..cea675b131 100644 --- a/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenState.kt +++ b/screen/loans/src/main/java/com/ivy/loans/loan/LoanScreenState.kt @@ -13,5 +13,6 @@ data class LoanScreenState( val loanModalData: LoanModalData?, val reorderModalVisible: Boolean, val totalOweAmount: String, - val totalOwedAmount: String + val totalOwedAmount: String, + val paidOffLoanVisibility: Boolean, ) diff --git a/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt b/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt index 4952de7b8c..6db6f12b7d 100644 --- a/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt +++ b/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt @@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.viewModelScope +import com.ivy.base.legacy.SharedPrefs import com.ivy.data.db.dao.read.LoanRecordDao import com.ivy.data.db.dao.read.SettingsDao import com.ivy.data.db.dao.write.WriteLoanDao @@ -12,7 +13,6 @@ import com.ivy.domain.ComposeViewModel import com.ivy.domain.event.AccountUpdatedEvent import com.ivy.domain.event.EventBus import com.ivy.frp.test.TestIdlingResource -import com.ivy.base.legacy.SharedPrefs import com.ivy.legacy.datamodel.Account import com.ivy.legacy.datamodel.Loan import com.ivy.legacy.domain.deprecated.logic.AccountCreator @@ -56,6 +56,12 @@ class LoanViewModel @Inject constructor( private val selectedAccount = mutableStateOf(null) private val loanModalData = mutableStateOf(null) private val reorderModalVisible = mutableStateOf(false) + + /** If true paid off loans will be visible */ + private val paidOffLoanVisibility = mutableStateOf(true) + + /** Contains all loans including both paidOff and pending*/ + private var allLoans: ImmutableList = persistentListOf() private var defaultCurrencyCode = "" private var totalOweAmount = 0.0 private var totalOwedAmount = 0.0 @@ -74,7 +80,8 @@ class LoanViewModel @Inject constructor( loanModalData = getLoanModalData(), reorderModalVisible = getReorderModalVisible(), totalOweAmount = getTotalOweAmount(totalOweAmount, defaultCurrencyCode), - totalOwedAmount = getTotalOwedAmount(totalOwedAmount, defaultCurrencyCode) + totalOwedAmount = getTotalOwedAmount(totalOwedAmount, defaultCurrencyCode), + paidOffLoanVisibility = getPaidOffLoanVisibility() ) } @@ -100,6 +107,9 @@ class LoanViewModel @Inject constructor( @Composable private fun getAccounts() = accounts.value + @Composable + private fun getPaidOffLoanVisibility(): Boolean = paidOffLoanVisibility.value + override fun onEvent(event: LoanScreenEvent) { when (event) { is LoanScreenEvent.OnLoanCreate -> { @@ -129,6 +139,10 @@ class LoanViewModel @Inject constructor( is LoanScreenEvent.OnCreateAccount -> { createAccount(event.accountData) } + + LoanScreenEvent.OnTogglePaidOffLoanVisibility -> { + updatePaidOffLoanVisibility() + } } } @@ -147,7 +161,7 @@ class LoanViewModel @Inject constructor( totalOweAmount = 0.0 totalOwedAmount = 0.0 - loans.value = ioThread { + allLoans = ioThread { loansAct(Unit) .map { loan -> val amountPaid = calculateAmountPaid(loan) @@ -177,6 +191,8 @@ class LoanViewModel @Inject constructor( ) }.toImmutableList() } + filterLoans() + TestIdlingResource.decrement() } } @@ -242,6 +258,14 @@ class LoanViewModel @Inject constructor( } } + /** It filters [allLoans] and updates [loans] based on weather to show paid off loans or not */ + private fun filterLoans() { + loans.value = when (paidOffLoanVisibility.value) { + true -> allLoans + false -> allLoans.filter { loan -> loan.percentPaid < 1.0 }.toImmutableList() + } + } + private fun createAccount(data: CreateAccountData) { viewModelScope.launch { TestIdlingResource.increment() @@ -289,4 +313,9 @@ class LoanViewModel @Inject constructor( return amount } + + private fun updatePaidOffLoanVisibility() { + paidOffLoanVisibility.value = paidOffLoanVisibility.value.not() + filterLoans() + } } diff --git a/screen/loans/src/main/java/com/ivy/loans/loan/LoansScreen.kt b/screen/loans/src/main/java/com/ivy/loans/loan/LoansScreen.kt index 71ebfe0873..bf92a25cea 100644 --- a/screen/loans/src/main/java/com/ivy/loans/loan/LoansScreen.kt +++ b/screen/loans/src/main/java/com/ivy/loans/loan/LoansScreen.kt @@ -127,9 +127,13 @@ private fun BoxWithConstraintsScope.UI( } LoanBottomBar( + isPaidOffLoanVisible = state.paidOffLoanVisibility, onAdd = { onEventHandler.invoke(LoanScreenEvent.OnAddLoan) }, + onTogglePaidOffLoanVisibility = { + onEventHandler.invoke(LoanScreenEvent.OnTogglePaidOffLoanVisibility) + }, onClose = { nav.back() }, @@ -442,7 +446,8 @@ private fun Preview() { totalOwedAmount = "1500.0 INR", loanModalData = LoanModalData(loan = null, baseCurrency = "INR"), reorderModalVisible = false, - selectedAccount = null + selectedAccount = null, + paidOffLoanVisibility = true ) IvyWalletPreview { UI(