property_rent_vm.dart 7.0 KB

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