2024-11-19 23:39:56 +04:00
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
import 'package:mobiles_labs_5th_semester/data/repositories/games_repository.dart';
|
|
|
|
|
import 'events.dart';
|
|
|
|
|
import 'state.dart';
|
|
|
|
|
|
|
|
|
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|
|
|
|
final GamesRepository repo;
|
|
|
|
|
|
2024-11-20 00:37:34 +04:00
|
|
|
|
//связывание метода _onLoadData с конкретным событием в конструкторе
|
2024-11-19 23:39:56 +04:00
|
|
|
|
HomeBloc(this.repo) : super (const HomeState()) {
|
|
|
|
|
on<HomeLoadDataEvent>(_onLoadData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Emitter - генератор событий
|
|
|
|
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async{
|
|
|
|
|
if (event.nextPage == null) {
|
|
|
|
|
emit(state.copyWith(isLoading: true));
|
|
|
|
|
} else {
|
|
|
|
|
emit(state.copyWith(isPaginationLoading: true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String? error;
|
|
|
|
|
|
|
|
|
|
final data = await repo.loadData(
|
|
|
|
|
q: event.search,
|
|
|
|
|
page: event.nextPage ?? 1,
|
|
|
|
|
onError: (e) => error = e
|
|
|
|
|
);
|
|
|
|
|
if (event.nextPage != null) {
|
|
|
|
|
data?.data?.insertAll(0, state.data?.data ?? []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
|
data: data,
|
|
|
|
|
isLoading: false,
|
|
|
|
|
isPaginationLoading: false,
|
|
|
|
|
error: error
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|