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.

155 lines
5.6KB

  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:farm_tpf/custom_model/NotificationDTO.dart';
  5. import 'package:farm_tpf/custom_model/NotificationObjectDTO.dart';
  6. import 'package:farm_tpf/custom_model/UpdateNoti.dart';
  7. import 'package:farm_tpf/data/api/app_exception.dart';
  8. import 'package:farm_tpf/data/repository/repository.dart';
  9. import 'package:farm_tpf/utils/const_string.dart';
  10. import 'package:meta/meta.dart';
  11. part 'noti_event.dart';
  12. part 'noti_state.dart';
  13. class NotiBloc extends Bloc<NotiEvent, NotiState> {
  14. final Repository repository;
  15. NotiBloc({@required this.repository}) : super(NotiInitial());
  16. static int pageSize = 20;
  17. @override
  18. Stream<NotiState> mapEventToState(
  19. NotiEvent event,
  20. ) async* {
  21. if (event is DataFetched &&
  22. !(state is NotiSuccess && (state as NotiSuccess).hasReachedMax)) {
  23. try {
  24. if (state is NotiInitial) {
  25. yield NotiLoadding();
  26. final response =
  27. await repository.getNotifications(page: 0, size: pageSize);
  28. List<NotificationDTO> items = new List<NotificationDTO>();
  29. response.notificationDTO
  30. .forEach((e) => items.add(NotificationDTO.clone(e)));
  31. yield NotiSuccess(
  32. unread: response.numberUnreadTotal,
  33. read: response.numberReadTotal,
  34. items: items,
  35. page: 0,
  36. hasReachedMax:
  37. response.notificationDTO.length < pageSize ? true : false);
  38. }
  39. if (state is NotiSuccess) {
  40. final currentState = state as NotiSuccess;
  41. int page = currentState.page + 1;
  42. final response =
  43. await repository.getNotifications(page: page, size: pageSize);
  44. yield response.notificationDTO.isEmpty
  45. ? currentState.copyWith(hasReachedMax: true)
  46. : NotiSuccess(
  47. unread: response.numberUnreadTotal,
  48. read: response.numberReadTotal,
  49. items: currentState.items + response.notificationDTO,
  50. page: currentState.page + 1,
  51. hasReachedMax: false);
  52. }
  53. } catch (e) {
  54. yield NotiFailure(errorString: AppException.handleError(e));
  55. }
  56. }
  57. if (event is OnRefresh) {
  58. yield NotiLoadding();
  59. try {
  60. final response =
  61. await repository.getNotifications(page: 0, size: pageSize);
  62. List<NotificationDTO> items = new List<NotificationDTO>();
  63. response.notificationDTO
  64. .forEach((e) => items.add(NotificationDTO.clone(e)));
  65. yield NotiSuccess(
  66. unread: response.numberUnreadTotal,
  67. read: response.numberReadTotal,
  68. items: items,
  69. page: 0,
  70. hasReachedMax:
  71. response.notificationDTO.length < pageSize ? true : false);
  72. } catch (e) {
  73. yield NotiFailure(errorString: AppException.handleError(e));
  74. }
  75. } else if (event is OnRefreshFromNotification) {
  76. try {
  77. final response =
  78. await repository.getNotifications(page: 0, size: pageSize);
  79. List<NotificationDTO> items = new List<NotificationDTO>();
  80. response.notificationDTO
  81. .forEach((e) => items.add(NotificationDTO.clone(e)));
  82. yield NotiSuccess(
  83. unread: response.numberUnreadTotal,
  84. read: response.numberReadTotal,
  85. items: items,
  86. page: 0,
  87. hasReachedMax:
  88. response.notificationDTO.length < pageSize ? true : false);
  89. } catch (e) {
  90. yield NotiFailure(errorString: AppException.handleError(e));
  91. }
  92. }
  93. if (event is OnUpdate) {
  94. yield NotiLoadding();
  95. try {
  96. //Change status notification if mark read
  97. if (event.currentItemId != null) {
  98. var updateNoti = UpdateNoti()
  99. ..id = event.currentItemId
  100. ..isRead = 1;
  101. await repository.updateNoti(updateNoti);
  102. }
  103. yield NotiSuccess(
  104. unread: event.unread,
  105. read: event.read,
  106. items: event.currentItems,
  107. page: event.currentPage,
  108. hasReachedMax: event.hasReachedMax);
  109. } catch (e) {
  110. yield NotiFailure(errorString: exception_common);
  111. }
  112. } else if (event is MarkAllNotificationUpdate) {
  113. yield NotiLoadding();
  114. try {
  115. await repository.updateAllNotification(event.status);
  116. final response =
  117. await repository.getNotifications(page: 0, size: pageSize);
  118. List<NotificationDTO> items = new List<NotificationDTO>();
  119. response.notificationDTO
  120. .forEach((e) => items.add(NotificationDTO.clone(e)));
  121. yield NotiSuccess(
  122. unread: response.numberUnreadTotal,
  123. read: response.numberReadTotal,
  124. items: items,
  125. page: 0,
  126. hasReachedMax:
  127. response.notificationDTO.length < pageSize ? true : false);
  128. } catch (e) {
  129. yield NotiFailure(errorString: exception_common);
  130. }
  131. } else if (event is ReceiveDataFromSocket) {
  132. List<NotificationDTO> updatedItems = new List<NotificationDTO>();
  133. event.updatedItemObject.notificationDTO.forEach((e) {
  134. updatedItems.add(NotificationDTO.clone(e));
  135. });
  136. event.currentItems.forEach((e) {
  137. updatedItems.add(NotificationDTO.clone(e));
  138. });
  139. yield NotiSuccess(
  140. unread: event.updatedItemObject.numberUnreadTotal,
  141. read: event.updatedItemObject.numberReadTotal,
  142. items: updatedItems,
  143. page: event.page,
  144. hasReachedMax: updatedItems.length < pageSize ? true : false);
  145. }
  146. }
  147. }