property_rent_vm.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'package:domain/entity/property_sale_rent_entity.dart';
  2. import 'package:plugin_platform/http/http_result.dart';
  3. import 'package:riverpod/src/framework.dart';
  4. import 'package:riverpod_annotation/riverpod_annotation.dart';
  5. import 'package:shared/utils/log_utils.dart';
  6. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  7. import 'package:widgets/load_state_layout.dart';
  8. import 'package:widgets/widget_export.dart';
  9. import '../../sale/repository/property_sale_repository.dart';
  10. import '../page/property_rent_state.dart';
  11. part 'property_rent_vm.g.dart';
  12. @riverpod
  13. class PropertyRentVm extends _$PropertyRentVm {
  14. late PropertySaleRepository propertySaleRepository;
  15. bool _needShowPlaceholder = false; //是否展示LoadingView
  16. // Refresh 控制器
  17. final EasyRefreshController refreshController = EasyRefreshController(
  18. controlFinishRefresh: true, //允许刷新
  19. controlFinishLoad: true, //允许加载
  20. );
  21. PropertyRentState initState() {
  22. return PropertyRentState(
  23. list: [],
  24. );
  25. }
  26. @override
  27. PropertyRentState build() {
  28. // 引入数据仓库
  29. propertySaleRepository = ref.read(propertySaleRepositoryProvider);
  30. // 引入数据仓库
  31. // 初始化状态
  32. PropertyRentState state = initState();
  33. // 初始化列表数据
  34. return state;
  35. }
  36. //刷新页面状态
  37. void changeLoadingState(LoadState loadState, String? errorMsg) {
  38. state = state.copyWith(
  39. loadingState: loadState,
  40. errorMessage: errorMsg
  41. );
  42. }
  43. // 初始化页面数据
  44. initPageData() {
  45. Log.d("----property_news_vm-----initPageData");
  46. onRefresh();
  47. }
  48. // 上拉加载 更多
  49. Future loadMore() async {
  50. Log.d("----property_news_vm-----loadMore");
  51. // await Future.delayed(const Duration(seconds: 2));
  52. // if(state.list.length >= state.count){
  53. // return;
  54. // }else {
  55. // int page = state.page + 1;
  56. // state = state.copyWith(page: page,);
  57. // getListData();
  58. // }
  59. // 检查 page 是否为 null,并初始化为 1
  60. int newCurPage = state.page ?? 1;
  61. state = state.copyWith(page: ++newCurPage);
  62. getListData();
  63. }
  64. // 下拉刷新
  65. Future onRefresh() async {
  66. Log.d("----property_news_vm-----onRefresh ");
  67. // await Future.delayed(const Duration(seconds: 2));
  68. state = state.copyWith(page: 1);
  69. getListData();
  70. }
  71. // 重试请求
  72. Future retryRequest() async {
  73. state = state.copyWith(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--${state.page}---");
  83. // try {
  84. // //请求网络
  85. // Map<String, dynamic> params = {
  86. // "page": state.page,
  87. // "limit": state.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": "Jul 2024 Blk XX #XX to XX 1,100 - 1,200 sqft",
  111. // "price": "\$4000",
  112. // "unit": "per month",
  113. // },
  114. // {
  115. // "id": 2,
  116. // "title": "Jul 2024 Blk XX #XX to XX 1,100 - 1,200 sqft",
  117. // "price": "\$4000",
  118. // "unit": "per month",
  119. // },
  120. // ];
  121. //
  122. // if (state.page == 1) {
  123. // //刷新的方式
  124. // state = state.copyWith(list: listData);
  125. // refreshController.finishRefresh();
  126. // //更新展示的状态
  127. // changeLoadingState(LoadState.State_Success, null);
  128. // } else {
  129. // //加载更多
  130. // final allList = state.list;
  131. // if(allList.length >= state.count! * state.limit!){
  132. // //更新展示的状态
  133. // changeLoadingState(LoadState.State_Success, null);
  134. // refreshController.finishLoad(IndicatorResult.noMore, true);
  135. // }else {
  136. // allList.addAll(listData);
  137. // state = state.copyWith(list: allList);
  138. // refreshController.finishLoad();
  139. // }
  140. // }
  141. //
  142. // // 最后赋值
  143. // _needShowPlaceholder = false;
  144. try {
  145. //请求网络
  146. Map<String, dynamic> params = {
  147. "type": 2, // 1 sale 2 rent
  148. "page": state.page,
  149. "limit": state.limit,
  150. };
  151. Log.d("请求参数------$params");
  152. final result = await propertySaleRepository.fetchList(params);
  153. //校验成功失败
  154. if (result.isSuccess) {
  155. // state = state.copyWith(count: (result.data as PropertySaleRentEntity).count);
  156. handlerResultList((result.data as PropertySaleRentEntity).list);
  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. void handlerResultList(List<PropertySaleRentList>? list) {
  167. if (list != null && list.isNotEmpty) {
  168. //有数据,判断是刷新还是加载更多的数据
  169. if (state.page == 1) {
  170. //刷新的方式
  171. state.list!.clear();
  172. state.list!.addAll(list.map((item)=> item.toJson()).toList());
  173. refreshController.finishRefresh();
  174. //更新展示的状态
  175. changeLoadingState(LoadState.State_Success, null);
  176. } else {
  177. //加载更多
  178. final allList = state.list;
  179. state = state.copyWith(list: allList);
  180. refreshController.finishLoad();
  181. // update();
  182. }
  183. } else {
  184. if (state.page == 1) {
  185. //展示无数据的布局
  186. state.list!.clear();
  187. changeLoadingState(LoadState.State_Empty, null);
  188. refreshController.finishRefresh();
  189. } else {
  190. //展示加载完成,没有更多数据了
  191. if(state.list!.length == 0){
  192. changeLoadingState(LoadState.State_Empty, null);
  193. }else {
  194. changeLoadingState(LoadState.State_Success, null);
  195. }
  196. refreshController.finishLoad(IndicatorResult.noMore);
  197. }
  198. }
  199. }
  200. }