import 'package:anitrack/src/data/anime.dart'; import 'package:anitrack/src/data/manga.dart'; import 'package:anitrack/src/data/type.dart'; import 'package:anitrack/src/service/database.dart'; import 'package:anitrack/src/ui/bloc/anime_list_bloc.dart'; import 'package:anitrack/src/ui/bloc/navigation_bloc.dart'; import 'package:anitrack/src/ui/constants.dart'; import 'package:bloc/bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:get_it/get_it.dart'; part 'details_state.dart'; part 'details_event.dart'; part 'details_bloc.freezed.dart'; class DetailsBloc extends Bloc { DetailsBloc() : super(DetailsState()) { on(_onAnimeRequested); on(_onMangaRequested); on(_onDetailsUpdated); } Future _onAnimeRequested(AnimeDetailsRequestedEvent event, Emitter emit) async { emit( state.copyWith( trackingType: TrackingMediumType.anime, data: event.anime, ), ); GetIt.I.get().add( PushedNamedEvent( const NavigationDestination(detailsRoute), ), ); } Future _onMangaRequested(MangaDetailsRequestedEvent event, Emitter emit) async { emit( state.copyWith( trackingType: TrackingMediumType.manga, data: event.manga, ), ); GetIt.I.get().add( PushedNamedEvent( const NavigationDestination(detailsRoute), ), ); } Future _onDetailsUpdated(DetailsUpdatedEvent event, Emitter emit) async { if (state.trackingType == TrackingMediumType.anime) { emit( state.copyWith( data: event.data, ), ); await GetIt.I.get().updateAnime(event.data as AnimeTrackingData); GetIt.I.get().add( AnimeUpdatedEvent(event.data as AnimeTrackingData), ); } else { emit( state.copyWith( data: event.data, ), ); await GetIt.I.get().updateManga(event.data as MangaTrackingData); GetIt.I.get().add( MangaUpdatedEvent(event.data as MangaTrackingData), ); } } }