Skip to content

Commit d526909

Browse files
committed
update
1 parent 48382e7 commit d526909

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

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

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ class SavingsAccountViewmodel(
6666
}
6767
}
6868

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

@@ -89,7 +97,11 @@ class SavingsAccountViewmodel(
8997
}
9098
}
9199

92-
/** A helper function to update the mutable state flow. */
100+
/**
101+
* A helper function to update the mutable state flow.
102+
*
103+
* @param update A lambda function that takes the current state and returns a new state.
104+
*/
93105
private fun updateState(update: (SavingsAccountState) -> SavingsAccountState) {
94106
mutableStateFlow.update(update)
95107
}
@@ -125,7 +137,11 @@ class SavingsAccountViewmodel(
125137
}
126138
}
127139

128-
/** Retries data fetching depending on network availability. */
140+
/**
141+
* A helper function to update the mutable state flow.
142+
*
143+
* @param update A lambda function that takes the current state and returns a new state.
144+
*/
129145
private fun retry() {
130146
viewModelScope.launch {
131147
if (!state.networkStatus) {
@@ -136,7 +152,9 @@ class SavingsAccountViewmodel(
136152
}
137153
}
138154

139-
/** Toggles visibility of total savings amount in UI. */
155+
/**
156+
* Toggles visibility of total savings amount in UI.
157+
* */
140158
private fun handleAmountVisible() {
141159
mutableStateFlow.update {
142160
it.copy(isAmountVisible = !state.isAmountVisible)
@@ -150,8 +168,15 @@ class SavingsAccountViewmodel(
150168
}
151169
}
152170

153-
/** Loads savings accounts for the current client. */
154-
private fun loadAccounts(selectedFilters: List<StringResource?>) {
171+
/**
172+
* Fetches accounts from the repository and applies filters.
173+
* If cached data is available, it uses it directly.
174+
*
175+
* @param selectedFilters List of selected filters to apply.
176+
*/
177+
private fun loadAccounts(
178+
selectedFilters: List<StringResource?>,
179+
) {
155180
viewModelScope.launch {
156181
updateState { it.copy(uiState = ScreenUiState.Loading) }
157182
accountsRepositoryImpl.loadAccounts(
@@ -168,7 +193,12 @@ class SavingsAccountViewmodel(
168193
}
169194
}
170195

171-
/** Handles repository response and updates UI state accordingly. */
196+
/**
197+
* Handles the result of the repository call and updates the state.
198+
*
199+
* @param dataState Result of fetching savings accounts (Success, Error, Loading).
200+
* @param selectedFilters Filters applied to the list.
201+
*/
172202
private fun handleReceivedAccounts(
173203
dataState: DataState<ClientAccounts>,
174204
selectedFilters: List<StringResource?>,
@@ -229,7 +259,14 @@ class SavingsAccountViewmodel(
229259
}
230260
}
231261

232-
/** Filters the accounts based on the selected filters (status). */
262+
263+
/**
264+
* Filters the accounts based on the selected filters (status).
265+
*
266+
* @param selectedFilters List of selected labels for filtering.
267+
* @param accounts Original unfiltered list of accounts.
268+
* @return List of accounts that match the applied filters.
269+
*/
233270
private fun filterAccounts(
234271
selectedFilters: List<StringResource?>,
235272
accounts: List<SavingAccount>,
@@ -244,7 +281,12 @@ class SavingsAccountViewmodel(
244281
return filteredByStatus.distinct()
245282
}
246283

247-
/** Sorts accounts based on the defined status order. */
284+
285+
/**
286+
* Calculates the total savings balance and updates state.
287+
*
288+
* @param accounts List of [SavingAccount] to compute totals from.
289+
*/
248290
private fun sortAccountsByStatus(accounts: List<SavingAccount>): List<SavingAccount> {
249291
return accounts.sortedWith(compareBy { state.statusOrder.indexOf(it.status?.value) })
250292
}

0 commit comments

Comments
 (0)