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.

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