You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8KB

  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:farm_tpf/custom_model/Supply.dart';
  5. import 'package:farm_tpf/data/repository/repository.dart';
  6. part 'supply_event.dart';
  7. part 'supply_state.dart';
  8. class SupplyBloc extends Bloc<SupplyEvent, SupplyState> {
  9. final Repository repository;
  10. SupplyBloc({required this.repository}) : super(SupplyInitial());
  11. @override
  12. Stream<SupplyState> mapEventToState(
  13. SupplyEvent event,
  14. ) async* {
  15. if (event is DataFetched) {
  16. try {
  17. final response = await repository.getSupplies(event.urlSupply);
  18. List<Supply> supplies = response.map((supply) {
  19. if (supply.id == event.selectedId) {
  20. supply.isSelected = true;
  21. }
  22. return supply;
  23. }).toList();
  24. yield SupplySuccess(items: supplies);
  25. } catch (e) {
  26. yield SupplyFailure();
  27. }
  28. } else if (event is OnRefresh) {
  29. try {
  30. final response = await repository.getSupplies(event.urlSupply);
  31. List<Supply> supplies = response.map((supply) {
  32. if (supply.id == event.selectedId) {
  33. supply.isSelected = true;
  34. }
  35. return supply;
  36. }).toList();
  37. yield SupplySuccess(items: supplies);
  38. } catch (_) {
  39. yield SupplyFailure();
  40. }
  41. } else if (event is OnSearch) {
  42. try {
  43. final response = await repository.getSupplies(event.urlSupply,
  44. query: event.searchString ?? '');
  45. List<Supply> supplies = response.map((supply) {
  46. if (supply.id == event.selectedId) {
  47. supply.isSelected = true;
  48. }
  49. return supply;
  50. }).toList();
  51. yield SupplySuccess(items: supplies);
  52. } catch (_) {
  53. yield SupplyFailure();
  54. }
  55. }
  56. }
  57. }