|
| 1 | +import 'package:bloc/bloc.dart'; |
| 2 | +import 'package:collection/collection.dart'; |
| 3 | +import 'package:core/core.dart'; |
| 4 | +import 'package:data_repository/data_repository.dart'; |
| 5 | +import 'package:equatable/equatable.dart'; |
| 6 | +import 'package:logging/logging.dart'; |
| 7 | + |
| 8 | +part 'discover_event.dart'; |
| 9 | +part 'discover_state.dart'; |
| 10 | + |
| 11 | +/// {@template discover_bloc} |
| 12 | +/// A BLoC that manages the state of the discover feature. |
| 13 | +/// |
| 14 | +/// This BLoC is responsible for fetching all available news sources and |
| 15 | +/// grouping them by their respective [SourceType] for display on the |
| 16 | +/// discover page. |
| 17 | +/// {@endtemplate} |
| 18 | +class DiscoverBloc extends Bloc<DiscoverEvent, DiscoverState> { |
| 19 | + /// {@macro discover_bloc} |
| 20 | + DiscoverBloc({ |
| 21 | + required DataRepository<Source> sourcesRepository, |
| 22 | + required Logger logger, |
| 23 | + }) : _sourcesRepository = sourcesRepository, |
| 24 | + _logger = logger, |
| 25 | + super(const DiscoverState()) { |
| 26 | + on<DiscoverStarted>(_onDiscoverStarted); |
| 27 | + } |
| 28 | + |
| 29 | + final DataRepository<Source> _sourcesRepository; |
| 30 | + final Logger _logger; |
| 31 | + |
| 32 | + /// Handles the initial fetching and grouping of all sources. |
| 33 | + /// |
| 34 | + /// When [DiscoverStarted] is added, this method fetches all sources from |
| 35 | + /// the repository, groups them into a map by [SourceType], and emits |
| 36 | + /// a success or failure state. |
| 37 | + Future<void> _onDiscoverStarted( |
| 38 | + DiscoverStarted event, |
| 39 | + Emitter<DiscoverState> emit, |
| 40 | + ) async { |
| 41 | + _logger.fine('[DiscoverBloc] DiscoverStarted event received.'); |
| 42 | + emit(state.copyWith(status: DiscoverStatus.loading)); |
| 43 | + |
| 44 | + try { |
| 45 | + // Fetch all available sources from the repository. |
| 46 | + final sourcesResponse = await _sourcesRepository.readAll(); |
| 47 | + _logger.info( |
| 48 | + '[DiscoverBloc] Successfully fetched ${sourcesResponse.items.length} sources.', |
| 49 | + ); |
| 50 | + |
| 51 | + // Group the fetched sources by their sourceType. |
| 52 | + final groupedSources = groupBy<Source, SourceType>( |
| 53 | + sourcesResponse.items, |
| 54 | + (source) => source.sourceType, |
| 55 | + ); |
| 56 | + |
| 57 | + emit( |
| 58 | + state.copyWith( |
| 59 | + status: DiscoverStatus.success, |
| 60 | + groupedSources: groupedSources, |
| 61 | + ), |
| 62 | + ); |
| 63 | + } on HttpException catch (e, s) { |
| 64 | + _logger.severe('[DiscoverBloc] Failed to fetch sources.', e, s); |
| 65 | + emit(state.copyWith(status: DiscoverStatus.failure, error: e)); |
| 66 | + } catch (e, s) { |
| 67 | + _logger.severe('[DiscoverBloc] Failed to fetch sources.', e, s); |
| 68 | + emit( |
| 69 | + state.copyWith( |
| 70 | + status: DiscoverStatus.failure, |
| 71 | + error: UnknownException('An unexpected error occurred: $e'), |
| 72 | + ), |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments