manage_view_model.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
  2. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  3. import 'package:riverpod_annotation/riverpod_annotation.dart';
  4. import 'package:shared/utils/log_utils.dart';
  5. import 'package:widgets/dialog/app_default_dialog.dart';
  6. import 'package:widgets/load_state_layout.dart';
  7. import 'package:widgets/widget_export.dart';
  8. import 'manage_state.dart';
  9. part 'manage_view_model.g.dart';
  10. @riverpod
  11. class ManageViewModel extends _$ManageViewModel {
  12. var _curPage = 1; //请求参数当前的页面
  13. var _needShowPlaceholder = true; //是否展示LoadingView
  14. // Refresh 控制器
  15. final EasyRefreshController refreshController = EasyRefreshController(
  16. controlFinishRefresh: true, //允许刷新
  17. controlFinishLoad: true, //允许加载
  18. );
  19. @override
  20. ManageState build() {
  21. return ManageState(datas: []);
  22. }
  23. //刷新页面状态
  24. void changeLoadingState(LoadState loadState, String? errorMsg) {
  25. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  26. }
  27. // Refresh 刷新事件
  28. Future onRefresh() async {
  29. _curPage = 1;
  30. fetchList();
  31. }
  32. // Refresh 加载事件
  33. Future loadMore() async {
  34. _curPage++;
  35. fetchList();
  36. }
  37. // 重试请求
  38. Future retryRequest() async {
  39. _curPage = 1;
  40. _needShowPlaceholder = true;
  41. fetchList();
  42. }
  43. /// 获取服务器数据
  44. Future fetchList() async {
  45. if (_needShowPlaceholder) {
  46. changeLoadingState(LoadState.State_Loading, null);
  47. }
  48. // 获取 Applied 列表
  49. // var listResult = await _jobRepository.fetchJobAppliedList(
  50. // state.jobId,
  51. // state.selectedStatusId,
  52. // state.keyword,
  53. // curPage: _curPage,
  54. // cancelToken: cancelToken,
  55. // );
  56. //
  57. // // 处理数据
  58. // if (listResult.isSuccess) {
  59. // handleList(listResult.data?.rows);
  60. // } else {
  61. // errorMessage = listResult.errorMsg;
  62. // changeLoadingState(LoadState.State_Error);
  63. // }
  64. await Future.delayed(const Duration(milliseconds: 1500));
  65. final List<bool> list = [true, false, false, false];
  66. if (_curPage == 1) {
  67. //刷新的方式
  68. state = state.copyWith(datas: list);
  69. refreshController.finishRefresh();
  70. //更新展示的状态
  71. changeLoadingState(LoadState.State_Success, null);
  72. } else {
  73. //加载更多
  74. final allList = state.datas;
  75. allList.addAll(list);
  76. state.datas.addAll(list);
  77. refreshController.finishLoad();
  78. state = state.copyWith(datas: allList);
  79. }
  80. // 最后赋值
  81. _needShowPlaceholder = false;
  82. }
  83. // 处理数据与展示的逻辑
  84. // void handleList(List<JobAppliedListSGRows>? list) {
  85. // if (list != null && list.isNotEmpty) {
  86. // //有数据,判断是刷新还是加载更多的数据
  87. // if (_curPage == 1) {
  88. // //刷新的方式
  89. // state.datas.clear();
  90. // state.datas.addAll(list);
  91. // refreshController.finishRefresh();
  92. //
  93. // //更新展示的状态
  94. // changeLoadingState(LoadState.State_Success);
  95. // } else {
  96. // //加载更多
  97. // state.datas.addAll(list);
  98. // refreshController.finishLoad();
  99. // update();
  100. // }
  101. // } else {
  102. // if (_curPage == 1) {
  103. // //展示无数据的布局
  104. // state.datas.clear();
  105. // changeLoadingState(LoadState.State_Empty);
  106. // refreshController.finishRefresh();
  107. // } else {
  108. // //展示加载完成,没有更多数据了
  109. // refreshController.finishLoad(IndicatorResult.noMore);
  110. // }
  111. // }
  112. // }
  113. //设置为主卡
  114. void setAsPrimary(int index) {
  115. bool isPrimary = state.datas[index];
  116. if (!isPrimary) {
  117. // 创建数据列表的副本
  118. List<bool> newDataList = List.from(state.datas);
  119. //先全部设置为 false
  120. for (int i = 0; i < newDataList.length; i++) {
  121. newDataList[i] = false;
  122. }
  123. //再把当前索引设置为 true
  124. newDataList[index] = true;
  125. state = state.copyWith(datas: newDataList);
  126. }
  127. }
  128. // 删除提示弹窗
  129. void showDeleteDialog(int index) {
  130. DialogEngine.show(
  131. widget: AppDefaultDialog(
  132. message: "Are you sure you want to delete this Card?",
  133. confirmAction: () {
  134. ToastEngine.show("点击了确定");
  135. },
  136. ));
  137. }
  138. //去添加新卡的页面
  139. void gotoAddCardPage() {
  140. }
  141. }