property_news_vm.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import 'package:plugin_platform/http/http_result.dart';
  2. import 'package:riverpod_annotation/riverpod_annotation.dart';
  3. import 'package:shared/utils/log_utils.dart';
  4. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  5. import 'package:widgets/load_state_layout.dart';
  6. import 'package:widgets/widget_export.dart';
  7. import '../page/property_news_state.dart';
  8. import '../repository/property_news_repository.dart';
  9. part 'property_news_vm.g.dart';
  10. @riverpod
  11. class PropertyNewsVm extends _$PropertyNewsVm {
  12. late PropertyNewsRepository propertyNewsRepository;
  13. bool _needShowPlaceholder = true; //是否展示LoadingView
  14. // Refresh 控制器
  15. final EasyRefreshController refreshController = EasyRefreshController(
  16. controlFinishRefresh: true, //允许刷新
  17. controlFinishLoad: true, //允许加载
  18. );
  19. PropertyNewsState initState() {
  20. return PropertyNewsState(
  21. list: [],
  22. );
  23. }
  24. @override
  25. PropertyNewsState build() {
  26. // 引入数据仓库
  27. propertyNewsRepository = ref.read(propertyNewsRepositoryProvider);
  28. // 初始化状态
  29. PropertyNewsState state = initState();
  30. // 初始化列表数据
  31. return state;
  32. }
  33. //刷新页面状态
  34. void changeLoadingState(LoadState loadState, String? errorMsg) {
  35. state = state.copyWith(
  36. loadingState: loadState,
  37. errorMessage: errorMsg
  38. );
  39. }
  40. // 初始化页面数据
  41. initPageData() {
  42. Log.d("----property_news_vm-----initPageData");
  43. onRefresh();
  44. }
  45. // 上拉加载 更多
  46. Future loadMore() async {
  47. Log.d("----property_news_vm-----loadMore");
  48. // await Future.delayed(const Duration(seconds: 2));
  49. // if(state.list.length >= state.filterCount){
  50. // return;
  51. // }else {
  52. // int curPage = state.curPage + 1;
  53. // state = state.copyWith(curPage: curPage,);
  54. // getListData();
  55. // }
  56. // 检查 curPage 是否为 null,并初始化为 1
  57. int newCurPage = state.curPage ?? 1;
  58. state = state.copyWith(curPage: ++newCurPage);
  59. getListData();
  60. }
  61. // 下拉刷新
  62. Future onRefresh() async {
  63. Log.d("----property_news_vm-----onRefresh ");
  64. // await Future.delayed(const Duration(seconds: 2));
  65. state = state.copyWith(curPage: 1);
  66. getListData();
  67. }
  68. // 重试请求
  69. Future retryRequest() async {
  70. state = state.copyWith(curPage: 1);
  71. _needShowPlaceholder = true;
  72. getListData();
  73. }
  74. // 获取list 列表数据
  75. Future getListData<T>() async {
  76. if (_needShowPlaceholder) {
  77. changeLoadingState(LoadState.State_Loading, null);
  78. }
  79. Log.d("加载listData数据---------------start--${state.curPage}---");
  80. // try {
  81. // //请求网络
  82. // Map<String, dynamic> params = {
  83. // "curPage": state.curPage,
  84. // "pageSize": state.pageSize,
  85. // };
  86. // Log.d("请求参数------$params");
  87. // final result = await propertyNewsRepository.fetchPropertyNewsList(params);
  88. // Log.d("请求完成结果------${result.data}");
  89. // //校验成功失败
  90. // if (result.isSuccess) {
  91. // // state = state.copyWith(serverTime: result.data);
  92. // state = state;
  93. // handleList(listResult.data?.rows);
  94. // ToastEngine.show("获取数据成功");
  95. // } else {
  96. // errorMessage = listResult.errorMsg;
  97. // changeLoadingState(LoadState.State_Error);
  98. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  99. // }
  100. // } catch (e) {
  101. // ToastEngine.show("Error: $e");
  102. // }
  103. await Future.delayed(const Duration(milliseconds: 1500));
  104. final List<Map<String, dynamic>> listData = [
  105. {
  106. "id": 1,
  107. "title": "fkladsfk fldask fldsakfllfkaslsd",
  108. "description": "fsklfdsk罚款乱收费上课了发送卡",
  109. "time": "2024-02-15 12:00:00",
  110. "isCollection": true,
  111. "pic": ""
  112. },
  113. {
  114. "id": 2,
  115. "title": "JHKFDSAJKjfkdsfjkasjkjklfajfkajifwoqirujweiqofjndsaikfniasdhfiasdhfiadshfifjadslfjkdlsafjlkadsj",
  116. "description": "oifosjf fjdskafj hjiwehfriohjfiash",
  117. "time": "2024-10-16 12:00:00",
  118. "isCollection": false,
  119. "pic": ""
  120. },
  121. ];
  122. if (state.curPage == 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.filterCount! * state.pageSize!){
  132. //更新展示的状态
  133. changeLoadingState(LoadState.State_Success, null);
  134. // refreshController.finishLoad(IndicatorResult.noMore);
  135. }else {
  136. allList.addAll(listData);
  137. state = state.copyWith(list: allList);
  138. }
  139. refreshController.finishLoad();
  140. }
  141. // 最后赋值
  142. _needShowPlaceholder = false;
  143. }
  144. // 去新闻详情页
  145. void goNewsDetail(String item) {
  146. Log.d("goNewsDetail");
  147. // PropertyPage.startInstance(context: context, item: item);
  148. }
  149. // 收藏/取消收藏
  150. void handlerCollection(curItem, bool isCollection){
  151. List<Map<String, dynamic>> newList = state.list.map((item) {
  152. if(item['id'] == curItem['id']){
  153. return {
  154. ...item,
  155. 'isCollection': !isCollection
  156. };
  157. }
  158. return item;
  159. }).toList();
  160. // Log.d("handlerCollection $newList");
  161. state = state.copyWith(list: newList);
  162. if(isCollection){
  163. ToastEngine.show("取消收藏");
  164. }else {
  165. ToastEngine.show("收藏成功");
  166. }
  167. }
  168. // 处理数据与展示的逻辑
  169. // void handleList(List<JobAppliedListSGRows>? list) {
  170. // if (list != null && list.isNotEmpty) {
  171. // //有数据,判断是刷新还是加载更多的数据
  172. // if (_curPage == 1) {
  173. // //刷新的方式
  174. // state.list.clear();
  175. // state.list.addAll(list);
  176. // refreshController.finishRefresh();
  177. //
  178. // //更新展示的状态
  179. // changeLoadingState(LoadState.State_Success);
  180. // } else {
  181. // //加载更多
  182. // state.list.addAll(list);
  183. // refreshController.finishLoad();
  184. // update();
  185. // }
  186. // } else {
  187. // if (_curPage == 1) {
  188. // //展示无数据的布局
  189. // state.list.clear();
  190. // changeLoadingState(LoadState.State_Empty);
  191. // refreshController.finishRefresh();
  192. // } else {
  193. // //展示加载完成,没有更多数据了
  194. // refreshController.finishLoad(IndicatorResult.noMore);
  195. // }
  196. // }
  197. // }
  198. }