controller.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'package:get/get.dart';
  2. import 'package:o2oa_all_platform/common/extension/index.dart';
  3. import '../../../common/api/index.dart';
  4. import '../../../common/models/index.dart';
  5. import '../../../common/routers/index.dart';
  6. import '../../../common/utils/index.dart';
  7. import 'calendar_list/index.dart';
  8. import 'create_calendar_event/index.dart';
  9. import 'index.dart';
  10. class CalendarController extends GetxController {
  11. CalendarController();
  12. final state = CalendarState();
  13. final Map<String, List<EventInfo>> _eventListMap = {};
  14. /// 在 widget 内存中分配后立即调用。
  15. @override
  16. void onInit() {
  17. super.onInit();
  18. }
  19. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  20. @override
  21. void onReady() {
  22. state.title = Get.parameters['displayName'] ?? "calendar_title".tr;
  23. loadEventFromNet(state.focusedDay);
  24. super.onReady();
  25. }
  26. /// 在 [onDelete] 方法之前调用。
  27. @override
  28. void onClose() {
  29. super.onClose();
  30. }
  31. /// dispose 释放内存
  32. @override
  33. void dispose() {
  34. super.dispose();
  35. }
  36. Future<void> loadEventFromNet(DateTime changeDay) async {
  37. final lastMonthDate = changeDay.addMonths(-1);
  38. final nextMonthDate = changeDay.addMonths(1);
  39. _eventListMap.clear();
  40. Loading.show();
  41. // 上个月
  42. final lastList = await CalendarAssembleControlService.to.eventListWithFilter('${lastMonthDate.monthFirstDate().ymd()} 00:00:00', '${lastMonthDate.monthLastDate().ymd()} 23:59:59');
  43. if (lastList != null) {
  44. putEventInMap(lastList);
  45. }
  46. // 本月
  47. final eventList = await CalendarAssembleControlService.to.eventListWithFilter('${changeDay.monthFirstDate().ymd()} 00:00:00', '${changeDay.monthLastDate().ymd()} 23:59:59');
  48. if (eventList != null) {
  49. putEventInMap(eventList);
  50. }
  51. // 下个月
  52. final nextList = await CalendarAssembleControlService.to.eventListWithFilter('${nextMonthDate.monthFirstDate().ymd()} 00:00:00', '${nextMonthDate.monthLastDate().ymd()} 23:59:59');
  53. if (nextList != null) {
  54. putEventInMap(nextList);
  55. }
  56. Loading.dismiss();
  57. state.selectedDay = changeDay;
  58. state.focusedDay = changeDay;
  59. state.eventList.clear();
  60. state.eventList.addAll(loadEvent(changeDay));
  61. update();
  62. }
  63. /// 计算全天事件持续的日期
  64. List<DateTime> _tilAllDate(DateTime start, DateTime end) {
  65. List<DateTime> allDates = [];
  66. DateTime currentDate = start;
  67. while (currentDate.isBefore(end) || currentDate.isAtSameMomentAs(end)) {
  68. allDates.add(currentDate);
  69. currentDate = currentDate.add(const Duration(days: 1));
  70. }
  71. return allDates;
  72. }
  73. /// 查询的日程数据存入 map 中
  74. void putEventInMap(EventFilterList eventList) {
  75. final wholeEvent = eventList.wholeDayEvents ?? [];
  76. for (var event in wholeEvent) {
  77. final startDate = DateTime.tryParse(event.startTime ?? '');
  78. final endDate = DateTime.tryParse(event.endTime ?? '');
  79. if (startDate != null && endDate != null) {
  80. List<DateTime> dates = _tilAllDate(startDate, endDate);
  81. for (var element in dates) {
  82. final eDate = element.ymd();
  83. if (_eventListMap.containsKey(eDate)) {
  84. _eventListMap[eDate]?.add(event);
  85. } else {
  86. _eventListMap[eDate] = [event];
  87. }
  88. }
  89. }
  90. }
  91. final oneDayEvent = eventList.inOneDayEvents ?? [];
  92. for (var element in oneDayEvent) {
  93. if (element.eventDate == null || element.eventDate?.isEmpty == true) {
  94. continue;
  95. }
  96. if (_eventListMap.containsKey(element.eventDate!)) {
  97. _eventListMap[element.eventDate!]?.addAll(element.inOneDayEvents ?? []);
  98. } else {
  99. _eventListMap[element.eventDate!] = element.inOneDayEvents??[];
  100. }
  101. }
  102. }
  103. List<EventInfo> loadEvent(DateTime day) {
  104. String key = day.ymd();
  105. return _eventListMap[key] ?? [];
  106. }
  107. void onDaySelected(DateTime selectedDay, DateTime focusedDay) {
  108. OLogger.d('selected :$selectedDay, focused: $focusedDay');
  109. state.selectedDay = selectedDay;
  110. state.focusedDay = focusedDay;
  111. state.eventList.clear();
  112. state.eventList.addAll(loadEvent(selectedDay));
  113. }
  114. /// 日历页面
  115. void enterCalendarListPage() {
  116. CalendarListPage.open();
  117. }
  118. /// 更新日程
  119. void enterEventUpdate(EventInfo info) async {
  120. await CreateCalendarEventPage.openUpdate(info);
  121. loadEventFromNet(state.focusedDay);
  122. }
  123. /// 创建日程
  124. void enterCreateEvent() async {
  125. await Get.toNamed(O2OARoutes.appCalendarEventCreate);
  126. loadEventFromNet(state.focusedDay);
  127. }
  128. }