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.

94 lines
3.8KB

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