PIbd-31_MasenkinMS_PMU/lib/view/home_page/bloc/bloc.dart
2024-09-30 22:15:55 +04:00

39 lines
1.0 KiB
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pmu_labworks/data/repositories/potterdb_repository.dart';
import 'package:pmu_labworks/view/home_page/bloc/events.dart';
import 'package:pmu_labworks/view/home_page/bloc/state.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final PotterDBRepository 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));
}
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(
isLoading: false,
isPaginationLoading: false,
data: data,
error: error,
));
}
}