services_view_model.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'package:cpt_services/modules/services/dialog/account_deactivation_dialog.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
  4. import 'package:plugin_platform/http/http_result.dart';
  5. import 'package:riverpod_annotation/riverpod_annotation.dart';
  6. import 'package:shared/utils/log_utils.dart';
  7. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  8. import 'services_state.dart';
  9. import 'services_repository.dart';
  10. part 'services_view_model.g.dart';
  11. @riverpod
  12. class ServicesVm extends _$ServicesVm {
  13. late ServicesRepository servicesRepository;
  14. ServicesState initState() {
  15. return ServicesState(
  16. curPage: 1,
  17. pageSize: 10,
  18. list: [
  19. {
  20. "id": 1,
  21. "title": "Part-TimeCleaning",
  22. "price": Assets.serviceServiceIcon1,
  23. },
  24. {
  25. "id": 2,
  26. "title": "Move in/outcleaning",
  27. "price": Assets.serviceServiceIcon2,
  28. },
  29. {
  30. "id": 3,
  31. "title": "Laundry AnddryCleaning",
  32. "price": Assets.serviceServiceIcon3,
  33. },
  34. {
  35. "id": 4,
  36. "title": "SofaCleaning",
  37. "price": Assets.serviceServiceIcon4,
  38. },
  39. {
  40. "id": 5,
  41. "title": "MattressCleaning",
  42. "price": Assets.serviceServiceIcon5,
  43. },
  44. {
  45. "id": 5,
  46. "title": "MarbleCleaning",
  47. "price": Assets.serviceServiceIcon6,
  48. },
  49. {
  50. "id": 6,
  51. "title": "ParquetCleaning",
  52. "price": Assets.serviceServiceIcon7,
  53. },
  54. {
  55. "id": 6,
  56. "title": "Removalist",
  57. "price": Assets.serviceServiceIcon8,
  58. },
  59. {
  60. "id": 7,
  61. "title": "CarpetCleaning",
  62. "price": Assets.serviceServiceIcon9,
  63. },
  64. {
  65. "id": 7,
  66. "title": "InteriorDesign",
  67. "price": Assets.serviceServiceIcon10,
  68. },
  69. {
  70. "id": 7,
  71. "title": "DomesticHelper agency",
  72. "price": Assets.serviceServiceIcon11,
  73. },
  74. {
  75. "id": 7,
  76. "title": "Recycling AndDisposal Service",
  77. "price": Assets.serviceServiceIcon12,
  78. },
  79. {
  80. "id": 7,
  81. "title": "Sell YourPre-Loved Items",
  82. "price": Assets.serviceServiceIcon13,
  83. },
  84. {
  85. "id": 7,
  86. "title": "UpholsteryCleaning",
  87. "price": Assets.serviceServiceIcon14,
  88. },
  89. ],
  90. filterCount: 6,
  91. );
  92. }
  93. @override
  94. ServicesState build() {
  95. // 引入数据仓库
  96. servicesRepository = ref.read(servicesRepositoryProvider);
  97. // 初始化状态
  98. ServicesState state = initState();
  99. // 初始化列表数据
  100. return state;
  101. }
  102. // 初始化页面数据
  103. initPageData() {
  104. Log.d("----property_news_vm-----initPageData");
  105. refreshListData();
  106. }
  107. // 上拉加载
  108. Future onLoadData() async {
  109. Log.d("----property_news_vm-----initListData");
  110. // await Future.delayed(const Duration(seconds: 2));
  111. // if(state.list.length >= state.filterCount){
  112. // return;
  113. // }else {
  114. // int curPage = state.curPage + 1;
  115. // state = state.copyWith(curPage: curPage,);
  116. // getListData();
  117. // }
  118. getListData();
  119. }
  120. // 去新闻详情页
  121. void goNewsDetail(String item) {
  122. Log.d(item);
  123. // PropertyPage.startInstance(context: context, item: item);
  124. }
  125. // 获取list 列表数据
  126. void getListData<T>() async {
  127. Log.d("加载listData数据---------------start-----");
  128. try {
  129. //请求网络
  130. Map<String, dynamic> params = {
  131. "curPage": state.curPage,
  132. "pageSize": state.pageSize,
  133. };
  134. Log.d("请求参数------$params");
  135. final result = await servicesRepository.fetchPropertyNewsList(params);
  136. Log.d("请求完成结果------${result.data}");
  137. //校验成功失败
  138. if (result.isSuccess) {
  139. // state = state.copyWith(serverTime: result.data);
  140. state = state;
  141. ToastEngine.show("获取数据成功");
  142. } else {
  143. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  144. }
  145. } catch (e) {
  146. ToastEngine.show("Error: $e");
  147. }
  148. }
  149. // 下拉刷新
  150. Future refreshListData() async {
  151. Log.d("----property_news_vm-----refreshListData ");
  152. // await Future.delayed(const Duration(seconds: 2));
  153. state = state.copyWith(curPage: 1, pageSize: 10);
  154. // ref.invalidateSelf();
  155. // ref.invalidate(propertyNewsVmProvider);
  156. getListData();
  157. }
  158. void doDeleteAccount() {
  159. DialogEngine.show(widget: AccountDeactivationDialog(
  160. confirmAction: () {
  161. ToastEngine.show("点击了确定");
  162. },
  163. ));
  164. }
  165. }