|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 4 | +import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart'; |
| 5 | +import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/widgets/save_filter_dialog.dart'; |
| 6 | +import 'package:flutter_news_app_mobile_client_full_source_code/l10n/l10n.dart'; |
| 7 | +import 'package:ui_kit/ui_kit.dart'; |
| 8 | + |
| 9 | +/// {@template saved_filters_page} |
| 10 | +/// A page for managing saved feed filters, allowing users to reorder, |
| 11 | +/// rename, or delete them. |
| 12 | +/// |
| 13 | +/// Reordering is handled via a [ReorderableListView], which dispatches a |
| 14 | +/// [SavedFiltersReordered] event to the [AppBloc] to persist the new order. |
| 15 | +/// Renaming and deletion are handled via a [PopupMenuButton] on each list item. |
| 16 | +/// {@endtemplate} |
| 17 | +class SavedFiltersPage extends StatelessWidget { |
| 18 | + /// {@macro saved_filters_page} |
| 19 | + const SavedFiltersPage({super.key}); |
| 20 | + |
| 21 | + @override |
| 22 | + Widget build(BuildContext context) { |
| 23 | + final l10n = AppLocalizationsX(context).l10n; |
| 24 | + final theme = Theme.of(context); |
| 25 | + |
| 26 | + return Scaffold( |
| 27 | + appBar: AppBar( |
| 28 | + title: Text( |
| 29 | + // Use the correct localization key for the page title. |
| 30 | + l10n.savedFiltersPageTitle, |
| 31 | + style: theme.textTheme.titleLarge, |
| 32 | + ), |
| 33 | + ), |
| 34 | + body: BlocBuilder<AppBloc, AppState>( |
| 35 | + builder: (context, state) { |
| 36 | + final savedFilters = state.userContentPreferences?.savedFilters ?? []; |
| 37 | + |
| 38 | + if (savedFilters.isEmpty) { |
| 39 | + return InitialStateWidget( |
| 40 | + icon: Icons.filter_list_off_outlined, |
| 41 | + headline: l10n.savedFiltersEmptyHeadline, |
| 42 | + subheadline: l10n.savedFiltersEmptySubheadline, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return ReorderableListView.builder( |
| 47 | + // Disable the default trailing drag handles to use only the |
| 48 | + // custom leading handle. |
| 49 | + buildDefaultDragHandles: false, |
| 50 | + itemCount: savedFilters.length, |
| 51 | + itemBuilder: (context, index) { |
| 52 | + final filter = savedFilters[index]; |
| 53 | + return ListTile( |
| 54 | + // A key is required for ReorderableListView to work correctly. |
| 55 | + key: ValueKey(filter.id), |
| 56 | + leading: ReorderableDragStartListener( |
| 57 | + index: index, |
| 58 | + child: const Icon(Icons.drag_handle), |
| 59 | + ), |
| 60 | + title: Text(filter.name), |
| 61 | + trailing: PopupMenuButton<String>( |
| 62 | + onSelected: (value) { |
| 63 | + switch (value) { |
| 64 | + case 'rename': |
| 65 | + showDialog<void>( |
| 66 | + context: context, |
| 67 | + builder: (_) => SaveFilterDialog( |
| 68 | + initialValue: filter.name, |
| 69 | + onSave: (newName) { |
| 70 | + final updatedFilter = filter.copyWith( |
| 71 | + name: newName, |
| 72 | + ); |
| 73 | + context.read<AppBloc>().add( |
| 74 | + SavedFilterUpdated(filter: updatedFilter), |
| 75 | + ); |
| 76 | + }, |
| 77 | + ), |
| 78 | + ); |
| 79 | + case 'delete': |
| 80 | + context.read<AppBloc>().add( |
| 81 | + SavedFilterDeleted(filterId: filter.id), |
| 82 | + ); |
| 83 | + } |
| 84 | + }, |
| 85 | + itemBuilder: (BuildContext context) => |
| 86 | + <PopupMenuEntry<String>>[ |
| 87 | + PopupMenuItem<String>( |
| 88 | + value: 'rename', |
| 89 | + child: Text(l10n.savedFiltersMenuRename), |
| 90 | + ), |
| 91 | + PopupMenuItem<String>( |
| 92 | + value: 'delete', |
| 93 | + child: Text( |
| 94 | + l10n.savedFiltersMenuDelete, |
| 95 | + style: TextStyle(color: theme.colorScheme.error), |
| 96 | + ), |
| 97 | + ), |
| 98 | + ], |
| 99 | + ), |
| 100 | + ); |
| 101 | + }, |
| 102 | + onReorder: (oldIndex, newIndex) { |
| 103 | + // This adjustment is necessary when moving an item downwards |
| 104 | + // in the list. |
| 105 | + if (oldIndex < newIndex) { |
| 106 | + newIndex -= 1; |
| 107 | + } |
| 108 | + |
| 109 | + // Create a mutable copy of the list. |
| 110 | + final reorderedList = List<SavedFilter>.from(savedFilters); |
| 111 | + |
| 112 | + // Remove the item from its old position and insert it into the |
| 113 | + // new position. |
| 114 | + final movedFilter = reorderedList.removeAt(oldIndex); |
| 115 | + reorderedList.insert(newIndex, movedFilter); |
| 116 | + |
| 117 | + // Dispatch the event to the AppBloc to persist the new order. |
| 118 | + context.read<AppBloc>().add( |
| 119 | + SavedFiltersReordered(reorderedFilters: reorderedList), |
| 120 | + ); |
| 121 | + }, |
| 122 | + ); |
| 123 | + }, |
| 124 | + ), |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments