Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Tabular Loan UI added #3539

Merged
merged 15 commits into from
Sep 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sealed interface LoanScreenEvent {
data class OnReordered(val reorderedList: List<DisplayLoan>) : LoanScreenEvent
data class OnCreateAccount(val accountData: CreateAccountData) : LoanScreenEvent
data class OnReOrderModalShow(val show: Boolean) : LoanScreenEvent
data class OnTabChanged(val tab: LoanTab) : LoanScreenEvent
data object OnAddLoan : LoanScreenEvent
data object OnLoanModalDismiss : LoanScreenEvent
data object OnChangeDate : LoanScreenEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ivy.loans.loan

sealed interface LoanScreenMode {
data object TabularMode : LoanScreenMode
data object NonTabularMode : LoanScreenMode
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import java.time.Instant
data class LoanScreenState(
val baseCurrency: String,
val loans: ImmutableList<DisplayLoan>,
val completedLoans: ImmutableList<DisplayLoan>,
val pendingLoans: ImmutableList<DisplayLoan>,
val accounts: ImmutableList<Account>,
val selectedAccount: Account?,
val loanModalData: LoanModalData?,
val reorderModalVisible: Boolean,
val totalOweAmount: String,
val totalOwedAmount: String,
val paidOffLoanVisibility: Boolean,
val dateTime: Instant
)
val screenMode: LoanScreenMode,
val dateTime: Instant,
val selectedTab: LoanTab
)
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions screen/loans/src/main/java/com/ivy/loans/loan/LoanTab.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ivy.loans.loan

import androidx.compose.runtime.Immutable

@Immutable
enum class LoanTab {
PENDING, COMPLETED
}
53 changes: 52 additions & 1 deletion screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.ivy.data.db.dao.read.LoanRecordDao
import com.ivy.data.db.dao.read.SettingsDao
import com.ivy.data.db.dao.write.WriteLoanDao
import com.ivy.data.model.LoanType
import com.ivy.domain.features.Features
import com.ivy.frp.test.TestIdlingResource
import com.ivy.legacy.datamodel.Account
import com.ivy.legacy.datamodel.Loan
Expand Down Expand Up @@ -58,15 +59,19 @@ class LoanViewModel @Inject constructor(
private val timeConverter: TimeConverter,
private val timeProvider: TimeProvider,
private val dateTimePicker: DateTimePicker,
private val features: Features
) : ComposeViewModel<LoanScreenState, LoanScreenEvent>() {

private var baseCurrencyCode by mutableStateOf(getDefaultFIATCurrency().currencyCode)
private var loans by mutableStateOf<ImmutableList<DisplayLoan>>(persistentListOf())
private var completedLoans by mutableStateOf<ImmutableList<DisplayLoan>>(persistentListOf())
private var pendingLoans by mutableStateOf<ImmutableList<DisplayLoan>>(persistentListOf())
private var accounts by mutableStateOf<ImmutableList<Account>>(persistentListOf())
private var selectedAccount by mutableStateOf<Account?>(null)
private var loanModalData by mutableStateOf<LoanModalData?>(null)
private var reorderModalVisible by mutableStateOf(false)
private var dateTime by mutableStateOf<Instant>(timeProvider.utcNow())
private var selectedTab by mutableStateOf(LoanTab.PENDING)

/** If true paid off loans will be visible */
private var paidOffLoanVisibility by mutableStateOf(true)
Expand All @@ -93,10 +98,41 @@ class LoanViewModel @Inject constructor(
totalOweAmount = getTotalOweAmount(totalOweAmount, defaultCurrencyCode),
totalOwedAmount = getTotalOwedAmount(totalOwedAmount, defaultCurrencyCode),
paidOffLoanVisibility = getPaidOffLoanVisibility(),
dateTime = dateTime
screenMode = getScreenMode(),
dateTime = dateTime,
selectedTab = getSelectedTab(),
completedLoans = getCompletedLoans(),
pendingLoans = getPendingLoans()
)
}

fun setTab(tab: LoanTab) {
selectedTab = tab
}

@Composable
private fun getSelectedTab(): LoanTab {
return selectedTab
}

@Composable
private fun getCompletedLoans(): ImmutableList<DisplayLoan> {
return completedLoans
}

@Composable
private fun getPendingLoans(): ImmutableList<DisplayLoan> {
return pendingLoans
}

@Composable
private fun getScreenMode(): LoanScreenMode {
return when (features.tabularLoanMode.asEnabledState()) {
true -> LoanScreenMode.TabularMode
else -> LoanScreenMode.NonTabularMode
}
}

@Composable
private fun getReorderModalVisible() = reorderModalVisible

Expand Down Expand Up @@ -160,9 +196,14 @@ class LoanViewModel @Inject constructor(
is LoanScreenEvent.OnChangeDate -> {
handleChangeDate()
}

is LoanScreenEvent.OnChangeTime -> {
handleChangeTime()
}

is LoanScreenEvent.OnTabChanged -> {
setTab(event.tab)
}
}
}

Expand Down Expand Up @@ -218,6 +259,8 @@ class LoanViewModel @Inject constructor(
}.toImmutableList()
}
filterLoans()
loadPendingLoans()
loadCompletedLoans()

TestIdlingResource.decrement()
}
Expand Down Expand Up @@ -332,6 +375,14 @@ class LoanViewModel @Inject constructor(
}
}

private fun loadCompletedLoans() {
completedLoans = allLoans.filter { loan -> loan.percentPaid == 1.0 }.toImmutableList()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shamim-emon could you replace == with >= to patch #3546?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nvllz sure

}

private fun loadPendingLoans() {
pendingLoans = allLoans.filter { loan -> loan.percentPaid < 1.0 }.toImmutableList()
}

private fun createAccount(data: CreateAccountData) {
viewModelScope.launch {
TestIdlingResource.increment()
Expand Down
Loading