import 'package:anitrack/src/data/anime.dart'; import 'package:anitrack/src/data/search_result.dart'; import 'package:anitrack/src/ui/constants.dart'; import 'package:anitrack/src/ui/bloc/anime_list_bloc.dart' as list; import 'package:anitrack/src/ui/bloc/navigation_bloc.dart'; import 'package:bloc/bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:get_it/get_it.dart'; import 'package:jikan_api/jikan_api.dart'; part 'anime_search_state.dart'; part 'anime_search_event.dart'; part 'anime_search_bloc.freezed.dart'; class AnimeSearchBloc extends Bloc { AnimeSearchBloc() : super(AnimeSearchState()) { on(_onRequested); on(_onQueryChanged); on(_onQuerySubmitted); on(_onAnimeAdded); } Future _onRequested(AnimeSearchRequestedEvent event, Emitter emit) async { emit( state.copyWith( searchQuery: '', working: false, searchResults: [], ), ); GetIt.I.get().add( PushedNamedEvent( NavigationDestination(animeSearchRoute), ), ); } Future _onQueryChanged(SearchQueryChangedEvent event, Emitter emit) async { emit( state.copyWith( searchQuery: event.query, ), ); } Future _onQuerySubmitted(SearchQuerySubmittedEvent event, Emitter emit) async { if (state.searchQuery.isEmpty) return; emit( state.copyWith( working: true, ), ); final result = await Jikan().searchAnime( query: state.searchQuery, ); emit( state.copyWith( working: false, ), ); emit( state.copyWith( searchResults: result.map((Anime anime) => AnimeSearchResult( anime.title, anime.malId.toString(), anime.episodes, anime.imageUrl, anime.synopsis ?? '', ),).toList(), ), ); print(result); } Future _onAnimeAdded(AnimeAddedEvent event, Emitter emit) async { GetIt.I.get().add( list.AnimeAddedEvent( AnimeTrackingData( event.result.id, AnimeTrackingState.watching, event.result.title, 0, event.result.episodesTotal, event.result.thumbnailUrl, ), ), ); GetIt.I.get().add( PoppedRouteEvent(), ); } }