@@ -23,11 +23,11 @@ import org.mifos.mobile.core.common.DataState
2323import org.mifos.mobile.core.data.repository.AccountsRepository
2424import org.mifos.mobile.core.data.util.NetworkMonitor
2525import org.mifos.mobile.core.datastore.UserPreferencesRepository
26+ import org.mifos.mobile.core.model.SavingStatus
2627import org.mifos.mobile.core.model.entity.accounts.savings.SavingAccount
2728import org.mifos.mobile.core.model.entity.client.ClientAccounts
2829import org.mifos.mobile.core.ui.utils.BaseViewModel
2930import org.mifos.mobile.core.ui.utils.ScreenUiState
30- import org.mifos.mobile.core.ui.utils.ScreenUiState.Network
3131import org.mifos.mobile.feature.savingsaccount.utils.FilterUtil
3232import 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