property_news_vm.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. }
  25. @override
  26. PropertyNewsState build() {
  27. // 引入数据仓库
  28. propertyNewsRepository = ref.read(propertyNewsRepositoryProvider);
  29. // 初始化状态
  30. PropertyNewsState state = initState();
  31. // 初始化列表数据
  32. return state;
  33. }
  34. //刷新页面状态
  35. void changeLoadingState(LoadState loadState, String? errorMsg) {
  36. state = state.copyWith(
  37. loadingState: loadState,
  38. errorMessage: errorMsg
  39. );
  40. }
  41. // 初始化页面数据
  42. initPageData() {
  43. Log.d("----property_news_vm-----initPageData");
  44. refreshListData();
  45. }
  46. // 上拉加载
  47. Future onLoadData() async {
  48. Log.d("----property_news_vm-----initListData");
  49. // await Future.delayed(const Duration(seconds: 2));
  50. // if(state.list.length >= state.filterCount){
  51. // return;
  52. // }else {
  53. // int curPage = state.curPage + 1;
  54. // state = state.copyWith(curPage: curPage,);
  55. // getListData();
  56. // }
  57. // 检查 curPage 是否为 null,并初始化为 1
  58. int newCurPage = state.curPage ?? 1;
  59. state = state.copyWith(curPage: ++newCurPage);
  60. getListData();
  61. }
  62. // 下拉刷新
  63. Future refreshListData() async {
  64. Log.d("----property_news_vm-----refreshListData ");
  65. // await Future.delayed(const Duration(seconds: 2));
  66. state = state.copyWith(curPage: 1);
  67. getListData();
  68. }
  69. // 重试请求
  70. Future retryRequest() async {
  71. state = state.copyWith(curPage: 1);
  72. _needShowPlaceholder = true;
  73. getListData();
  74. }
  75. // 获取list 列表数据
  76. void getListData<T>() async {
  77. if (_needShowPlaceholder) {
  78. changeLoadingState(LoadState.State_Loading, null);
  79. }
  80. Log.d("加载listData数据---------------start-----");
  81. // try {
  82. // //请求网络
  83. // Map<String, dynamic> params = {
  84. // "curPage": state.curPage,
  85. // "pageSize": state.pageSize,
  86. // };
  87. // Log.d("请求参数------$params");
  88. // final result = await propertyNewsRepository.fetchPropertyNewsList(params);
  89. // Log.d("请求完成结果------${result.data}");
  90. // //校验成功失败
  91. // if (result.isSuccess) {
  92. // // state = state.copyWith(serverTime: result.data);
  93. // state = state;
  94. // ToastEngine.show("获取数据成功");
  95. // } else {
  96. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  97. // }
  98. // } catch (e) {
  99. // ToastEngine.show("Error: $e");
  100. // }
  101. await Future.delayed(const Duration(milliseconds: 1500));
  102. final List<Map<String, dynamic>> listData = [
  103. {
  104. "id": 1,
  105. "title": "fkladsfk fldask fldsakfllfkaslsd",
  106. "description": "fsklfdsk罚款乱收费上课了发送卡",
  107. "time": "2024-02-15 12:00:00",
  108. "isCollection": true,
  109. "pic": ""
  110. },
  111. {
  112. "id": 2,
  113. "title": "JHKFDSAJKjfkdsfjkasjkjklfajfkajifwoqirujweiqofjndsaikfniasdhfiasdhfiadshfifjadslfjkdlsafjlkadsj",
  114. "description": "oifosjf fjdskafj hjiwehfriohjfiash",
  115. "time": "2024-10-16 12:00:00",
  116. "isCollection": false,
  117. "pic": ""
  118. },
  119. ];
  120. if (state.curPage == 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. allList.addAll(listData);
  130. state.list.addAll(listData);
  131. refreshController.finishLoad();
  132. state = state.copyWith(list: allList);
  133. }
  134. // 最后赋值
  135. _needShowPlaceholder = false;
  136. }
  137. // 去新闻详情页
  138. void goNewsDetail(String item) {
  139. Log.d("goNewsDetail");
  140. // PropertyPage.startInstance(context: context, item: item);
  141. }
  142. // 收藏/取消收藏
  143. void handlerCollection(curItem, bool isCollection){
  144. List<Map<String, dynamic>> newList = state.list.map((item) {
  145. if(item['id'] == curItem['id']){
  146. return {
  147. ...item,
  148. 'isCollection': !isCollection
  149. };
  150. }
  151. return item;
  152. }).toList();
  153. // Log.d("handlerCollection $newList");
  154. state = state.copyWith(list: newList);
  155. if(isCollection){
  156. ToastEngine.show("取消收藏");
  157. }else {
  158. ToastEngine.show("收藏成功");
  159. }
  160. }
  161. }