manage_view_model.dart 4.4 KB

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