manage_view_model.dart 4.4 KB

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