applied_add_staff_controller.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:domain/entity/response/job_list_applied_staff_search_entity.dart';
  2. import 'package:domain/repository/job_repository.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:plugin_basic/basic_export.dart';
  5. import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
  6. import 'package:shared/utils/log_utils.dart';
  7. import 'package:widgets/load_state_layout.dart';
  8. import 'package:widgets/widget_export.dart';
  9. class AppliedAddStaffController extends GetxController with DioCancelableMixin {
  10. final JobRepository _jobRepository = Get.find();
  11. TextEditingController searchController = TextEditingController();
  12. var _curPage = 1;
  13. var _needShowPlaceholder = true;
  14. //页面PlaceHolder的展示
  15. LoadState loadingState = LoadState.State_Success;
  16. String? errorMessage;
  17. String keyword = "";
  18. String? jobId = null;
  19. List<JobListAppliedStaffSearchRows> datas = [];
  20. //刷新页面状态
  21. void changeLoadingState(LoadState state) {
  22. loadingState = state;
  23. update();
  24. }
  25. // Refresh 控制器
  26. final EasyRefreshController refreshController = EasyRefreshController(
  27. controlFinishRefresh: true,
  28. controlFinishLoad: true,
  29. );
  30. // Refresh 刷新事件
  31. Future onRefresh() async {
  32. _curPage = 1;
  33. fetchAllStaffList();
  34. }
  35. // Refresh 加载事件
  36. Future loadMore() async {
  37. _curPage++;
  38. fetchAllStaffList();
  39. }
  40. // 重试请求
  41. Future retryRequest() async {
  42. _curPage = 1;
  43. _needShowPlaceholder = true;
  44. fetchAllStaffList();
  45. }
  46. /// 获取列表数据
  47. Future fetchAllStaffList() async {
  48. if (_needShowPlaceholder) {
  49. changeLoadingState(LoadState.State_Loading);
  50. }
  51. // 获取 Staff 列表
  52. var listResult = await _jobRepository.searchStaffList(
  53. jobId,
  54. keyword,
  55. curPage: _curPage,
  56. cancelToken: cancelToken,
  57. );
  58. // 处理数据
  59. if (listResult.isSuccess) {
  60. handleList(listResult.data?.rows);
  61. } else {
  62. errorMessage = listResult.errorMsg;
  63. changeLoadingState(LoadState.State_Error);
  64. }
  65. // 最后赋值
  66. _needShowPlaceholder = false;
  67. }
  68. // 处理数据与展示的逻辑
  69. void handleList(List<JobListAppliedStaffSearchRows>? list) {
  70. if (list != null && list.isNotEmpty) {
  71. //有数据,判断是刷新还是加载更多的数据
  72. if (_curPage == 1) {
  73. //刷新的方式
  74. datas.clear();
  75. datas.addAll(list);
  76. refreshController.finishRefresh();
  77. //更新展示的状态
  78. changeLoadingState(LoadState.State_Success);
  79. } else {
  80. //加载更多
  81. datas.addAll(list);
  82. refreshController.finishLoad();
  83. update();
  84. }
  85. } else {
  86. if (_curPage == 1) {
  87. //展示无数据的布局
  88. datas.clear();
  89. changeLoadingState(LoadState.State_Empty);
  90. refreshController.finishRefresh();
  91. } else {
  92. //展示加载完成,没有更多数据了
  93. refreshController.finishLoad(IndicatorResult.noMore);
  94. }
  95. }
  96. }
  97. //搜索
  98. void doSearch(String keyword) {
  99. this.keyword = keyword;
  100. refreshController.callRefresh();
  101. }
  102. @override
  103. void onReady() {
  104. super.onReady();
  105. fetchAllStaffList();
  106. }
  107. @override
  108. void onClose() {
  109. datas.clear();
  110. super.onClose();
  111. }
  112. }