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.

98 lines
3.9KB

  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:farm_tpf/custom_model/CropPlot.dart';
  5. import 'package:farm_tpf/data/api/app_exception.dart';
  6. import 'package:farm_tpf/data/repository/repository.dart';
  7. import 'package:meta/meta.dart';
  8. part 'plot_detail_event.dart';
  9. part 'plot_detail_state.dart';
  10. class PlotDetailBloc extends Bloc<PlotDetailEvent, PlotDetailState> {
  11. final Repository repository;
  12. PlotDetailBloc({required this.repository}) : super(PlotDetailInitial());
  13. static int pageSize = 20;
  14. @override
  15. Stream<PlotDetailState> mapEventToState(
  16. PlotDetailEvent event,
  17. ) async* {
  18. if (event is DataFetched && !(state is PlotDetailSuccess && ((state as PlotDetailSuccess).hasReachedMax ?? false))) {
  19. try {
  20. if (state is PlotDetailInitial) {
  21. yield PlotDetailLoading();
  22. var response;
  23. if (event.cropId != null) {
  24. response = await repository.getPlotDetail(event.cropId ?? -1, page: 0, size: pageSize);
  25. yield PlotDetailSuccess(items: response.activities, page: 0, hasReachedMax: response.activities.length < pageSize ? true : false);
  26. } else {
  27. CropPlot response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  28. yield PlotDetailSuccess(ownerItem: response, items: response.activities, page: 0, hasReachedMax: true);
  29. }
  30. }
  31. if (state is PlotDetailSuccess) {
  32. final currentState = state as PlotDetailSuccess;
  33. int page = (currentState.page ?? 0) + 1;
  34. var response;
  35. if (event.cropId != null) {
  36. response = await repository.getPlotDetail(event.cropId ?? -1, page: page, size: pageSize);
  37. yield response.activities.isEmpty
  38. ? currentState.copyWith(hasReachedMax: true)
  39. : PlotDetailSuccess(
  40. items: (currentState.items ?? []) + response.activities,
  41. page: (currentState.page ?? 0) + 1,
  42. hasReachedMax: false,
  43. );
  44. } else {
  45. CropPlot response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  46. yield PlotDetailSuccess(ownerItem: response, items: response.activities, page: 0, hasReachedMax: true);
  47. }
  48. }
  49. } catch (e) {
  50. var errorString = AppException.handleError(e);
  51. yield PlotDetailFailure(errorString: errorString);
  52. }
  53. }
  54. if (event is CheckInfo) {
  55. try {
  56. yield PlotDetailLoading();
  57. var response;
  58. if (event.cropId != null) {
  59. response = await repository.getPlotDetail(event.cropId ?? -1, page: 0, size: pageSize);
  60. } else {
  61. response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  62. }
  63. CropPlot cropPlot = response as CropPlot;
  64. yield PlotDetailSuccess(ownerItem: cropPlot, page: 0, hasReachedMax: (response.activities ?? []).length < pageSize ? true : false);
  65. } catch (e) {
  66. yield PlotDetailFailure(errorString: AppException.handleError(e));
  67. }
  68. }
  69. if (event is OnRefresh) {
  70. try {
  71. yield PlotDetailLoading();
  72. var response;
  73. if (event.cropId != null) {
  74. response = await repository.getPlotDetail(event.cropId ?? -1, page: 0, size: pageSize);
  75. } else {
  76. response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  77. }
  78. yield PlotDetailSuccess(items: response.activities, page: 0, hasReachedMax: response.activities.length < pageSize ? true : false);
  79. } catch (e) {
  80. yield PlotDetailFailure(errorString: AppException.handleError(e));
  81. }
  82. } else if (event is OnUpdate) {
  83. yield PlotDetailLoading();
  84. try {
  85. yield PlotDetailSuccess(items: event.currentItems, page: event.currentPage, hasReachedMax: event.hasReachedMax);
  86. } catch (e) {
  87. yield PlotDetailFailure(errorString: AppException.handleError(e));
  88. }
  89. }
  90. }
  91. }