property_sale_vm.dart 6.8 KB

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