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.

73 lines
2.2KB

  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:farm_tpf/custom_model/LocationUnit.dart';
  5. import 'package:farm_tpf/data/repository/repository.dart';
  6. import 'package:farm_tpf/utils/const_common.dart';
  7. import 'package:meta/meta.dart';
  8. part 'location_event.dart';
  9. part 'location_state.dart';
  10. class LocationBloc extends Bloc<LocationEvent, LocationState> {
  11. final Repository repository;
  12. LocationBloc({required this.repository}) : super(LocationInitial());
  13. @override
  14. Stream<LocationState> mapEventToState(
  15. LocationEvent event,
  16. ) async* {
  17. if (event is DataFetched) {
  18. try {
  19. final response = await repository.getLocationUnits(
  20. locationType: event.locationType,
  21. filterId: event.filterId ?? -1,
  22. );
  23. List<LocationUnit> locations = response.map((location) {
  24. if (location.id == event.selectedId) {
  25. location.isSelected = true;
  26. }
  27. return location;
  28. }).toList();
  29. yield LocationSuccess(items: locations);
  30. } catch (_) {
  31. yield LocationFailure();
  32. }
  33. } else if (event is OnRefresh) {
  34. try {
  35. final response = await repository.getLocationUnits(
  36. locationType: event.locationType,
  37. filterId: event.filterId ?? -1,
  38. );
  39. List<LocationUnit> locations = response.map((location) {
  40. if (location.id == event.selectedId) {
  41. location.isSelected = true;
  42. }
  43. return location;
  44. }).toList();
  45. yield LocationSuccess(items: locations);
  46. } catch (_) {
  47. yield LocationFailure();
  48. }
  49. } else if (event is OnSearch) {
  50. try {
  51. final response = await repository.getLocationUnits(
  52. locationType: event.locationType,
  53. filterId: event.filterId ?? -1,
  54. query: event.searchString ?? '',
  55. );
  56. List<LocationUnit> locations = response.map((location) {
  57. if (location.id == event.selectedId) {
  58. location.isSelected = true;
  59. }
  60. return location;
  61. }).toList();
  62. yield LocationSuccess(items: locations);
  63. } catch (_) {
  64. yield LocationFailure();
  65. }
  66. }
  67. }
  68. }