Skip to content

Commit a2a508a

Browse files
committed
feat(app): implement saved filters reordering in AppBloc
- Add SavedFiltersReordered event handling to AppBloc - Implement reordering logic by updating the list of saved filters - Reuse existing AppUserContentPreferencesChanged event for persistence - Add logging for event handling and potential issues
1 parent ba55ecc commit a2a508a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
8181
on<SavedFilterAdded>(_onSavedFilterAdded);
8282
on<SavedFilterUpdated>(_onSavedFilterUpdated);
8383
on<SavedFilterDeleted>(_onSavedFilterDeleted);
84+
on<SavedFiltersReordered>(_onSavedFiltersReordered);
8485
on<AppLogoutRequested>(_onLogoutRequested);
8586

8687
// Subscribe to the authentication repository's authStateChanges stream.
@@ -914,4 +915,36 @@ class AppBloc extends Bloc<AppEvent, AppState> {
914915

915916
add(AppUserContentPreferencesChanged(preferences: updatedPreferences));
916917
}
918+
919+
/// Handles reordering the list of saved filters.
920+
///
921+
/// This method receives the complete list of filters in their new order
922+
/// and dispatches an [AppUserContentPreferencesChanged] event to persist
923+
/// the change. This approach leverages the natural order of the list for
924+
/// persistence, avoiding the need for a separate 'order' property on the
925+
/// [SavedFilter] model.
926+
Future<void> _onSavedFiltersReordered(
927+
SavedFiltersReordered event,
928+
Emitter<AppState> emit,
929+
) async {
930+
_logger.info('[AppBloc] SavedFiltersReordered event received.');
931+
932+
if (state.userContentPreferences == null) {
933+
_logger.warning(
934+
'[AppBloc] Skipping SavedFiltersReordered: UserContentPreferences not loaded.',
935+
);
936+
return;
937+
}
938+
939+
// Create an updated preferences object with the reordered list.
940+
final updatedPreferences = state.userContentPreferences!.copyWith(
941+
savedFilters: event.reorderedFilters,
942+
);
943+
944+
// Dispatch the existing event to handle persistence and state updates.
945+
// This reuses the existing logic for updating user preferences.
946+
add(AppUserContentPreferencesChanged(preferences: updatedPreferences));
947+
_logger.info('[AppBloc] Dispatched AppUserContentPreferencesChanged '
948+
'with reordered filters.');
949+
}
917950
}

0 commit comments

Comments
 (0)