property_news_vm.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import 'package:auto_route/src/route/page_route_info.dart';
  2. import 'package:cpt_property/modules/ioan/property_ioan_page.dart';
  3. import 'package:cpt_property/respository/property_resposity.dart';
  4. import 'package:cpt_property/router/page/property_page_router.dart';
  5. import 'package:domain/entity/property_news_entity.dart';
  6. import 'package:flutter/cupertino.dart';
  7. import 'package:plugin_platform/http/http_result.dart';
  8. import 'package:riverpod_annotation/riverpod_annotation.dart';
  9. import 'package:router/ext/auto_router_extensions.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  12. import 'package:widgets/load_state_layout.dart';
  13. import 'package:widgets/widget_export.dart';
  14. import '../../news_detail/property_news_detail_page.dart';
  15. import '../../property/page/property_page.dart';
  16. import '../page/property_news_state.dart';
  17. part 'property_news_vm.g.dart';
  18. @riverpod
  19. class PropertyNewsVm extends _$PropertyNewsVm {
  20. late PropertyRespository propertyRespositoryInstance;
  21. bool _needShowPlaceholder = false; //是否展示LoadingView
  22. int _page = 1; // 当前页
  23. int _limit = 10; // 每页数量
  24. int _count = 0; // 总条数
  25. Map<String, dynamic> _newsQueryMap = {
  26. "isCollection": false,
  27. };
  28. // Refresh 控制器
  29. final EasyRefreshController refreshController = EasyRefreshController(
  30. controlFinishRefresh: true, //允许刷新
  31. controlFinishLoad: true, //允许加载
  32. );
  33. PropertyNewsState initState() {
  34. return PropertyNewsState(
  35. list: [],
  36. );
  37. }
  38. @override
  39. PropertyNewsState build() {
  40. // 引入数据仓库
  41. propertyRespositoryInstance = ref.read(propertyRespositoryProvider);
  42. // 初始化状态
  43. PropertyNewsState state = initState();
  44. // 初始化列表数据
  45. return state;
  46. }
  47. //刷新页面状态
  48. void changeLoadingState(LoadState loadState, String? errorMsg) {
  49. state = state.copyWith(
  50. loadingState: loadState,
  51. errorMessage: errorMsg
  52. );
  53. }
  54. // 初始化页面数据
  55. initPageData() {
  56. Log.d("----property_news_vm-----initPageData");
  57. onRefresh();
  58. }
  59. // 上拉加载 更多
  60. Future loadMore() async {
  61. Log.d("----property_news_vm-----loadMore");
  62. // await Future.delayed(const Duration(seconds: 2));
  63. // if(state.list.length >= _count){
  64. // return;
  65. // }else {
  66. // int page = _page + 1;
  67. // state = state.copyWith(page: page,);
  68. // getListData();
  69. // }
  70. // 检查 page 是否为 null,并初始化为 1
  71. _page++;
  72. getListData();
  73. }
  74. // 下拉刷新
  75. Future onRefresh() async {
  76. Log.d("----property_news_vm-----onRefresh ");
  77. // await Future.delayed(const Duration(seconds: 2));
  78. _page = 1;
  79. getListData();
  80. }
  81. // 重试请求
  82. Future retryRequest() async {
  83. _page = 1;
  84. _needShowPlaceholder = true;
  85. getListData();
  86. }
  87. // 获取list 列表数据
  88. Future getListData<T>() async {
  89. if (_needShowPlaceholder) {
  90. changeLoadingState(LoadState.State_Loading, null);
  91. }
  92. Log.d("加载listData数据---------------start--${_page}---");
  93. try {
  94. //请求网络
  95. Map<String, dynamic> params = {
  96. "isCollection": _newsQueryMap['isCollection'],
  97. "page": _page,
  98. "limit": _limit,
  99. };
  100. Log.d("请求参数------$params");
  101. final result = await propertyRespositoryInstance.fetchNewsList(params);
  102. //校验成功失败
  103. if (result.isSuccess) {
  104. handlerResultList((result.data as PropertyNewsEntity).list);
  105. } else {
  106. String errorMessage = result.errorMsg!;
  107. changeLoadingState(LoadState.State_Error, errorMessage);
  108. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  109. }
  110. } catch (e) {
  111. ToastEngine.show("Error: $e");
  112. }
  113. // await Future.delayed(const Duration(milliseconds: 1500));
  114. // final List<Map<String, dynamic>> listData = [
  115. // {
  116. // "id": 1,
  117. // "title": "法国进口高档国家的看过艰苦奋斗是感觉扣税的房价过快岁的法国艰苦奋斗是架构开发更加开放的国际法的是",
  118. // "description": "fsklfdsk罚款乱收费上课了发送卡fsklfdsk罚款乱收费上课了发送卡fsklfdsk罚款乱收费上课了发送卡fsklfdsk罚款乱收费上课了发送卡fsklfdsk罚款乱收费上课了发送卡",
  119. // "time": "2024-02-15 12:00:00",
  120. // "isCollection": true,
  121. // "pic": "https://img1.baidu.com/it/u=2500825327,873404587&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500"
  122. // },
  123. // {
  124. // "id": 2,
  125. // "title": "JHKFDSAJKjfkdsfjkasjkjklfajfkajifwoqirujweiqofjndsaikfniasdhfiasdhfiadshfifjadslfjkdlsafjlkadsj",
  126. // "description": "oifosjf fjdskafj hjiwehfriohjfiash",
  127. // "time": "2024-10-16 12:00:00",
  128. // "isCollection": false,
  129. // "pic": ""
  130. // },
  131. // ];
  132. //
  133. // if (_page == 1) {
  134. // //刷新的方式
  135. // state = state.copyWith(list: listData);
  136. // refreshController.finishRefresh();
  137. // //更新展示的状态
  138. // changeLoadingState(LoadState.State_Success, null);
  139. // } else {
  140. // //加载更多
  141. // final allList = state.list;
  142. // if(allList.length >= _count! * _limit!){
  143. // //更新展示的状态
  144. // changeLoadingState(LoadState.State_Success, null);
  145. // refreshController.finishLoad(IndicatorResult.noMore);
  146. // }else {
  147. // allList.addAll(listData);
  148. // state = state.copyWith(list: allList);
  149. // refreshController.finishLoad();
  150. // }
  151. // }
  152. // // 最后赋值
  153. // _needShowPlaceholder = false;
  154. }
  155. void handlerResultList(List<PropertyNewsList>? list) {
  156. if (list != null && list.isNotEmpty) {
  157. //有数据,判断是刷新还是加载更多的数据
  158. if (_page == 1) {
  159. //刷新的方式
  160. state.list!.clear();
  161. state.list!.addAll(list.map((item)=> item.toJson()).toList());
  162. refreshController.finishRefresh();
  163. //更新展示的状态
  164. changeLoadingState(LoadState.State_Success, null);
  165. } else {
  166. //加载更多
  167. final allList = state.list;
  168. state = state.copyWith(list: allList);
  169. refreshController.finishLoad();
  170. // update();
  171. }
  172. } else {
  173. if (_page == 1) {
  174. //展示无数据的布局
  175. state.list!.clear();
  176. changeLoadingState(LoadState.State_Empty, null);
  177. refreshController.finishRefresh();
  178. } else {
  179. //展示加载完成,没有更多数据了
  180. if(state.list!.length == 0){
  181. changeLoadingState(LoadState.State_Empty, null);
  182. refreshController.finishLoad();
  183. }else {
  184. if(_needShowPlaceholder){
  185. changeLoadingState(LoadState.State_Success, null);
  186. }
  187. }
  188. //更新展示的状态
  189. refreshController.finishLoad(IndicatorResult.noMore);
  190. }
  191. }
  192. }
  193. // 去新闻详情页
  194. void goNewsDetailPage(BuildContext? context, int id) {
  195. Log.d("goNewsDetail");
  196. PropertyNewsDetailPage.startInstance(context: context, id: id,);
  197. }
  198. // 收藏/取消收藏
  199. void handlerCollection(curItem, bool liked) async {
  200. try {
  201. Map<String, dynamic> params = {
  202. "id": curItem['id'],
  203. };
  204. final result = await propertyRespositoryInstance.fetchPropertyNewsLikeClick(params);
  205. //校验成功失败
  206. if (result.isSuccess) {
  207. List<Map<String, dynamic>>? newList = state.list?.map((item) {
  208. if(item['id'] == curItem['id']){
  209. return {
  210. ...item,
  211. 'liked': !liked
  212. };
  213. }
  214. return item;
  215. }).toList();
  216. // Log.d("handlerCollection $newList");
  217. state = state.copyWith(list: newList);
  218. if(liked){
  219. ToastEngine.show("取消收藏");
  220. }else {
  221. ToastEngine.show("收藏成功");
  222. }
  223. } else {
  224. String errorMessage = result.errorMsg!;
  225. ToastEngine.show(errorMessage ?? "Network Load Error");
  226. }
  227. } catch (e) {
  228. ToastEngine.show("Error: $e");
  229. }
  230. }
  231. changeQueryMap(Map<String, dynamic> queryMap){
  232. _newsQueryMap = { ..._newsQueryMap, ...queryMap};
  233. }
  234. }