Skip to content

Commit 48382e7

Browse files
committed
update
1 parent 530ef3a commit 48382e7

File tree

2 files changed

+39
-106
lines changed

2 files changed

+39
-106
lines changed

feature/savings-account/src/commonMain/kotlin/org/mifos/mobile/feature/savingsaccount/savingsAccount/SavingsAccountScreen.kt

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import org.mifos.mobile.core.ui.component.MifosErrorComponent
6161
import org.mifos.mobile.core.ui.component.MifosProgressIndicator
6262
import org.mifos.mobile.core.ui.utils.EventsEffect
6363
import org.mifos.mobile.core.ui.utils.ScreenUiState
64-
import kotlin.collections.orEmpty
6564

6665
@Composable
6766
fun SavingsAccountScreen(
@@ -92,29 +91,21 @@ fun SavingsAccountScreen(
9291
EventsEffect(viewModel.eventFlow) { event ->
9392
when (event) {
9493
is SavingsAccountsEvent.NavigateBack -> navigateBack.invoke()
95-
9694
is SavingsAccountsEvent.AccountClicked -> {
9795
onAccountClicked(Constants.SAVINGS_ACCOUNT, event.accountId)
9896
}
99-
100-
is SavingsAccountsEvent.LoadingCompleted -> {
101-
onLoadingCompleted.invoke()
102-
}
97+
is SavingsAccountsEvent.LoadingCompleted -> onLoadingCompleted.invoke()
10398
}
10499
}
105100

106101
SavingsAccountDialog(
107102
dialogState = state.dialogState,
108-
onAction = remember(viewModel) {
109-
{ viewModel.trySendAction(it) }
110-
},
103+
onAction = remember(viewModel) { { viewModel.trySendAction(it) } },
111104
)
112105

113106
SavingsAccountContent(
114107
state = state,
115-
onAction = remember(viewModel) {
116-
{ viewModel.trySendAction(it) }
117-
},
108+
onAction = remember(viewModel) { { viewModel.trySendAction(it) } },
118109
filtersClicked = filtersClicked,
119110
)
120111
}
@@ -132,7 +123,6 @@ internal fun SavingsAccountDialog(
132123
isRetryEnabled = true,
133124
)
134125
}
135-
136126
null -> Unit
137127
}
138128
}
@@ -211,19 +201,7 @@ internal fun SavingsAccountContent(
211201
)
212202
}
213203

