@@ -11,7 +11,9 @@ package org.mifos.mobile.feature.home
1111
1212import androidx.compose.runtime.Immutable
1313import androidx.lifecycle.viewModelScope
14+ import co.touchlab.kermit.Logger
1415import kotlinx.collections.immutable.ImmutableList
16+ import kotlinx.collections.immutable.toImmutableList
1517import kotlinx.coroutines.CancellationException
1618import kotlinx.coroutines.flow.catch
1719import kotlinx.coroutines.flow.distinctUntilChanged
@@ -58,6 +60,10 @@ internal class HomeViewModel(
5860
5961 private var isHandlingNetworkChange = false
6062
63+ companion object {
64+ private val logger = Logger .withTag(" HomeViewModel" )
65+ }
66+
6167 init {
6268 observeNetworkStatus()
6369 loadSavedServices()
@@ -218,6 +224,21 @@ internal class HomeViewModel(
218224 mutableStateFlow.update(update)
219225 }
220226
227+ /* *
228+ * Computes the visible items based on the current edit mode and selected services.
229+ * In edit mode, all items are shown. Otherwise, only selected services are shown.
230+ */
231+ private fun computeVisibleItems (
232+ isEditMode : Boolean = state.isEditMode,
233+ selectedServices : Set <String > = state.selectedServices,
234+ ): ImmutableList <ServiceItem > {
235+ return if (isEditMode) {
236+ state.items
237+ } else {
238+ state.items.filter { selectedServices.contains(it.route) }.toImmutableList()
239+ }
240+ }
241+
221242 /* *
222243 * Toggles the visibility of the amount on the screen.
223244 */
@@ -233,10 +254,11 @@ internal class HomeViewModel(
233254 */
234255 private fun loadSavedServices () {
235256 val allRoutes = serviceCards.map { it.route }.toSet()
236- val savedServices = userPreferencesRepositoryImpl.selectedServices
257+ val savedServices = userPreferencesRepositoryImpl.selectedServices ? : allRoutes
237258 updateState {
238259 it.copy(
239- selectedServices = savedServices ? : allRoutes,
260+ selectedServices = savedServices,
261+ visibleItems = computeVisibleItems(selectedServices = savedServices),
240262 )
241263 }
242264 }
@@ -246,32 +268,51 @@ internal class HomeViewModel(
246268 viewModelScope.launch {
247269 try {
248270 userPreferencesRepositoryImpl.saveSelectedServices(state.selectedServices)
249- updateState { it.copy(isEditMode = false ) }
271+ updateState {
272+ it.copy(
273+ isEditMode = false ,
274+ visibleItems = computeVisibleItems(isEditMode = false ),
275+ )
276+ }
250277 } catch (e: CancellationException ) {
251278 throw e
252279 } catch (e: Exception ) {
280+ logger.e { " Error saving selected services: ${e.message} " }
253281 val lastSaved = userPreferencesRepositoryImpl.selectedServices
282+ val rollbackServices = lastSaved ? : serviceCards.map { it.route }.toSet()
254283 updateState {
255284 it.copy(
256285 isEditMode = false ,
257- selectedServices = lastSaved ? : serviceCards.map { it.route }.toSet(),
286+ selectedServices = rollbackServices,
287+ visibleItems = computeVisibleItems(
288+ isEditMode = false ,
289+ selectedServices = rollbackServices,
290+ ),
258291 )
259292 }
260293 }
261294 }
262295 } else {
263- updateState { it.copy(isEditMode = true ) }
296+ updateState {
297+ it.copy(
298+ isEditMode = true ,
299+ visibleItems = computeVisibleItems(isEditMode = true ),
300+ )
301+ }
264302 }
265303 }
266304
267305 private fun handleToggleServiceSelection (route : String ) {
268- updateState { currentState ->
269- val newSelection = if (currentState.selectedServices.contains(route)) {
270- currentState.selectedServices - route
271- } else {
272- currentState.selectedServices + route
273- }
274- currentState.copy(selectedServices = newSelection)
306+ val newSelection = if (state.selectedServices.contains(route)) {
307+ state.selectedServices - route
308+ } else {
309+ state.selectedServices + route
310+ }
311+ updateState {
312+ it.copy(
313+ selectedServices = newSelection,
314+ visibleItems = computeVisibleItems(selectedServices = newSelection),
315+ )
275316 }
276317 }
277318
@@ -492,6 +533,7 @@ internal data class HomeState(
492533 val isAmountVisible : Boolean = false ,
493534 val dialogState : DialogState ? = null ,
494535 val items : ImmutableList <ServiceItem >,
536+ val visibleItems : ImmutableList <ServiceItem > = items,
495537 val networkStatus : Boolean = true ,
496538 val uiState : HomeScreenState ? ,
497539 val selectedServices : Set <String > = emptySet(),
0 commit comments