I have the following code, and would like to clean it up to have better separation of responsibilities.
Do I move the logic outside into a view model, create a class called AccountSummaryFormatter, or should I just throw them into a helperFunction.kt file? I have an AccountsViewModel.kt file at the top of my component tree, but I feel prop drilling is overkill.
Also, account summary sits near the bottom of component tree.
Here is the code snippet. Its function is to take a certain object type, and generate the relative UI property names for their respective values. I reuse this card multiple times, and didn't want to create multiple compose functions for something that changes slightly.
SummaryTitle is a simple enum class.
fun AccountSummary(
account: Account,
modifier: Modifier = Modifier,
toggleHUD: (Int, Int) -> Unit,
currSelection: Int,
scrollTarget: Float,
scrollState: LazyListState,
){
var btnText = if(currSelection == account.name.ordinal) "CLOSE" else "TRANSFER"
var coroutineScope = rememberCoroutineScope()
fun fetchKeyValuePairs(titleList: List<SummaryTitle>, obj: Account): List<Pair<String, Serializable>> {
return titleList.map{ title ->
title.title to when(title){
SummaryTitle.UNFUNDED -> (obj.total - obj.funded)
SummaryTitle.FUNDED -> obj.funded
SummaryTitle.BUDGET -> obj.total
SummaryTitle.SPENT -> obj.spentFunds
SummaryTitle.REMAINING -> obj.remainingFunds
SummaryTitle.UNASSIGNED -> obj.unassignedFunds
SummaryTitle.TOTAL -> obj.total
SummaryTitle.INCOME -> obj.total
SummaryTitle.WAITING -> obj.total - obj.funded
SummaryTitle.RECEIVED -> obj.funded
else -> IllegalArgumentException("Unknown title, failed to process: $title")
}
}
}
fun getListOfRowItems(account: Account): List<Pair<String, Serializable>> {
val incomeDisplayProperties = listOf(
SummaryTitle.WAITING,
SummaryTitle.RECEIVED,
SummaryTitle.INCOME,
SummaryTitle.UNASSIGNED,
)
val fixedExpenseDisplayProperties= listOf(
SummaryTitle.UNFUNDED,
SummaryTitle.FUNDED,
SummaryTitle.BUDGET,
SummaryTitle.UNASSIGNED,
SummaryTitle.SPENT,
SummaryTitle.REMAINING,
)
val emergencyFundDisplayProperties= listOf(
SummaryTitle.UNFUNDED,
SummaryTitle.FUNDED,
SummaryTitle.BUDGET,
SummaryTitle.UNASSIGNED
)
val loansDisplayProperties= listOf(
SummaryTitle.TOTAL,
SummaryTitle.UNASSIGNED
)
val savingsDisplayProperties= listOf(
SummaryTitle.TOTAL,
SummaryTitle.UNASSIGNED
)
return when (account.name) {
AccountType.INCOME -> fetchKeyValuePairs(incomeDisplayProperties, account)
AccountType.FIXED_EXPENSES -> fetchKeyValuePairs(fixedExpenseDisplayProperties, account)
AccountType.LOANS -> fetchKeyValuePairs(loansDisplayProperties, account)
AccountType.SAVINGS -> fetchKeyValuePairs(savingsDisplayProperties, account)
AccountType.EMERGENCY_FUND -> fetchKeyValuePairs(emergencyFundDisplayProperties, account)
else -> throw IllegalArgumentException("Unknown AccountType: ${account.name}")
}
}
var list = getListOfRowItems(account)
val leftList = when(list.size){
2 -> list.slice(0..1)
4 -> list.slice(0..2)
6 -> list.slice(0..2)
else -> throw IllegalArgumentException("Non accounted for list length of: ${list.size}. Currently only using summaries with 2, 4 and 6")
}
val rightList: List<Pair<String, Serializable>>? = when(list.size){
2 -> null
4 -> list.slice(3..3)
6 -> list.slice(3..5)
else -> throw IllegalArgumentException("Non accounted for list length of: ${list.size}. Currently only using summaries with 2, 4 and 6")
}
Column {...//UI code}