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.

137 lines
5.5KB

  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 && !(state is NotiSuccess && ((state as NotiSuccess).hasReachedMax ?? false))) {
  22. try {
  23. if (state is NotiInitial) {
  24. yield NotiLoadding();
  25. final response = await repository.getNotifications(page: 0, size: pageSize);
  26. List<NotificationDTO> items = <NotificationDTO>[];
  27. response.notificationDTO!.forEach((e) => items.add(NotificationDTO.clone(e)));
  28. yield NotiSuccess(
  29. unread: response.numberUnreadTotal ?? 0,
  30. read: response.numberReadTotal ?? 0,
  31. items: items,
  32. page: 0,
  33. hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
  34. }
  35. if (state is NotiSuccess) {
  36. final currentState = state as NotiSuccess;
  37. int page = (currentState.page ?? 0) + 1;
  38. final response = await repository.getNotifications(page: page, size: pageSize);
  39. yield response.notificationDTO!.isEmpty
  40. ? currentState.copyWith(hasReachedMax: true)
  41. : NotiSuccess(
  42. unread: response.numberUnreadTotal ?? 0,
  43. read: response.numberReadTotal ?? 0,
  44. items: currentState.items ?? [] + response.notificationDTO!,
  45. page: (currentState.page ?? 0) + 1,
  46. hasReachedMax: false);
  47. }
  48. } catch (e) {
  49. yield NotiFailure(errorString: AppException.handleError(e));
  50. }
  51. }
  52. if (event is OnRefresh) {
  53. yield NotiLoadding();
  54. try {
  55. final response = await repository.getNotifications(page: 0, size: pageSize);
  56. List<NotificationDTO> items = <NotificationDTO>[];
  57. response.notificationDTO?.forEach((e) => items.add(NotificationDTO.clone(e)));
  58. yield NotiSuccess(
  59. unread: response.numberUnreadTotal ?? 0,
  60. read: response.numberReadTotal ?? 0,
  61. items: items,
  62. page: 0,
  63. hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
  64. } catch (e) {
  65. yield NotiFailure(errorString: AppException.handleError(e));
  66. }
  67. } else if (event is OnRefreshFromNotification) {
  68. try {
  69. final response = await repository.getNotifications(page: 0, size: pageSize);
  70. List<NotificationDTO> items = <NotificationDTO>[];
  71. response.notificationDTO?.forEach((e) => items.add(NotificationDTO.clone(e)));
  72. yield NotiSuccess(
  73. unread: response.numberUnreadTotal ?? 0,
  74. read: response.numberReadTotal ?? 0,
  75. items: items,
  76. page: 0,
  77. hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
  78. } catch (e) {
  79. yield NotiFailure(errorString: AppException.handleError(e));
  80. }
  81. }
  82. if (event is OnUpdate) {
  83. yield NotiLoadding();
  84. try {
  85. //Change status notification if mark read
  86. if (event.currentItemId != null) {
  87. var updateNoti = UpdateNoti()
  88. ..id = event.currentItemId
  89. ..isRead = 1;
  90. await repository.updateNoti(updateNoti);
  91. }
  92. yield NotiSuccess(
  93. unread: event.unread, read: event.read, items: event.currentItems, page: event.currentPage, hasReachedMax: event.hasReachedMax);
  94. } catch (e) {
  95. yield NotiFailure(errorString: exception_common);
  96. }
  97. } else if (event is MarkAllNotificationUpdate) {
  98. yield NotiLoadding();
  99. try {
  100. await repository.updateAllNotification(event.status);
  101. final response = await repository.getNotifications(page: 0, size: pageSize);
  102. List<NotificationDTO> items = <NotificationDTO>[];
  103. response.notificationDTO?.forEach((e) => items.add(NotificationDTO.clone(e)));
  104. yield NotiSuccess(
  105. unread: response.numberUnreadTotal ?? 0,
  106. read: response.numberReadTotal ?? 0,
  107. items: items,
  108. page: 0,
  109. hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
  110. } catch (e) {
  111. yield NotiFailure(errorString: AppException.handleError(e));
  112. }
  113. } else if (event is ReceiveDataFromSocket) {
  114. List<NotificationDTO> updatedItems = <NotificationDTO>[];
  115. event.updatedItemObject?.notificationDTO?.forEach((e) {
  116. updatedItems.add(NotificationDTO.clone(e));
  117. });
  118. event.currentItems.forEach((e) {
  119. updatedItems.add(NotificationDTO.clone(e));
  120. });
  121. yield NotiSuccess(
  122. unread: event.updatedItemObject?.numberUnreadTotal ?? 0,
  123. read: event.updatedItemObject?.numberReadTotal ?? 0,
  124. items: updatedItems,
  125. page: event.page ?? 1,
  126. hasReachedMax: updatedItems.length < pageSize ? true : false);
  127. }
  128. }
  129. }