Skip to content

Commit

Permalink
feat: Paid off loans visibility toggle in Loan Screen (Ivy-Apps#2873) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivekban authored Feb 6, 2024
1 parent 32cfae3 commit 178be51
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
39 changes: 32 additions & 7 deletions screen/loans/src/main/java/com/ivy/loans/loan/LoanBottomBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}
Expand All @@ -41,8 +64,10 @@ private fun PreviewBottomBar() {
}

LoanBottomBar(
isPaidOffLoanVisible = false,
onAdd = {},
onClose = {}
onClose = {},
onTogglePaidOffLoanVisibility = {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
35 changes: 32 additions & 3 deletions screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -56,6 +56,12 @@ class LoanViewModel @Inject constructor(
private val selectedAccount = mutableStateOf<Account?>(null)
private val loanModalData = mutableStateOf<LoanModalData?>(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<DisplayLoan> = persistentListOf()
private var defaultCurrencyCode = ""
private var totalOweAmount = 0.0
private var totalOwedAmount = 0.0
Expand All @@ -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()
)
}

Expand All @@ -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 -> {
Expand Down Expand Up @@ -129,6 +139,10 @@ class LoanViewModel @Inject constructor(
is LoanScreenEvent.OnCreateAccount -> {
createAccount(event.accountData)
}

LoanScreenEvent.OnTogglePaidOffLoanVisibility -> {
updatePaidOffLoanVisibility()
}
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -177,6 +191,8 @@ class LoanViewModel @Inject constructor(
)
}.toImmutableList()
}
filterLoans()

TestIdlingResource.decrement()
}
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -289,4 +313,9 @@ class LoanViewModel @Inject constructor(

return amount
}

private fun updatePaidOffLoanVisibility() {
paidOffLoanVisibility.value = paidOffLoanVisibility.value.not()
filterLoans()
}
}
7 changes: 6 additions & 1 deletion screen/loans/src/main/java/com/ivy/loans/loan/LoansScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 178be51

Please sign in to comment.