|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:bloc/bloc.dart'; |
| 4 | +import 'package:bloc_concurrency/bloc_concurrency.dart'; // For transformers |
| 5 | +import 'package:equatable/equatable.dart'; |
| 6 | +import 'package:ht_countries_client/ht_countries_client.dart'; // For Country model and exceptions |
| 7 | +import 'package:ht_countries_repository/ht_countries_repository.dart'; |
| 8 | +import 'package:ht_shared/ht_shared.dart'; // For PaginatedResponse |
| 9 | + |
| 10 | +part 'countries_filter_event.dart'; |
| 11 | +part 'countries_filter_state.dart'; |
| 12 | + |
| 13 | +/// {@template countries_filter_bloc} |
| 14 | +/// Manages the state for fetching and displaying countries for filtering. |
| 15 | +/// |
| 16 | +/// Handles initial fetching and pagination of countries using the |
| 17 | +/// provided [HtCountriesRepository]. |
| 18 | +/// {@endtemplate} |
| 19 | +class CountriesFilterBloc extends Bloc<CountriesFilterEvent, CountriesFilterState> { |
| 20 | + /// {@macro countries_filter_bloc} |
| 21 | + /// |
| 22 | + /// Requires a [HtCountriesRepository] to interact with the data layer. |
| 23 | + CountriesFilterBloc({required HtCountriesRepository countriesRepository}) |
| 24 | + : _countriesRepository = countriesRepository, |
| 25 | + super(const CountriesFilterState()) { |
| 26 | + on<CountriesFilterRequested>( |
| 27 | + _onCountriesFilterRequested, |
| 28 | + transformer: restartable(), // Only process the latest request |
| 29 | + ); |
| 30 | + on<CountriesFilterLoadMoreRequested>( |
| 31 | + _onCountriesFilterLoadMoreRequested, |
| 32 | + transformer: droppable(), // Ignore new requests while one is processing |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + final HtCountriesRepository _countriesRepository; |
| 37 | + |
| 38 | + /// Number of countries to fetch per page. |
| 39 | + static const _countriesLimit = 20; |
| 40 | + |
| 41 | + /// Handles the initial request to fetch countries. |
| 42 | + Future<void> _onCountriesFilterRequested( |
| 43 | + CountriesFilterRequested event, |
| 44 | + Emitter<CountriesFilterState> emit, |
| 45 | + ) async { |
| 46 | + // Prevent fetching if already loading or successful |
| 47 | + if (state.status == CountriesFilterStatus.loading || |
| 48 | + state.status == CountriesFilterStatus.success) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + emit(state.copyWith(status: CountriesFilterStatus.loading)); |
| 53 | + |
| 54 | + try { |
| 55 | + // Note: Repository uses 'cursor' parameter name here |
| 56 | + final response = await _countriesRepository.fetchCountries( |
| 57 | + limit: _countriesLimit, |
| 58 | + cursor: null, // Explicitly null for initial fetch |
| 59 | + ); |
| 60 | + emit( |
| 61 | + state.copyWith( |
| 62 | + status: CountriesFilterStatus.success, |
| 63 | + countries: response.items, |
| 64 | + hasMore: response.hasMore, |
| 65 | + cursor: response.cursor, |
| 66 | + clearError: true, // Clear any previous error |
| 67 | + ), |
| 68 | + ); |
| 69 | + } on CountryFetchFailure catch (e) { |
| 70 | + emit( |
| 71 | + state.copyWith( |
| 72 | + status: CountriesFilterStatus.failure, |
| 73 | + error: e, |
| 74 | + ), |
| 75 | + ); |
| 76 | + } catch (e) { |
| 77 | + // Catch unexpected errors |
| 78 | + emit( |
| 79 | + state.copyWith( |
| 80 | + status: CountriesFilterStatus.failure, |
| 81 | + error: e, |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// Handles the request to load more countries for pagination. |
| 88 | + Future<void> _onCountriesFilterLoadMoreRequested( |
| 89 | + CountriesFilterLoadMoreRequested event, |
| 90 | + Emitter<CountriesFilterState> emit, |
| 91 | + ) async { |
| 92 | + // Only proceed if currently successful and has more items |
| 93 | + if (state.status != CountriesFilterStatus.success || !state.hasMore) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + emit(state.copyWith(status: CountriesFilterStatus.loadingMore)); |
| 98 | + |
| 99 | + try { |
| 100 | + // Note: Repository uses 'cursor' parameter name here |
| 101 | + final response = await _countriesRepository.fetchCountries( |
| 102 | + limit: _countriesLimit, |
| 103 | + cursor: state.cursor, // Use the cursor from the current state |
| 104 | + ); |
| 105 | + emit( |
| 106 | + state.copyWith( |
| 107 | + status: CountriesFilterStatus.success, |
| 108 | + // Append new countries to the existing list |
| 109 | + countries: List.of(state.countries)..addAll(response.items), |
| 110 | + hasMore: response.hasMore, |
| 111 | + cursor: response.cursor, |
| 112 | + ), |
| 113 | + ); |
| 114 | + } on CountryFetchFailure catch (e) { |
| 115 | + // Keep existing data but indicate failure |
| 116 | + emit( |
| 117 | + state.copyWith( |
| 118 | + status: CountriesFilterStatus.failure, |
| 119 | + error: e, |
| 120 | + ), |
| 121 | + ); |
| 122 | + } catch (e) { |
| 123 | + // Catch unexpected errors |
| 124 | + emit( |
| 125 | + state.copyWith( |
| 126 | + status: CountriesFilterStatus.failure, |
| 127 | + error: e, |
| 128 | + ), |
| 129 | + ); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments