Skip to content

Commit 3d9ad2c

Browse files
refactor: make transfer handling (#2955)
1 parent 9e1c4ce commit 3d9ad2c

File tree

7 files changed

+83
-50
lines changed

7 files changed

+83
-50
lines changed

core/ui/src/commonMain/composeResources/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<string name="savings_account">Savings Account</string>
118118
<string name="available_balance">Client Name</string>
119119
<string name="select_other_payment_account">Select other payment account</string>
120+
<string name="pay_from">Pay from</string>
120121
<string name="available_balance_formatted">Available Balance - %1$s</string>
121122

122123
<string name="feature_savings_filter">Filters</string>

core/ui/src/commonMain/kotlin/org/mifos/mobile/core/ui/component/MifosDropDownPayFromComponent.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import androidx.compose.ui.zIndex
4242
import mifos_mobile.core.ui.generated.resources.Res
4343
import mifos_mobile.core.ui.generated.resources.available_balance
4444
import mifos_mobile.core.ui.generated.resources.ic_icon_dashboard
45+
import mifos_mobile.core.ui.generated.resources.pay_from
4546
import mifos_mobile.core.ui.generated.resources.savings_account
4647
import mifos_mobile.core.ui.generated.resources.select_other_payment_account
4748
import org.jetbrains.compose.resources.painterResource
@@ -58,23 +59,21 @@ fun MifosPayFromDropdownUI(
5859
accounts: List<Pair<String, String>>,
5960
onAccountSelected: (String, String) -> Unit,
6061
modifier: Modifier = Modifier,
61-
label: String = stringResource(Res.string.select_other_payment_account),
62+
selectedAccountNo: String = "",
63+
selectedAccountName: String = "",
64+
label: String = stringResource(Res.string.pay_from),
6265
) {
63-
var selectedAccount by rememberSaveable { mutableStateOf("") }
64-
var selectedBalance by rememberSaveable { mutableStateOf("") }
6566
Column {
6667
MifosDropDownPayFromComponent(
67-
accountNumber = selectedAccount,
68-
availableBalance = selectedBalance,
68+
accountNumber = selectedAccountNo,
69+
customerName = selectedAccountName,
6970
modifier = modifier,
7071
label = label,
7172
)
7273
AccountDropdownList(
7374
accounts = accounts,
74-
selectedAccount = selectedAccount,
75+
selectedAccount = selectedAccountNo,
7576
onAccountSelected = { accountNumber, balance ->
76-
selectedAccount = accountNumber
77-
selectedBalance = balance
7877
onAccountSelected(accountNumber, balance)
7978
},
8079
)
@@ -84,7 +83,7 @@ fun MifosPayFromDropdownUI(
8483
@Composable
8584
fun MifosDropDownPayFromComponent(
8685
accountNumber: String,
87-
availableBalance: String,
86+
customerName: String,
8887
label: String,
8988
modifier: Modifier = Modifier,
9089
) {
@@ -130,7 +129,7 @@ fun MifosDropDownPayFromComponent(
130129
color = AppColors.customWhite.copy(alpha = 0.5f),
131130
)
132131
Text(
133-
text = availableBalance,
132+
text = customerName,
134133
style = MifosTypography.titleMediumEmphasized,
135134
color = AppColors.customWhite,
136135
)

feature/loan-account/src/commonMain/kotlin/org/mifos/mobile/feature/loanaccount/loanAccountDetails/LoanAccountDetailsScreen.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ internal fun LoanAccountDetailsScreen(
8686
event.route == Constants.MAKE_PAYMENT -> {
8787
val transferArgs = AccountDetails(
8888
accountId = uiState.accountId,
89-
outstandingBalance = uiState.totalOutStandingBalance ?: 1.00,
9089
transferType = TRANSFER_PAY_TO,
9190
transferTarget = TransferType.SELF,
9291
transferSuccessDestination = StatusNavigationDestination.LOAN_ACCOUNT.name,

feature/third-party-transfer/src/commonMain/kotlin/org/mifos/mobile/feature/third/party/transfer/thirdPartyTransfer/TptScreen.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ internal fun TptForm(
196196
onAction(TptAction.OnFromAccountSelected(account))
197197
},
198198
label = stringResource(Res.string.feature_tpt_label_origin_account),
199+
selectedAccountNo = state.fromAccount?.accountNo ?: "",
200+
selectedAccountName = state.fromAccount?.clientName ?: "",
199201
)
200202

201203
MifosDropDownDoubleTextField(

feature/third-party-transfer/src/commonMain/kotlin/org/mifos/mobile/feature/third/party/transfer/thirdPartyTransfer/TptViewModel.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import org.mifos.mobile.core.datastore.UserPreferencesRepository
2828
import org.mifos.mobile.core.model.entity.payload.ReviewTransferPayload
2929
import org.mifos.mobile.core.model.entity.templates.account.AccountOption
3030
import org.mifos.mobile.core.model.entity.templates.account.AccountOptionsTemplate
31-
import org.mifos.mobile.core.model.enums.AccountType
3231
import org.mifos.mobile.core.ui.utils.BaseViewModel
3332
import org.mifos.mobile.core.ui.utils.ValidationHelper
3433
/**
@@ -165,6 +164,7 @@ internal class TptViewModel(
165164
* @param fromAccount The account number of the selected 'from' account.
166165
*/
167166
private fun handleFromAccountChange(fromAccount: String) {
167+
println("From Account Selected: $fromAccount")
168168
val fromAccountSelected = state.accountOptionsTemplate.fromAccountOptions
169169
.find { it.accountNo == fromAccount }
170170

@@ -194,7 +194,7 @@ internal class TptViewModel(
194194
.find { it.accountNo == toAccount }
195195

196196
val fromAccounts = state.accountOptionsTemplate.fromAccountOptions.filter {
197-
it.accountNo != toAccount && it.accountType?.value == AccountType.SAVINGS.value
197+
it.accountNo != toAccount
198198
}
199199

200200
updateState {
@@ -409,14 +409,10 @@ internal class TptViewModel(
409409
}
410410

411411
is DataState.Success -> {
412-
val savingsAccounts = dataState.data.fromAccountOptions.filter { acc ->
413-
acc.accountType?.value == AccountType.SAVINGS.value
414-
}
415-
416412
updateState {
417413
it.copy(
418414
accountOptionsTemplate = dataState.data,
419-
fromAccountOptions = savingsAccounts,
415+
fromAccountOptions = dataState.data.fromAccountOptions,
420416
toAccountOptions = dataState.data.toAccountOptions,
421417
uiState = TptState.TptScreenState.Success,
422418
)

feature/transfer-process/src/commonMain/kotlin/org/mifos/mobile/feature/transfer/process/makeTransfer/MakeTransferScreen.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ internal fun MakeTransferScreenContent(
172172
onAccountSelected = { account, balance ->
173173
onAction(MakeTransferAction.OnFromAccountSelected(account))
174174
},
175+
selectedAccountNo = state.fromAccount?.accountNo ?: "",
176+
selectedAccountName = state.fromAccount?.clientName ?: "",
175177
)
176178

177179
MifosOutlinedTextField(

feature/transfer-process/src/commonMain/kotlin/org/mifos/mobile/feature/transfer/process/makeTransfer/MakeTransferViewModel.kt

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import org.mifos.mobile.core.model.entity.client.ClientAccounts
3535
import org.mifos.mobile.core.model.entity.payload.ReviewTransferPayload
3636
import org.mifos.mobile.core.model.entity.templates.account.AccountOption
3737
import org.mifos.mobile.core.model.entity.templates.account.AccountOptionsTemplate
38+
import org.mifos.mobile.core.model.enums.AccountType
3839
import org.mifos.mobile.core.model.enums.TransferType
3940
import org.mifos.mobile.core.ui.utils.BaseViewModel
4041
import org.mifos.mobile.core.ui.utils.ValidationHelper
@@ -176,12 +177,15 @@ internal class MakeTransferViewModel(
176177
* @param toAccountNo The account number of the newly selected `to` account.
177178
*/
178179
private fun handleToAccountChange(toAccountNo: String) {
179-
val accountNo = toAccountNo
180180
val toAccountSelected = state.accountOptionsTemplate.toAccountOptions
181-
.find { it.accountNo == accountNo }
182-
val fromAccounts = state.accountOptionsTemplate.fromAccountOptions.filter {
183-
it.accountNo != accountNo
184-
}
181+
.find { it.accountNo == toAccountNo }
182+
183+
val fromAccounts = state.accountOptionsTemplate.fromAccountOptions
184+
.filter {
185+
it.accountType?.value == AccountType.SAVINGS.value &&
186+
it.accountNo != toAccountNo
187+
}
188+
185189
updateState {
186190
it.copy(
187191
toAccount = toAccountSelected,
@@ -201,12 +205,13 @@ internal class MakeTransferViewModel(
201205
* @param fromAccountNo The account number of the newly selected `from` account.
202206
*/
203207
private fun handleFromAccountChange(fromAccountNo: String) {
204-
val accountNo = fromAccountNo
205208
val fromAccountSelected = state.accountOptionsTemplate.fromAccountOptions
206-
.find { it.accountNo == accountNo }
207-
val toAccounts = state.accountOptionsTemplate.toAccountOptions.filter {
208-
it.accountNo != accountNo
209-
}
209+
.filter { it.accountType?.value == AccountType.SAVINGS.value }
210+
.find { it.accountNo == fromAccountNo }
211+
212+
val toAccounts = state.accountOptionsTemplate.toAccountOptions
213+
.filter { it.accountNo != fromAccountNo }
214+
210215
updateState {
211216
it.copy(
212217
fromAccount = fromAccountSelected,
@@ -402,36 +407,65 @@ internal class MakeTransferViewModel(
402407
)
403408
}
404409
}
410+
405411
is DataState.Success -> {
406412
updateState { current ->
407413
val template = dataState.data
408414

409-
val matchedToAccount =
410-
if (current.outstandingBalance != null) {
411-
template.toAccountOptions.firstOrNull { option ->
412-
option.accountId?.toLong() == current.accountId
415+
when (current.transferSuccessDestination) {
416+
StatusNavigationDestination.SAVINGS_ACCOUNT.name,
417+
StatusNavigationDestination.LOAN_ACCOUNT.name,
418+
-> {
419+
val savingsFromAccounts = template.fromAccountOptions.filter {
420+
it.accountType?.value == AccountType.SAVINGS.value
413421
}
414-
} else {
415-
current.toAccount
416-
}
417422

418-
val updatedFromAccountOptions =
419-
if (current.outstandingBalance != null) {
420-
template.fromAccountOptions.filterNot { option ->
421-
option.accountId?.toLong() == current.accountId
423+
val prepopulatedFromAccount = when (current.transferSuccessDestination) {
424+
StatusNavigationDestination.SAVINGS_ACCOUNT.name,
425+
StatusNavigationDestination.LOAN_ACCOUNT.name,
426+
-> {
427+
savingsFromAccounts.firstOrNull { it.accountId?.toLong() == current.accountId }
428+
}
429+
else -> current.fromAccount
422430
}
423-
} else {
424-
template.fromAccountOptions
425-
}
426431

427-
current.copy(
428-
accountOptionsTemplate = template,
429-
fromAccountOptions = updatedFromAccountOptions,
430-
toAccountOptions = template.toAccountOptions,
431-
toAccount = matchedToAccount,
432-
amount = current.outstandingBalance?.toString() ?: "",
433-
uiState = MakeTransferState.MakeTransferScreenState.Success,
434-
)
432+
val prepopulatedToAccount = when (current.transferSuccessDestination) {
433+
StatusNavigationDestination.LOAN_ACCOUNT.name -> {
434+
template.toAccountOptions.firstOrNull {
435+
it.accountId?.toLong() == current.accountId
436+
}
437+
}
438+
else -> current.toAccount
439+
}
440+
441+
val amount = current.outstandingBalance?.toString() ?: current.amount
442+
443+
val filteredFromAccounts = savingsFromAccounts.filter {
444+
it.accountNo != prepopulatedToAccount?.accountNo
445+
}
446+
val filteredToAccounts = template.toAccountOptions.filter {
447+
it.accountNo != prepopulatedFromAccount?.accountNo
448+
}
449+
450+
current.copy(
451+
accountOptionsTemplate = template,
452+
fromAccountOptions = filteredFromAccounts,
453+
toAccountOptions = filteredToAccounts,
454+
fromAccount = prepopulatedFromAccount,
455+
toAccount = prepopulatedToAccount,
456+
amount = amount,
457+
uiState = MakeTransferState.MakeTransferScreenState.Success,
458+
)
459+
}
460+
else -> {
461+
current.copy(
462+
accountOptionsTemplate = template,
463+
fromAccountOptions = template.fromAccountOptions,
464+
toAccountOptions = template.toAccountOptions,
465+
uiState = MakeTransferState.MakeTransferScreenState.Success,
466+
)
467+
}
468+
}
435469
}
436470
}
437471
}

0 commit comments

Comments
 (0)