in_progress_vm.dart 11 KB

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