property_sale_vm.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import 'package:domain/entity/property_sale_rent_entity.dart';
  2. import 'package:plugin_platform/http/http_result.dart';
  3. import 'package:riverpod_annotation/riverpod_annotation.dart';
  4. import 'package:shared/utils/log_utils.dart';
  5. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  6. import 'package:widgets/load_state_layout.dart';
  7. import 'package:widgets/widget_export.dart';
  8. import '../page/property_sale_state.dart';
  9. import '../repository/property_sale_repository.dart';
  10. part 'property_sale_vm.g.dart';
  11. @riverpod
  12. class PropertySaleVm extends _$PropertySaleVm {
  13. late PropertySaleRepository propertySaleRepository;
  14. bool _needShowPlaceholder = false; //是否展示LoadingView
  15. // Refresh 控制器
  16. final EasyRefreshController refreshController = EasyRefreshController(
  17. controlFinishRefresh: true, //允许刷新
  18. controlFinishLoad: true, //允许加载
  19. );
  20. PropertySaleState initState() {
  21. return PropertySaleState(
  22. list: [],
  23. );
  24. }
  25. @override
  26. PropertySaleState build() {
  27. // 引入数据仓库
  28. propertySaleRepository = ref.read(propertySaleRepositoryProvider);
  29. // 初始化状态
  30. PropertySaleState state = initState();
  31. // 初始化列表数据
  32. return state;
  33. }
  34. //刷新页面状态
  35. void changeLoadingState(LoadState loadState, String? errorMsg) {
  36. state = state.copyWith(
  37. loadingState: loadState,
  38. errorMessage: errorMsg
  39. );
  40. }
  41. // 初始化页面数据
  42. initPageData() {
  43. Log.d("----property_news_vm-----initPageData");
  44. onRefresh();
  45. }
  46. // 上拉加载 更多
  47. Future loadMore() async {
  48. Log.d("----property_news_vm-----loadMore");
  49. // await Future.delayed(const Duration(seconds: 2));
  50. // if(state.list.length >= state.count){
  51. // return;
  52. // }else {
  53. // int page = state.page + 1;
  54. // state = state.copyWith(page: page,);
  55. // getListData();
  56. // }
  57. // 检查 page 是否为 null,并初始化为 1
  58. int newCurPage = state.page ?? 1;
  59. state = state.copyWith(page: ++newCurPage);
  60. getListData();
  61. }
  62. // 下拉刷新
  63. Future onRefresh() async {
  64. Log.d("----property_news_vm-----onRefresh ");
  65. // await Future.delayed(const Duration(seconds: 2));
  66. state = state.copyWith(page: 1);
  67. getListData();
  68. }
  69. // 重试请求
  70. Future retryRequest() async {
  71. state = state.copyWith(page: 1);
  72. _needShowPlaceholder = true;
  73. getListData();
  74. }
  75. // 获取list 列表数据
  76. Future getListData<T>() async {
  77. if (_needShowPlaceholder) {
  78. changeLoadingState(LoadState.State_Loading, null);
  79. }
  80. Log.d("加载listData数据---------------start--${state.page}---");
  81. // try {
  82. // //请求网络
  83. // Map<String, dynamic> params = {
  84. // "page": state.page,
  85. // "limit": state.limit,
  86. // };
  87. // Log.d("请求参数------$params");
  88. // final result = await propertyNewsRepository.fetchPropertyNewsList(params);
  89. // Log.d("请求完成结果------${result.data}");
  90. // //校验成功失败
  91. // if (result.isSuccess) {
  92. // // state = state.copyWith(serverTime: result.data);
  93. // state = state;
  94. // handleList(listResult.data?.rows);
  95. // ToastEngine.show("获取数据成功");
  96. // } else {
  97. // errorMessage = listResult.errorMsg;
  98. // changeLoadingState(LoadState.State_Error);
  99. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  100. // }
  101. // } catch (e) {
  102. // ToastEngine.show("Error: $e");
  103. // }
  104. // await Future.delayed(const Duration(milliseconds: 1500));
  105. // final List<Map<String, dynamic>> listData = [
  106. // {
  107. // "id": 1,
  108. // "title": "18 Sep 2024 BIK 39#09-XX 1337 psd 1001 sqft",
  109. // "price": "\$1.338 M",
  110. // },
  111. // {
  112. // "id": 2,
  113. // "title": "18 Sep 2024 BIK 39#09-XX 1337 psd 1001 sqft",
  114. // "price": "\$1.338 M",
  115. // },
  116. // ];
  117. //
  118. // if (state.page == 1) {
  119. // //刷新的方式
  120. // state = state.copyWith(list: listData);
  121. // refreshController.finishRefresh();
  122. // //更新展示的状态
  123. // changeLoadingState(LoadState.State_Success, null);
  124. // } else {
  125. // //加载更多
  126. // final allList = state.list;
  127. // if(allList.length >= state.count! * state.limit!){
  128. // //更新展示的状态
  129. // changeLoadingState(LoadState.State_Success, null);
  130. // refreshController.finishLoad(IndicatorResult.noMore, true);
  131. // }else {
  132. // allList.addAll(listData);
  133. // state = state.copyWith(list: allList);
  134. // refreshController.finishLoad();
  135. // }
  136. // }
  137. //
  138. // // 最后赋值
  139. // _needShowPlaceholder = false;
  140. try {
  141. //请求网络
  142. Map<String, dynamic> params = {
  143. "type": 1, // 1 sale 2 rent
  144. "page": state.page,
  145. "limit": state.limit,
  146. };
  147. Log.d("请求参数------$params");
  148. final result = await propertySaleRepository.fetchList(params);
  149. //校验成功失败
  150. if (result.isSuccess) {
  151. // state = state.copyWith(count: (result.data as PropertySaleRentEntity).count);
  152. handlerResultList((result.data as PropertySaleRentEntity).list);
  153. } else {
  154. String errorMessage = result.errorMsg!;
  155. changeLoadingState(LoadState.State_Error, errorMessage);
  156. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  157. }
  158. } catch (e) {
  159. ToastEngine.show("Error: $e");
  160. }
  161. }
  162. void handlerResultList(List<PropertySaleRentList>? list) {
  163. if (list != null && list.isNotEmpty) {
  164. //有数据,判断是刷新还是加载更多的数据
  165. if (state.page == 1) {
  166. //刷新的方式
  167. state.list!.clear();
  168. state.list!.addAll(list.map((item)=> item.toJson()).toList());
  169. refreshController.finishRefresh();
  170. //更新展示的状态
  171. changeLoadingState(LoadState.State_Success, null);
  172. } else {
  173. //加载更多
  174. final allList = state.list;
  175. state = state.copyWith(list: allList);
  176. refreshController.finishLoad();
  177. }
  178. } else {
  179. if (state.page == 1) {
  180. //展示无数据的布局
  181. state.list!.clear();
  182. changeLoadingState(LoadState.State_Empty, null);
  183. refreshController.finishRefresh();
  184. } else {
  185. //展示加载完成,没有更多数据了
  186. if(state.list!.length == 0){
  187. changeLoadingState(LoadState.State_Empty, null);
  188. }else {
  189. changeLoadingState(LoadState.State_Success, null);
  190. }
  191. refreshController.finishLoad(IndicatorResult.noMore);
  192. }
  193. }
  194. }
  195. }