214-
Row(
215-
horizontalArrangement = Arrangement.spacedBy(DesignToken.spacing.largeIncreased),
216-
) {
217-
// TODO : un-implemented feature,
218-
// commenting because user won't feels its good ,uncomment and implement it
219-
// Icon(
220-
// modifier = Modifier
221-
// .clickable {}
222-
// .size(20.dp),
223-
// imageVector = MifosIcons.SearchNew,
224-
// contentDescription =
225-
// stringResource(Res.string.content_description_search),
226-
// )
204+
Row(horizontalArrangement = Arrangement.spacedBy(DesignToken.spacing.largeIncreased)) {
227205
Icon(
228206
modifier = Modifier
229207
.clickable { filtersClicked() }
@@ -255,36 +233,22 @@ internal fun SavingsAccountContent(
255233
)
256234
}
257235
} else {
258-
val statusOrder = remember {
259-
listOf(
260-
SavingStatus.ACTIVE.status,
261-
SavingStatus.SUBMIT_AND_PENDING_APPROVAL.status,
262-
SavingStatus.CLOSED.status,
263-
SavingStatus.INACTIVE.status,
264-
)
265-
}
266-
val sortedAccounts = remember(state.savingsAccount) {
267-
state.savingsAccount.orEmpty().sortedWith(
268-
compareBy { statusOrder.indexOf(it.status?.value) },
269-
)
270-
}
236+
val accounts = state.savingsAccount.orEmpty()
271237
LazyColumn(
272238
modifier = Modifier
273239
.fillMaxSize()
274240
.weight(1f),
275241
) {
276-
item {
277-
Spacer(modifier = Modifier.height(DesignToken.spacing.small))
278-
}
279-
items(sortedAccounts) { account ->
242+
item { Spacer(modifier = Modifier.height(DesignToken.spacing.small)) }
243+
244+
items(accounts) { account ->
280245
val color = when (account.status?.value) {
281246
SavingStatus.ACTIVE.status -> AppColors.customEnable
282247
SavingStatus.SUBMIT_AND_PENDING_APPROVAL.status -> AppColors.customYellow
283-
284248
SavingStatus.INACTIVE.status -> MaterialTheme.colorScheme.error
285-
286249
else -> MaterialTheme.colorScheme.onSurface
287250
}
251+
288252
val accountStatus = if (account.status?.active == true) {
289253
CurrencyFormatter.format(
290254
account.accountBalance,

feature/savings-account/src/commonMain/kotlin/org/mifos/mobile/feature/savingsaccount/savingsAccount/SavingsAccountViewmodel.kt

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import org.mifos.mobile.core.common.DataState
2323
import org.mifos.mobile.core.data.repository.AccountsRepository
2424
import org.mifos.mobile.core.data.util.NetworkMonitor
2525
import org.mifos.mobile.core.datastore.UserPreferencesRepository
26+
import org.mifos.mobile.core.model.SavingStatus
2627
import org.mifos.mobile.core.model.entity.accounts.savings.SavingAccount
2728
import org.mifos.mobile.core.model.entity.client.ClientAccounts
2829
import org.mifos.mobile.core.ui.utils.BaseViewModel
2930
import org.mifos.mobile.core.ui.utils.ScreenUiState
30-
import org.mifos.mobile.core.ui.utils.ScreenUiState.Network
3131
import org.mifos.mobile.feature.savingsaccount.utils.FilterUtil
3232
import kotlin.collections.orEmpty
3333

@@ -55,9 +55,7 @@ class SavingsAccountViewmodel(
5555
observeNetwork()
5656
}
5757

58-
/**
59-
* Observes the network connectivity status and updates state accordingly.
60-
*/
58+
/** Observes the network connectivity status and updates state accordingly. */
6159
private fun observeNetwork() {
6260
viewModelScope.launch {
6361
networkMonitor.isOnline
@@ -68,15 +66,7 @@ class SavingsAccountViewmodel(
6866
}
6967
}
7068

71-
/**
72-
* Handles changes in network connectivity.
73-
*
74-
* It updates the `networkStatus` state. If the network is offline, it sets the
75-
* `uiState` to [ScreenUiState.Network]. If the network is online, it
76-
* automatically triggers a data fetch to refresh the content.
77-
*
78-
* @param isOnline A boolean indicating the current network status.
79-
*/
69+
/** Handles changes in network connectivity. */
8070
private fun handleNetworkStatus(isOnline: Boolean) {
8171
updateState { it.copy(networkStatus = isOnline) }
8272

@@ -99,11 +89,7 @@ class SavingsAccountViewmodel(
9989
}
10090
}
10191

102-
/**
103-
* A helper function to update the mutable state flow.
104-
*
105-
* @param update A lambda function that takes the current state and returns a new state.
106-
*/
92+
/** A helper function to update the mutable state flow. */
10793
private fun updateState(update: (SavingsAccountState) -> SavingsAccountState) {
10894
mutableStateFlow.update(update)
10995
}
@@ -139,11 +125,7 @@ class SavingsAccountViewmodel(
139125
}
140126
}
141127

142-
/**
143-
* Retries the data fetching process. If the network is unavailable, it shows
144-
* a network error dialog. Otherwise, it triggers the `loadAccounts` `fetchClient`,
145-
* `fetchLonPurpose` function.
146-
*/
128+
/** Retries data fetching depending on network availability. */
147129
private fun retry() {
148130
viewModelScope.launch {
149131
if (!state.networkStatus) {
@@ -154,33 +136,22 @@ class SavingsAccountViewmodel(
154136
}
155137
}
156138

157-
/**
158-
* Toggles visibility of the total savings amount in UI.
159-
*/
139+
/** Toggles visibility of total savings amount in UI. */
160140
private fun handleAmountVisible() {
161141
mutableStateFlow.update {
162142
it.copy(isAmountVisible = !state.isAmountVisible)
163143
}
164144
}
165145

166-
/**
167-
* Dismisses any active dialog in the UI.
168-
*/
146+
/** Dismisses any active dialog. */
169147
private fun handleDismissDialog() {
170148
mutableStateFlow.update {
171149
it.copy(dialogState = null)
172150
}
173151
}
174152

175-
/**
176-
* Fetches accounts from the repository and applies filters.
177-
* If cached data is available, it uses it directly.
178-
*
179-
* @param selectedFilters List of selected filters to apply.
180-
*/
181-
private fun loadAccounts(
182-
selectedFilters: List<StringResource?>,
183-
) {
153+
/** Loads savings accounts for the current client. */
154+
private fun loadAccounts(selectedFilters: List<StringResource?>) {
184155
viewModelScope.launch {
185156
updateState { it.copy(uiState = ScreenUiState.Loading) }
186157
accountsRepositoryImpl.loadAccounts(
@@ -197,12 +168,7 @@ class SavingsAccountViewmodel(
197168
}
198169
}
199170

200-
/**
201-
* Handles the result of the repository call and updates the state.
202-
*
203-
* @param dataState Result of fetching savings accounts (Success, Error, Loading).
204-
* @param selectedFilters Filters applied to the list.
205-
*/
171+
/** Handles repository response and updates UI state accordingly. */
206172
private fun handleReceivedAccounts(
207173
dataState: DataState<ClientAccounts>,
208174
selectedFilters: List<StringResource?>,
@@ -230,9 +196,10 @@ class SavingsAccountViewmodel(
230196
is DataState.Success -> {
231197
val allSavings = dataState.data.savingsAccounts.orEmpty()
232198
val filtered = filterAccounts(selectedFilters, allSavings)
199+
val sortedAccounts = sortAccountsByStatus(filtered)
233200
updateState {
234201
it.copy(
235-
decimals = filtered.firstOrNull()?.currency?.decimalPlaces ?: 2,
202+
decimals = sortedAccounts.firstOrNull()?.currency?.decimalPlaces ?: 2,
236203
)
237204
}
238205

@@ -242,12 +209,12 @@ class SavingsAccountViewmodel(
242209

243210
updateState {
244211
val isEmptyAccounts = allSavings.isEmpty()
245-
val isFilteredEmpty = filtered.isEmpty()
212+
val isFilteredEmpty = sortedAccounts.isEmpty()
246213

247214
it.copy(
248-
items = filtered.size,
215+
items = sortedAccounts.size,
249216
isFilteredEmpty = isFilteredEmpty,
250-
savingsAccount = filtered,
217+
savingsAccount = sortedAccounts,
251218
originalAccounts = allSavings,
252219
selectedFilters = selectedFilters,
253220
currency = allSavings.firstOrNull()?.currency?.displaySymbol,
@@ -262,13 +229,7 @@ class SavingsAccountViewmodel(
262229
}
263230
}
264231

265-
/**
266-
* Filters the accounts based on the selected filters (status).
267-
*
268-
* @param selectedFilters List of selected labels for filtering.
269-
* @param accounts Original unfiltered list of accounts.
270-
* @return List of accounts that match the applied filters.
271-
*/
232+
/** Filters the accounts based on the selected filters (status). */
272233
private fun filterAccounts(
273234
selectedFilters: List<StringResource?>,
274235
accounts: List<SavingAccount>,
@@ -280,15 +241,15 @@ class SavingsAccountViewmodel(
280241
} else {
281242
accounts
282243
}
283-
284244
return filteredByStatus.distinct()
285245
}
286246

287-
/**
288-
* Calculates the total savings balance and updates state.
289-
*
290-
* @param accounts List of [SavingAccount] to compute totals from.
291-
*/
247+
/** Sorts accounts based on the defined status order. */
248+
private fun sortAccountsByStatus(accounts: List<SavingAccount>): List<SavingAccount> {
249+
return accounts.sortedWith(compareBy { state.statusOrder.indexOf(it.status?.value) })
250+
}
251+
252+
/** Calculates total savings balance and updates state. */
292253
private fun getTotalSavingAmount(accounts: List<SavingAccount>?) {
293254
var amount = 0.0
294255
var items = 0
@@ -352,6 +313,14 @@ data class SavingsAccountState(
352313
val uiState: ScreenUiState? = ScreenUiState.Loading,
353314

354315
val networkStatus: Boolean = false,
316+
317+
/** Order of statuses for consistent sorting */
318+
val statusOrder: List<String> = listOf(
319+
SavingStatus.ACTIVE.status,
320+
SavingStatus.SUBMIT_AND_PENDING_APPROVAL.status,
321+
SavingStatus.CLOSED.status,
322+
SavingStatus.INACTIVE.status,
323+
),
355324
) {
356325

357326
/**

0 commit comments

Comments
 (0)