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.

101 lines
4.0KB

  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: true);
  26. } else {
  27. CropPlot response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  28. yield PlotDetailSuccess(ownerItem: response, items: response.activities, page: 0, hasReachedMax: true);
  29. }
  30. //remove paging
  31. return;
  32. }
  33. if (state is PlotDetailSuccess) {
  34. //remove paging
  35. // final currentState = state as PlotDetailSuccess;
  36. // int page = (currentState.page ?? 0) + 1;
  37. // var response;
  38. // if (event.cropId != null) {
  39. // response = await repository.getPlotDetail(event.cropId ?? -1, page: page, size: pageSize);
  40. // yield response.activities.isEmpty
  41. // ? currentState.copyWith(hasReachedMax: true)
  42. // : PlotDetailSuccess(
  43. // items: (currentState.items ?? []) + response.activities,
  44. // page: (currentState.page ?? 0) + 1,
  45. // hasReachedMax: false,
  46. // );
  47. // } else {
  48. // CropPlot response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  49. // yield PlotDetailSuccess(ownerItem: response, items: response.activities, page: 0, hasReachedMax: true);
  50. // }
  51. }
  52. } catch (e) {
  53. var errorString = AppException.handleError(e);
  54. yield PlotDetailFailure(errorString: errorString);
  55. }
  56. }
  57. if (event is CheckInfo) {
  58. try {
  59. yield PlotDetailLoading();
  60. var response;
  61. if (event.cropId != null) {
  62. response = await repository.getPlotDetail(event.cropId ?? -1, page: 0, size: pageSize);
  63. } else {
  64. response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  65. }
  66. CropPlot cropPlot = response as CropPlot;
  67. yield PlotDetailSuccess(ownerItem: cropPlot, page: 0, hasReachedMax: (response.activities ?? []).length < pageSize ? true : false);
  68. } catch (e) {
  69. yield PlotDetailFailure(errorString: AppException.handleError(e));
  70. }
  71. }
  72. if (event is OnRefresh) {
  73. try {
  74. yield PlotDetailLoading();
  75. var response;
  76. if (event.cropId != null) {
  77. response = await repository.getPlotDetail(event.cropId ?? -1, page: 0, size: pageSize);
  78. } else {
  79. response = await repository.getPlotDetailByCode(event.cropCode ?? '');
  80. }
  81. yield PlotDetailSuccess(items: response.activities, page: 0, hasReachedMax: true);
  82. } catch (e) {
  83. yield PlotDetailFailure(errorString: AppException.handleError(e));
  84. }
  85. } else if (event is OnUpdate) {
  86. yield PlotDetailLoading();
  87. try {
  88. yield PlotDetailSuccess(items: event.currentItems, page: event.currentPage, hasReachedMax: event.hasReachedMax);
  89. } catch (e) {
  90. yield PlotDetailFailure(errorString: AppException.handleError(e));
  91. }
  92. }
  93. }
  94. }