property_news_vm.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. allList.addAll(listData);
  132. refreshController.finishLoad();
  133. state = state.copyWith(list: allList);
  134. }
  135. // 最后赋值
  136. _needShowPlaceholder = false;
  137. }
  138. // 去新闻详情页
  139. void goNewsDetail(String item) {
  140. Log.d("goNewsDetail");
  141. // PropertyPage.startInstance(context: context, item: item);
  142. }
  143. // 收藏/取消收藏
  144. void handlerCollection(curItem, bool isCollection){
  145. List<Map<String, dynamic>> newList = state.list.map((item) {
  146. if(item['id'] == curItem['id']){
  147. return {
  148. ...item,
  149. 'isCollection': !isCollection
  150. };
  151. }
  152. return item;
  153. }).toList();
  154. // Log.d("handlerCollection $newList");
  155. state = state.copyWith(list: newList);
  156. if(isCollection){
  157. ToastEngine.show("取消收藏");
  158. }else {
  159. ToastEngine.show("收藏成功");
  160. }
  161. }
  162. // 处理数据与展示的逻辑
  163. // void handleList(List<JobAppliedListSGRows>? list) {
  164. // if (list != null && list.isNotEmpty) {
  165. // //有数据,判断是刷新还是加载更多的数据
  166. // if (_curPage == 1) {
  167. // //刷新的方式
  168. // state.list.clear();
  169. // state.list.addAll(list);
  170. // refreshController.finishRefresh();
  171. //
  172. // //更新展示的状态
  173. // changeLoadingState(LoadState.State_Success);
  174. // } else {
  175. // //加载更多
  176. // state.list.addAll(list);
  177. // refreshController.finishLoad();
  178. // update();
  179. // }
  180. // } else {
  181. // if (_curPage == 1) {
  182. // //展示无数据的布局
  183. // state.list.clear();
  184. // changeLoadingState(LoadState.State_Empty);
  185. // refreshController.finishRefresh();
  186. // } else {
  187. // //展示加载完成,没有更多数据了
  188. // refreshController.finishLoad(IndicatorResult.noMore);
  189. // }
  190. // }
  191. // }
  192. }