27 lines
946 B
Dart
27 lines
946 B
Dart
|
import 'package:flutter_app/data/repositories/manga_repository.dart';
|
||
|
import 'package:flutter_app/presentation/home_page/bloc/events.dart';
|
||
|
import 'package:flutter_app/presentation/home_page/bloc/state.dart';
|
||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
|
|
||
|
class HomeBloc extends Bloc<HomeEvent, HomeState>{
|
||
|
final MangaRepository repo;
|
||
|
|
||
|
HomeBloc(this.repo) : super (const HomeState()) {
|
||
|
on<HomeLoadDataEvent>(_onLoadData);
|
||
|
}
|
||
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||
|
if( event.nextPage == null) {
|
||
|
emit(state.copyWith(isLoading: true));
|
||
|
} else {
|
||
|
emit(state.copyWith(isPaginationLoading: true));
|
||
|
}
|
||
|
|
||
|
final data = await repo.loadData(q: event.search, page: event.nextPage ?? 1);
|
||
|
|
||
|
if (event.nextPage != null) {
|
||
|
data?.data?.insertAll(0, state.data?.data ?? []);
|
||
|
}
|
||
|
|
||
|
emit(state.copyWith(data: data, isLoading: false, isPaginationLoading: false));
|
||
|
}
|
||
|
}
|