37 lines
1.0 KiB
Dart
Raw Normal View History

2024-12-11 12:40:09 +04:00
import 'package:card_app/presentation/home_page/bloc/events.dart';
import 'package:card_app/presentation/home_page/bloc/state.dart';
import 'package:card_app/repositories/WordsRepository.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2024-12-19 08:53:16 +04:00
class HomeBloc extends Bloc<HomeEvent, HomeState> {
2024-12-11 12:40:09 +04:00
final WordsRepository repo;
2024-12-19 08:53:16 +04:00
HomeBloc(this.repo) : super(const HomeState()) {
2024-12-11 12:40:09 +04:00
on<HomeLoadDataEvent>(_onLoadData);
}
2024-12-19 08:53:16 +04:00
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
if (event.nextPage == null) {
2024-12-11 12:40:09 +04:00
emit(state.copyWith(isLoading: true));
2024-12-19 08:53:16 +04:00
} else {
2024-12-11 12:40:09 +04:00
emit(state.copyWith(isPaginationLoading: true));
}
String? error;
2024-12-19 08:53:16 +04:00
final data =
await repo.loadData(q: event.search, page: event.nextPage ?? 1, onError: (e) => error = e);
2024-12-11 12:40:09 +04:00
2024-12-19 08:53:16 +04:00
if (event.nextPage != null) {
2024-12-11 12:40:09 +04:00
data?.data?.insertAll(0, state.data?.data ?? []);
}
emit(state.copyWith(
isLoading: false,
2024-12-19 08:53:16 +04:00
isPaginationLoading: false,
data: data,
2024-12-11 12:40:09 +04:00
error: error,
));
}
2024-12-19 08:53:16 +04:00
}