notification_view_model.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'package:cpt_main/modules/notification/notification_group_data.dart';
  2. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  3. import 'package:riverpod_annotation/riverpod_annotation.dart';
  4. import 'package:widgets/load_state_layout.dart';
  5. import 'package:widgets/widget_export.dart';
  6. import 'notification_state.dart';
  7. part 'notification_view_model.g.dart';
  8. @riverpod
  9. class NotificationViewModel extends _$NotificationViewModel {
  10. @override
  11. NotificationState build() {
  12. return NotificationState(datas: []);
  13. }
  14. var _curPage = 1; //请求参数当前的页面
  15. var _needShowPlaceholder = true; //是否展示LoadingView
  16. // Refresh 控制器
  17. final EasyRefreshController refreshController = EasyRefreshController(
  18. controlFinishRefresh: true, //允许刷新
  19. controlFinishLoad: true, //允许加载
  20. );
  21. //刷新页面状态
  22. void changeLoadingState(LoadState loadState, String? errorMsg) {
  23. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  24. }
  25. // Refresh 刷新事件
  26. Future onRefresh() async {
  27. _curPage = 1;
  28. fetchList();
  29. }
  30. // Refresh 加载事件
  31. Future loadMore() async {
  32. _curPage++;
  33. fetchList();
  34. }
  35. // 重试请求
  36. Future retryRequest() async {
  37. _curPage = 1;
  38. _needShowPlaceholder = true;
  39. fetchList();
  40. }
  41. /// 获取服务器数据
  42. Future fetchList() async {
  43. if (_needShowPlaceholder) {
  44. changeLoadingState(LoadState.State_Loading, null);
  45. }
  46. // 获取 Applied 列表
  47. // var listResult = await _jobRepository.fetchJobAppliedList(
  48. // state.jobId,
  49. // state.selectedStatusId,
  50. // state.keyword,
  51. // curPage: _curPage,
  52. // cancelToken: cancelToken,
  53. // );
  54. //
  55. // // 处理数据
  56. // if (listResult.isSuccess) {
  57. // handleList(listResult.data?.rows);
  58. // } else {
  59. // errorMessage = listResult.errorMsg;
  60. // changeLoadingState(LoadState.State_Error);
  61. // }
  62. await Future.delayed(const Duration(milliseconds: 1500));
  63. List<NotificationGroupData> list = [];
  64. if (_curPage > 1) {
  65. //这里只加载一页吧
  66. } else {
  67. list.add(NotificationGroupData()
  68. ..groupId = "Toady"
  69. ..groupDatas = ["1", "2", "3", "4"]);
  70. list.add(NotificationGroupData()
  71. ..groupId = "Friday 11 oct 2024"
  72. ..groupDatas = ["1", "2", "3"]);
  73. list.add(NotificationGroupData()
  74. ..groupId = "Thursday 10 oct 2024"
  75. ..groupDatas = ["1", "2"]);
  76. list.add(NotificationGroupData()
  77. ..groupId = "Wednesday 9 oct 2024"
  78. ..groupDatas = ["1"]);
  79. }
  80. if (_curPage == 1) {
  81. //刷新的方式
  82. state = state.copyWith(datas: list);
  83. refreshController.finishRefresh();
  84. //更新展示的状态
  85. changeLoadingState(LoadState.State_Success, null);
  86. } else {
  87. //加载更多
  88. final allList = state.datas;
  89. allList.addAll(list);
  90. state.datas.addAll(list);
  91. // refreshController.finishLoad();
  92. refreshController.finishLoad(IndicatorResult.noMore);
  93. state = state.copyWith(datas: allList);
  94. }
  95. // 最后赋值
  96. _needShowPlaceholder = false;
  97. }
  98. // 处理数据与展示的逻辑
  99. // void handleList(List<JobAppliedListSGRows>? list) {
  100. // if (list != null && list.isNotEmpty) {
  101. // //有数据,判断是刷新还是加载更多的数据
  102. // if (_curPage == 1) {
  103. // //刷新的方式
  104. // state.datas.clear();
  105. // state.datas.addAll(list);
  106. // refreshController.finishRefresh();
  107. //
  108. // //更新展示的状态
  109. // changeLoadingState(LoadState.State_Success);
  110. // } else {
  111. // //加载更多
  112. // state.datas.addAll(list);
  113. // refreshController.finishLoad();
  114. // update();
  115. // }
  116. // } else {
  117. // if (_curPage == 1) {
  118. // //展示无数据的布局
  119. // state.datas.clear();
  120. // changeLoadingState(LoadState.State_Empty);
  121. // refreshController.finishRefresh();
  122. // } else {
  123. // //展示加载完成,没有更多数据了
  124. // refreshController.finishLoad(IndicatorResult.noMore);
  125. // }
  126. // }
  127. // }
  128. /// 点击标记全部
  129. void markAll() {
  130. ToastEngine.show("点击标记全部");
  131. }
  132. }