|
1 | 1 | import 'dart:async';
|
2 | 2 |
|
3 | 3 | import 'package:bloc/bloc.dart';
|
4 |
| -import 'package:bloc_concurrency/bloc_concurrency.dart'; // For transformers |
5 | 4 | import 'package:equatable/equatable.dart';
|
6 |
| -import 'package:ht_data_repository/ht_data_repository.dart'; // Generic Data Repository |
| 5 | +import 'package:ht_data_repository/ht_data_repository.dart'; |
7 | 6 | import 'package:ht_shared/ht_shared.dart'
|
8 |
| - show |
9 |
| - HtHttpException, |
10 |
| - Source; // Shared models, including Source and standardized exceptions |
| 7 | + show Country, HtHttpException, Source, SourceType; |
11 | 8 |
|
12 | 9 | part 'sources_filter_event.dart';
|
13 | 10 | part 'sources_filter_state.dart';
|
14 | 11 |
|
15 |
| -/// {@template sources_filter_bloc} |
16 |
| -/// Manages the state for fetching and displaying sources for filtering. |
17 |
| -/// |
18 |
| -/// Handles initial fetching and pagination of sources using the |
19 |
| -/// provided [HtDataRepository]. |
20 |
| -/// {@endtemplate} |
21 | 12 | class SourcesFilterBloc extends Bloc<SourcesFilterEvent, SourcesFilterState> {
|
22 |
| - /// {@macro sources_filter_bloc} |
23 |
| - /// |
24 |
| - /// Requires a [HtDataRepository<Source>] to interact with the data layer. |
25 |
| - SourcesFilterBloc({required HtDataRepository<Source> sourcesRepository}) |
26 |
| - : _sourcesRepository = sourcesRepository, |
27 |
| - super(const SourcesFilterState()) { |
28 |
| - on<SourcesFilterRequested>( |
29 |
| - _onSourcesFilterRequested, |
30 |
| - transformer: restartable(), // Only process the latest request |
31 |
| - ); |
32 |
| - on<SourcesFilterLoadMoreRequested>( |
33 |
| - _onSourcesFilterLoadMoreRequested, |
34 |
| - transformer: droppable(), // Ignore new requests while one is processing |
35 |
| - ); |
| 13 | + SourcesFilterBloc({ |
| 14 | + required HtDataRepository<Source> sourcesRepository, |
| 15 | + required HtDataRepository<Country> countriesRepository, |
| 16 | + }) : _sourcesRepository = sourcesRepository, |
| 17 | + _countriesRepository = countriesRepository, |
| 18 | + super(const SourcesFilterState()) { |
| 19 | + on<LoadSourceFilterData>(_onLoadSourceFilterData); |
| 20 | + on<CountryCapsuleToggled>(_onCountryCapsuleToggled); |
| 21 | + on<AllSourceTypesCapsuleToggled>(_onAllSourceTypesCapsuleToggled); // Added |
| 22 | + on<SourceTypeCapsuleToggled>(_onSourceTypeCapsuleToggled); |
| 23 | + on<SourceCheckboxToggled>(_onSourceCheckboxToggled); |
| 24 | + on<ClearSourceFiltersRequested>(_onClearSourceFiltersRequested); |
| 25 | + on<_FetchFilteredSourcesRequested>(_onFetchFilteredSourcesRequested); |
36 | 26 | }
|
37 | 27 |
|
38 | 28 | final HtDataRepository<Source> _sourcesRepository;
|
| 29 | + final HtDataRepository<Country> _countriesRepository; |
39 | 30 |
|
40 |
| - /// Number of sources to fetch per page. |
41 |
| - static const _sourcesLimit = 20; |
42 |
| - |
43 |
| - /// Handles the initial request to fetch sources. |
44 |
| - Future<void> _onSourcesFilterRequested( |
45 |
| - SourcesFilterRequested event, |
| 31 | + Future<void> _onLoadSourceFilterData( |
| 32 | + LoadSourceFilterData event, |
46 | 33 | Emitter<SourcesFilterState> emit,
|
47 | 34 | ) async {
|
48 |
| - // Prevent fetching if already loading or successful |
49 |
| - if (state.status == SourcesFilterStatus.loading || |
50 |
| - state.status == SourcesFilterStatus.success) { |
51 |
| - return; |
52 |
| - } |
| 35 | + emit( |
| 36 | + state.copyWith(dataLoadingStatus: SourceFilterDataLoadingStatus.loading), |
| 37 | + ); |
| 38 | + try { |
| 39 | + final availableCountries = await _countriesRepository.readAll(); |
| 40 | + final initialSelectedSourceIds = |
| 41 | + event.initialSelectedSources.map((s) => s.id).toSet(); |
53 | 42 |
|
54 |
| - emit(state.copyWith(status: SourcesFilterStatus.loading)); |
| 43 | + // Initialize selected capsules based on initialSelectedSources |
| 44 | + final initialSelectedCountryIsoCodes = <String>{}; |
| 45 | + final initialSelectedSourceTypes = <SourceType>{}; |
| 46 | + |
| 47 | + if (event.initialSelectedSources.isNotEmpty) { |
| 48 | + for (final source in event.initialSelectedSources) { |
| 49 | + if (source.headquarters?.isoCode != null) { |
| 50 | + initialSelectedCountryIsoCodes.add(source.headquarters!.isoCode); |
| 51 | + } |
| 52 | + if (source.sourceType != null) { |
| 53 | + initialSelectedSourceTypes.add(source.sourceType!); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
55 | 57 |
|
56 |
| - try { |
57 |
| - final response = await _sourcesRepository.readAll(limit: _sourcesLimit); |
58 | 58 | emit(
|
59 | 59 | state.copyWith(
|
60 |
| - status: SourcesFilterStatus.success, |
61 |
| - sources: response.items, |
62 |
| - hasMore: response.hasMore, |
63 |
| - cursor: response.cursor, |
64 |
| - clearError: true, // Clear any previous error |
| 60 | + availableCountries: availableCountries.items, |
| 61 | + finallySelectedSourceIds: initialSelectedSourceIds, |
| 62 | + selectedCountryIsoCodes: initialSelectedCountryIsoCodes, |
| 63 | + selectedSourceTypes: initialSelectedSourceTypes, |
| 64 | + // Keep loading status until sources are fetched |
65 | 65 | ),
|
66 | 66 | );
|
67 |
| - } on HtHttpException catch (e) { |
68 |
| - emit(state.copyWith(status: SourcesFilterStatus.failure, error: e)); |
| 67 | + // Trigger initial fetch of displayable sources |
| 68 | + add(const _FetchFilteredSourcesRequested()); |
69 | 69 | } catch (e) {
|
70 |
| - // Catch unexpected errors |
71 |
| - emit(state.copyWith(status: SourcesFilterStatus.failure, error: e)); |
| 70 | + emit( |
| 71 | + state.copyWith( |
| 72 | + dataLoadingStatus: SourceFilterDataLoadingStatus.failure, |
| 73 | + errorMessage: 'Failed to load filter criteria.', |
| 74 | + ), |
| 75 | + ); |
72 | 76 | }
|
73 | 77 | }
|
74 | 78 |
|
75 |
| - /// Handles the request to load more sources for pagination. |
76 |
| - Future<void> _onSourcesFilterLoadMoreRequested( |
77 |
| - SourcesFilterLoadMoreRequested event, |
| 79 | + Future<void> _onCountryCapsuleToggled( |
| 80 | + CountryCapsuleToggled event, |
| 81 | + Emitter<SourcesFilterState> emit, |
| 82 | + ) async { |
| 83 | + final currentSelected = Set<String>.from(state.selectedCountryIsoCodes); |
| 84 | + if (event.countryIsoCode.isEmpty) { |
| 85 | + // "All Countries" toggled |
| 86 | + // If "All" is tapped and it's already effectively "All" (empty set), or if it's tapped to select "All" |
| 87 | + // we clear the set. If specific items are selected and "All" is tapped, it also clears. |
| 88 | + // Essentially, tapping "All" always results in an empty set, meaning no country filter. |
| 89 | + currentSelected.clear(); |
| 90 | + } else { |
| 91 | + // Specific country toggled |
| 92 | + if (currentSelected.contains(event.countryIsoCode)) { |
| 93 | + currentSelected.remove(event.countryIsoCode); |
| 94 | + } else { |
| 95 | + currentSelected.add(event.countryIsoCode); |
| 96 | + } |
| 97 | + } |
| 98 | + emit(state.copyWith(selectedCountryIsoCodes: currentSelected)); |
| 99 | + add(const _FetchFilteredSourcesRequested()); |
| 100 | + } |
| 101 | + |
| 102 | + Future<void> _onAllSourceTypesCapsuleToggled( |
| 103 | + AllSourceTypesCapsuleToggled event, |
| 104 | + Emitter<SourcesFilterState> emit, |
| 105 | + ) async { |
| 106 | + // Toggling "All" for source types means clearing any specific selections. |
| 107 | + // If already clear, it remains clear. |
| 108 | + emit(state.copyWith(selectedSourceTypes: {})); |
| 109 | + add(const _FetchFilteredSourcesRequested()); |
| 110 | + } |
| 111 | + |
| 112 | + Future<void> _onSourceTypeCapsuleToggled( |
| 113 | + SourceTypeCapsuleToggled event, |
78 | 114 | Emitter<SourcesFilterState> emit,
|
79 | 115 | ) async {
|
80 |
| - // Only proceed if currently successful and has more items |
81 |
| - if (state.status != SourcesFilterStatus.success || !state.hasMore) { |
82 |
| - return; |
| 116 | + final currentSelected = Set<SourceType>.from(state.selectedSourceTypes); |
| 117 | + if (currentSelected.contains(event.sourceType)) { |
| 118 | + currentSelected.remove(event.sourceType); |
| 119 | + } else { |
| 120 | + currentSelected.add(event.sourceType); |
83 | 121 | }
|
| 122 | + // If specific types are selected, "All" is no longer true. |
| 123 | + // The UI will derive "All" state from selectedSourceTypes.isEmpty |
| 124 | + emit(state.copyWith(selectedSourceTypes: currentSelected)); |
| 125 | + add(const _FetchFilteredSourcesRequested()); |
| 126 | + } |
84 | 127 |
|
85 |
| - emit(state.copyWith(status: SourcesFilterStatus.loadingMore)); |
| 128 | + void _onSourceCheckboxToggled( |
| 129 | + SourceCheckboxToggled event, |
| 130 | + Emitter<SourcesFilterState> emit, |
| 131 | + ) { |
| 132 | + final currentSelected = Set<String>.from(state.finallySelectedSourceIds); |
| 133 | + if (event.isSelected) { |
| 134 | + currentSelected.add(event.sourceId); |
| 135 | + } else { |
| 136 | + currentSelected.remove(event.sourceId); |
| 137 | + } |
| 138 | + emit(state.copyWith(finallySelectedSourceIds: currentSelected)); |
| 139 | + } |
86 | 140 |
|
| 141 | + Future<void> _onClearSourceFiltersRequested( |
| 142 | + ClearSourceFiltersRequested event, |
| 143 | + Emitter<SourcesFilterState> emit, |
| 144 | + ) async { |
| 145 | + emit( |
| 146 | + state.copyWith( |
| 147 | + selectedCountryIsoCodes: {}, |
| 148 | + selectedSourceTypes: {}, |
| 149 | + finallySelectedSourceIds: {}, |
| 150 | + // Keep availableCountries and availableSourceTypes |
| 151 | + ), |
| 152 | + ); |
| 153 | + add(const _FetchFilteredSourcesRequested()); |
| 154 | + } |
| 155 | + |
| 156 | + Future<void> _onFetchFilteredSourcesRequested( |
| 157 | + _FetchFilteredSourcesRequested event, |
| 158 | + Emitter<SourcesFilterState> emit, |
| 159 | + ) async { |
| 160 | + emit( |
| 161 | + state.copyWith( |
| 162 | + dataLoadingStatus: SourceFilterDataLoadingStatus.loading, |
| 163 | + displayableSources: [], // Clear previous sources |
| 164 | + clearErrorMessage: true, |
| 165 | + ), |
| 166 | + ); |
87 | 167 | try {
|
88 |
| - final response = await _sourcesRepository.readAll( |
89 |
| - limit: _sourcesLimit, |
90 |
| - startAfterId: state.cursor, // Use the cursor from the current state |
91 |
| - ); |
| 168 | + final queryParameters = <String, dynamic>{}; |
| 169 | + if (state.selectedCountryIsoCodes.isNotEmpty) { |
| 170 | + queryParameters['countries'] = state.selectedCountryIsoCodes.join(','); |
| 171 | + } |
| 172 | + if (state.selectedSourceTypes.isNotEmpty) { |
| 173 | + queryParameters['sourceTypes'] = state.selectedSourceTypes |
| 174 | + .map((st) => st.name) |
| 175 | + .join(','); |
| 176 | + } |
| 177 | + |
| 178 | + final response = await _sourcesRepository.readAllByQuery(queryParameters); |
92 | 179 | emit(
|
93 | 180 | state.copyWith(
|
94 |
| - status: SourcesFilterStatus.success, |
95 |
| - // Append new sources to the existing list |
96 |
| - sources: List.of(state.sources)..addAll(response.items), |
97 |
| - hasMore: response.hasMore, |
98 |
| - cursor: response.cursor, |
| 181 | + displayableSources: response.items, |
| 182 | + dataLoadingStatus: SourceFilterDataLoadingStatus.success, |
99 | 183 | ),
|
100 | 184 | );
|
101 | 185 | } on HtHttpException catch (e) {
|
102 |
| - // Keep existing data but indicate failure |
103 |
| - emit(state.copyWith(status: SourcesFilterStatus.failure, error: e)); |
| 186 | + emit( |
| 187 | + state.copyWith( |
| 188 | + dataLoadingStatus: SourceFilterDataLoadingStatus.failure, |
| 189 | + errorMessage: e.message, |
| 190 | + ), |
| 191 | + ); |
104 | 192 | } catch (e) {
|
105 |
| - // Catch unexpected errors |
106 |
| - emit(state.copyWith(status: SourcesFilterStatus.failure, error: e)); |
| 193 | + emit( |
| 194 | + state.copyWith( |
| 195 | + dataLoadingStatus: SourceFilterDataLoadingStatus.failure, |
| 196 | + errorMessage: 'An unexpected error occurred while fetching sources.', |
| 197 | + ), |
| 198 | + ); |
107 | 199 | }
|
108 | 200 | }
|
109 | 201 | }
|
0 commit comments