in_progress_vm.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:domain/entity/garage_sale_rent_entity.dart';
  3. import 'package:domain/entity/newsfeed_detail_entity.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  6. import 'package:riverpod_annotation/riverpod_annotation.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:widgets/load_state_layout.dart';
  10. import 'package:widgets/widget_export.dart';
  11. import '../../../respository/services_respository.dart';
  12. import '../../../router/page/services_page_router.dart';
  13. import '../clean_order_detail/clean_order_detail_page.dart';
  14. import 'in_progress_state.dart';
  15. part 'in_progress_vm.g.dart';
  16. @riverpod
  17. class InProgressVm extends _$InProgressVm {
  18. late ServicesRespository servicesRespositoryInstance;
  19. bool _needShowPlaceholder = false; //是否展示LoadingView
  20. int _page = 1; // 当前页
  21. int _limit = 10; // 每页数量
  22. int _count = 0; // 总条数
  23. Map<String, dynamic> _queryParams = {
  24. 'category_id': null,
  25. 'keyword': null,
  26. 'is_liked': null,
  27. };
  28. // Refresh 控制器
  29. final EasyRefreshController refreshController = EasyRefreshController(
  30. controlFinishRefresh: true, //允许刷新
  31. controlFinishLoad: true, //允许加载
  32. );
  33. InProgressState initState() {
  34. return InProgressState(
  35. list: []
  36. );
  37. }
  38. @override
  39. InProgressState build(){
  40. // 引入数据仓库
  41. // servicesRespositoryInstance = ref.read(commonGarageRespositoryProvider);
  42. final state = initState();
  43. Log.d("--------------------------build---------------------");
  44. return state;
  45. }
  46. //刷新页面状态
  47. void changeLoadingState(LoadState loadState, String? errorMsg) {
  48. state = state.copyWith(
  49. loadingState: loadState,
  50. errorMessage: errorMsg
  51. );
  52. }
  53. // 初始化页面数据
  54. initPageData() {
  55. Log.d("----home_service_vm-----initPageData ${state.loadingState}");
  56. onRefresh();
  57. }
  58. // 上拉加载 更多
  59. Future loadMore() async {
  60. Log.d("----home_service_vm-----loadMore");
  61. _page++;
  62. getListData();
  63. }
  64. // 下拉刷新
  65. Future onRefresh() async {
  66. // 当前pageView 页面正处于显示状态
  67. Log.d("----forsale_vm-----onRefresh ");
  68. // await Future.delayed(const Duration(seconds: 2));
  69. _page = 1;
  70. getListData();
  71. }
  72. // 手动进行刷新
  73. Future triggerRefresh() async {
  74. Log.d("trggerRefresh");
  75. refreshController.callRefresh();
  76. }
  77. // 手动进行刷新
  78. Future directRefresh() async {
  79. state = state.copyWith(list:[]);
  80. // 注意:由于 nestedscrollview 嵌套easyfresh 组件 refreshController.callRefresh() 自动刷新只能滚动顶部但是不会触发下拉刷新,这里调用是 用到了将其滚动到顶部的作用,进而刷新操作主动掉接口
  81. // https://github.com/xuelongqy/flutter_easy_refresh/issues/692
  82. refreshController.callRefresh();
  83. refreshController.resetFooter();
  84. _page = 1;
  85. _needShowPlaceholder = true;
  86. getListData();
  87. }
  88. // 重试请求
  89. Future retryRequest() async {
  90. _page = 1;
  91. _needShowPlaceholder = true;
  92. getListData();
  93. }
  94. // 获取list 列表数据
  95. Future getListData<T>({bool? isLoadMore}) async {
  96. if (_needShowPlaceholder) {
  97. changeLoadingState(LoadState.State_Loading, null);
  98. }
  99. List<Map<String, dynamic>> list = [
  100. {
  101. 'id':1,
  102. 'service_type': 0, // 0 房屋保洁 1 空调保洁 2 维修
  103. 'cover_img': 'https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500',
  104. 'resources': ['https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500'],
  105. 'title': 'House Cleaning Services',
  106. 'duration': 'Daily cleaning for 2 hours',
  107. 'totalPrice': 66,
  108. 'visit_time': '14 0ct 2024 15:00',
  109. 'order_time': '13 0ct 2024 12:00',
  110. 'status_text': 'In Progress',
  111. 'status_code': 0,
  112. 'company_name': 'HONG YE GROUP PTE LTD',
  113. },
  114. {
  115. 'id':2,
  116. 'service_type': 0, // 0 房屋保洁 1 空调保洁 2 维修
  117. 'cover_img': 'https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500',
  118. 'resources': ['https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500'],
  119. 'title': 'House Cleaning Services',
  120. 'duration': 'Daily cleaning for 2 hours',
  121. 'totalPrice': 66,
  122. 'visit_time': '14 0ct 2024 15:00',
  123. 'order_time': '13 0ct 2024 12:00',
  124. 'status_text': 'In Progress',
  125. 'status_code': 1,
  126. 'company_name': 'HONG YE GROUP PTE LTD',
  127. },
  128. {
  129. 'id':3,
  130. 'service_type': 0, // 0 房屋保洁 1 空调保洁 2 维修
  131. 'cover_img': 'https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500',
  132. 'resources': ['https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500'],
  133. 'title': 'House Cleaning Services',
  134. 'duration': 'Daily cleaning for 2 hours',
  135. 'totalPrice': 66,
  136. 'visit_time': '14 0ct 2024 15:00',
  137. 'order_time': '13 0ct 2024 12:00',
  138. 'status_text': 'In Progress',
  139. 'status_code': 1,
  140. 'company_name': 'HONG YE GROUP PTE LTD',
  141. },
  142. ];
  143. handlerResultData(true, list:list);
  144. // try {
  145. // //请求网络
  146. // Map<String, dynamic> params = {
  147. // "type": 1, // 类型(1=Sale,2=Rent)
  148. // "category_id": _queryParams['category_id'],
  149. // "keyword": _queryParams['keyword'],
  150. // "page": _page,
  151. // "limit": _limit,
  152. // };
  153. // Log.d("请求参数------$params");
  154. // final result = await servicesRespositoryInstance.fetchGarageDataList(params);
  155. // //校验成功失败
  156. // if (result.isSuccess) {
  157. // // handlerResultList((result.data as GarageSaleRentEntity).list as List<GarageSaleRentList>, isLoadMore ?? false);
  158. // } else {
  159. // String errorMessage = result.errorMsg!;
  160. // changeLoadingState(LoadState.State_Error, errorMessage);
  161. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  162. // }
  163. // } catch (e) {
  164. // ToastEngine.show("Error: $e");
  165. // }
  166. // 最后赋值
  167. _needShowPlaceholder = false;
  168. }
  169. handlerResultData(bool isList, {List<Map<String, dynamic>>? list, dynamic? data}){
  170. Future.delayed(const Duration(seconds: 1)).then((value) {
  171. if(isList){
  172. // list 数据模式
  173. if(list != null && list.isNotEmpty){
  174. if(_page == 1){
  175. state.list.clear();
  176. state.list!.addAll(list);
  177. refreshController.finishRefresh();
  178. changeLoadingState(LoadState.State_Success, null);
  179. }else {
  180. final allList = state.list;
  181. allList!.addAll(list);
  182. state = state.copyWith(list: allList);
  183. refreshController.finishLoad();
  184. }
  185. }else {
  186. if(_page == 1){
  187. state.list.clear();
  188. changeLoadingState(LoadState.State_Empty, null);
  189. refreshController.finishRefresh();
  190. }else {
  191. refreshController.finishLoad(IndicatorResult.noMore);
  192. }
  193. }
  194. }else {
  195. // 单个数据模式
  196. if(data!=null){
  197. if(_page == 1){
  198. refreshController.finishRefresh();
  199. }else{
  200. refreshController.finishLoad();
  201. }
  202. changeLoadingState(LoadState.State_Success, null);
  203. }else {
  204. if(_page == 1){
  205. refreshController.finishRefresh();
  206. }else{
  207. refreshController.finishLoad();
  208. }
  209. changeLoadingState(LoadState.State_Empty, null);
  210. }
  211. }
  212. });
  213. }
  214. void handlerResultList(List<GarageSaleRentList>? list, bool isLoadMore) {
  215. if (list != null && list.isNotEmpty) {
  216. //有数据,判断是刷新还是加载更多的数据
  217. if (_page == 1) {
  218. //刷新的方式
  219. state.list!.clear();
  220. state.list!.addAll(list.map((item) => item.toJson()).toList());
  221. refreshController.finishRefresh();
  222. //更新展示的状态
  223. changeLoadingState(LoadState.State_Success, null);
  224. } else {
  225. //加载更多
  226. final allList = state.list;
  227. allList!.addAll(list.map((item) => item.toJson()).toList());
  228. state = state.copyWith(list: allList);
  229. refreshController.finishLoad();
  230. }
  231. } else {
  232. if (_page == 1) {
  233. //展示无数据的布局
  234. state.list!.clear();
  235. changeLoadingState(LoadState.State_Empty, null);
  236. refreshController.finishRefresh();
  237. } else {
  238. //展示加载完成,没有更多数据了
  239. if (_page == 1) {
  240. //展示无数据的布局
  241. state.list!.clear();
  242. changeLoadingState(LoadState.State_Empty, null);
  243. refreshController.finishRefresh();
  244. } else {
  245. //展示加载完成,没有更多数据了
  246. if(state.list!.length == 0){
  247. changeLoadingState(LoadState.State_Empty, null);
  248. refreshController.finishLoad();
  249. }else {
  250. if(_needShowPlaceholder){
  251. changeLoadingState(LoadState.State_Success, null);
  252. }
  253. }
  254. //更新展示的状态
  255. refreshController.finishLoad(IndicatorResult.noMore);
  256. }
  257. }
  258. }
  259. }
  260. // 设置当前的 _queryParams
  261. setCurrentQueryParams(Map<String, dynamic> params){
  262. _queryParams.addAll(params);
  263. }
  264. // 获取当前的 _queryParams
  265. Map<String, dynamic> getCurrentQueryParams(String? key){
  266. if(key!=null && key!.isNotEmpty){
  267. return _queryParams[key];
  268. }
  269. return _queryParams;
  270. }
  271. // 去详情页面
  272. gotoCleanOrderDetailPage(BuildContext context, int id, int serviceTypeCode, int serviesStatusCode){
  273. CleanOrderDetailPage.startInstance(id: id, serviceTypeCode: serviceTypeCode, serviesStatusCode: serviesStatusCode);
  274. }
  275. }