LCOV - code coverage report
Current view: top level - src/search/presentation/bloc - search_bloc.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 19 19 100.0 %
Date: 2026-03-02 18:37:46 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:tech_proof/data/repositories/movies_repository_imp.dart';
       2             : import 'package:tech_proof/domain/entities/movie_entity.dart';
       3             : import 'package:bloc_concurrency/bloc_concurrency.dart';
       4             : import 'package:bloc_event_transformers/bloc_event_transformers.dart';
       5             : import 'package:flutter_bloc/flutter_bloc.dart';
       6             : import 'package:equatable/equatable.dart';
       7             : 
       8             : part 'search_event.dart';
       9             : part 'search_state.dart';
      10             : 
      11             : class SearchBloc extends Bloc<SearchEvent, SearchState> {
      12             :   final MovieRepositoryImpl movieRepository;
      13             : 
      14           6 :   SearchBloc({required this.movieRepository}) : super(SearchInitial()) {
      15           6 :     on<SearchSubmitted>(_onSearchSubmitted, transformer: restartable());
      16           2 :     on<SearchTextChanged>(
      17           2 :       _onSearchTextChanged,
      18           2 :       transformer: debounce(const Duration(milliseconds: 300)),
      19             :     );
      20             :   }
      21             : 
      22           2 :   Future<void> _onSearchSubmitted(
      23             :     SearchSubmitted event,
      24             :     Emitter<SearchState> emit,
      25             :   ) async {
      26           4 :     await _performSearch(event.query, emit);
      27             :   }
      28             : 
      29           1 :   Future<void> _onSearchTextChanged(
      30             :     SearchTextChanged event,
      31             :     Emitter<SearchState> emit,
      32             :   ) async {
      33           2 :     await _performSearch(event.query, emit);
      34             :   }
      35             : 
      36           2 :   Future<void> _performSearch(String query, Emitter<SearchState> emit) async {
      37           4 :     if (state is! SearchLoaded) {
      38           4 :       emit(SearchLoading());
      39             :     }
      40             : 
      41           4 :     final movies = await movieRepository.searchMovies(query: query);
      42           2 :     movies.fold(
      43           3 :       (l) => emit(SearchError('Something went wrong with your query')),
      44           2 :       (r) {
      45           4 :         if (r.results.isEmpty) {
      46           4 :           emit(SearchInitial());
      47             :         } else {
      48           3 :           emit(SearchLoaded(r.results));
      49             :         }
      50             :       },
      51             :     );
      52             :   }
      53             : }

Generated by: LCOV version 1.14