in_progress_vm.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:domain/entity/garage_sale_history_entity.dart';
  3. import 'package:domain/entity/garage_sale_rent_entity.dart';
  4. import 'package:domain/entity/newsfeed_detail_entity.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:plugin_basic/constants/app_constant.dart';
  7. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  8. import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
  9. import 'package:riverpod_annotation/riverpod_annotation.dart';
  10. import 'package:router/ext/auto_router_extensions.dart';
  11. import 'package:shared/utils/log_utils.dart';
  12. import 'package:widgets/load_state_layout.dart';
  13. import 'package:widgets/widget_export.dart';
  14. import '../../../respository/services_respository.dart';
  15. import '../../../router/page/services_page_router.dart';
  16. import '../clean_order_detail/clean_order_detail_page.dart';
  17. import 'in_progress_state.dart';
  18. import 'package:shared/utils/event_bus.dart';
  19. part 'in_progress_vm.g.dart';
  20. @riverpod
  21. class InProgressVm extends _$InProgressVm with DioCancelableMixin {
  22. late ServicesRespository servicesRespositoryInstance;
  23. bool _needShowPlaceholder = false; //是否展示LoadingView
  24. int _page = 1; // 当前页
  25. int _limit = 10; // 每页数量
  26. int _count = 0; // 总条数
  27. Map<String, dynamic> _queryParams = {
  28. 'category_id': null,
  29. 'keyword': null,
  30. 'is_liked': null,
  31. };
  32. // Refresh 控制器
  33. final EasyRefreshController refreshController = EasyRefreshController(
  34. controlFinishRefresh: true, //允许刷新
  35. controlFinishLoad: true, //允许加载
  36. );
  37. InProgressState initState() {
  38. return InProgressState(list: []);
  39. }
  40. @override
  41. InProgressState build() {
  42. servicesRespositoryInstance = ref.read(servicesRespositoryProvider);
  43. // 引入数据仓库
  44. // servicesRespositoryInstance = ref.read(commonGarageRespositoryProvider);
  45. final state = initState();
  46. registerEventBus();
  47. Log.d("--------------------------build---------------------");
  48. registerCancellation(callback: () {
  49. unregisterEventBus();
  50. });
  51. return state;
  52. }
  53. //刷新页面状态
  54. void changeLoadingState(LoadState loadState, String? errorMsg) {
  55. state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
  56. }
  57. // 初始化页面数据
  58. initPageData() {
  59. Log.d("----home_service_vm-----initPageData ${state.loadingState}");
  60. onRefresh();
  61. }
  62. // 上拉加载 更多
  63. Future loadMore() async {
  64. Log.d("----home_service_vm-----loadMore");
  65. _page++;
  66. getListData();
  67. }
  68. // 下拉刷新
  69. Future onRefresh() async {
  70. // 当前pageView 页面正处于显示状态
  71. Log.d("----forsale_vm-----onRefresh ");
  72. // await Future.delayed(const Duration(seconds: 2));
  73. _page = 1;
  74. getListData();
  75. }
  76. // 手动进行刷新
  77. Future triggerRefresh() async {
  78. Log.d("trggerRefresh");
  79. refreshController.callRefresh();
  80. }
  81. // 手动进行刷新
  82. Future directRefresh() async {
  83. state = state.copyWith(list: []);
  84. // 注意:由于 nestedscrollview 嵌套easyfresh 组件 refreshController.callRefresh() 自动刷新只能滚动顶部但是不会触发下拉刷新,这里调用是 用到了将其滚动到顶部的作用,进而刷新操作主动掉接口
  85. // https://github.com/xuelongqy/flutter_easy_refresh/issues/692
  86. refreshController.callRefresh();
  87. refreshController.resetFooter();
  88. _page = 1;
  89. _needShowPlaceholder = true;
  90. getListData();
  91. }
  92. // 重试请求
  93. Future retryRequest() async {
  94. _page = 1;
  95. _needShowPlaceholder = true;
  96. getListData();
  97. }
  98. // 获取list 列表数据
  99. Future getListData<T>({bool? isLoadMore}) async {
  100. if (_needShowPlaceholder) {
  101. changeLoadingState(LoadState.State_Loading, null);
  102. }
  103. // 获取列表
  104. var listResult = await servicesRespositoryInstance.fetchPaidServiceOrderList(
  105. type: "in_progress",
  106. curPage: _page,
  107. cancelToken: cancelToken,
  108. );
  109. // 处理数据
  110. if (listResult.isSuccess) {
  111. handleList(listResult.data?.list);
  112. } else {
  113. changeLoadingState(LoadState.State_Error, listResult.errorMsg);
  114. }
  115. // 最后赋值
  116. _needShowPlaceholder = false;
  117. }
  118. // 处理数据与展示的逻辑
  119. void handleList(List<GarageSaleHistoryList>? list) {
  120. if (list != null && list.isNotEmpty) {
  121. //有数据,判断是刷新还是加载更多的数据
  122. if (_page == 1) {
  123. //刷新的方式
  124. state = state.copyWith(list: list);
  125. refreshController.finishRefresh();
  126. //更新展示的状态
  127. changeLoadingState(LoadState.State_Success, null);
  128. } else {
  129. //加载更多
  130. state.list.addAll(List<GarageSaleHistoryList>.from(state.list)..addAll(list));
  131. refreshController.finishLoad();
  132. }
  133. } else {
  134. if (_page == 1) {
  135. //展示无数据的布局
  136. state = state.copyWith(list: []);
  137. changeLoadingState(LoadState.State_Empty, null);
  138. refreshController.finishRefresh();
  139. } else {
  140. //展示加载完成,没有更多数据了
  141. refreshController.finishLoad(IndicatorResult.noMore);
  142. }
  143. }
  144. }
  145. // 设置当前的 _queryParams
  146. setCurrentQueryParams(Map<String, dynamic> params) {
  147. _queryParams.addAll(params);
  148. }
  149. // 获取当前的 _queryParams
  150. Map<String, dynamic> getCurrentQueryParams(String? key) {
  151. if (key != null && key!.isNotEmpty) {
  152. return _queryParams[key];
  153. }
  154. return _queryParams;
  155. }
  156. // EventBus 的事件接收
  157. Subscription? subscribe;
  158. void registerEventBus() {
  159. subscribe = bus.on(AppConstant.eventServiceOrderRefresh, (arg) {
  160. if (arg is String) {
  161. final String id = arg;
  162. Log.d("需要删除的订单id:$id");
  163. //从页面列表中找到对应的id并删除对应的Item
  164. final updatedList = state.list.where((element) => element.id != id).toList();
  165. state = state.copyWith(list: updatedList);
  166. }
  167. });
  168. }
  169. void unregisterEventBus() {
  170. bus.off(AppConstant.eventServiceOrderRefresh, subscribe);
  171. }
  172. // 去详情页面
  173. gotoCleanOrderDetailPage(BuildContext context, int id, String cleanServiceType, int serviesStatusCode) {
  174. CleanOrderDetailPage.startInstance(id: id, cleanServiceType: cleanServiceType, serviesStatusCode: serviesStatusCode);
  175. }
  176. }