news_vm.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  4. import 'package:riverpod_annotation/riverpod_annotation.dart';
  5. import 'package:router/ext/auto_router_extensions.dart';
  6. import 'package:shared/utils/log_utils.dart';
  7. import 'package:widgets/load_state_layout.dart';
  8. import 'package:widgets/widget_export.dart';
  9. import '../../../router/page/community_page_router.dart';
  10. import '../community_pageview_idx_data.dart';
  11. import '../community_vm.dart';
  12. import '../newsfeed_detail/newsfeed_detail_page.dart';
  13. import 'news_respository.dart';
  14. import 'news_state.dart';
  15. part 'news_vm.g.dart';
  16. @riverpod
  17. class NewsVm extends _$NewsVm {
  18. late NewsRepository NewsRepositoryInstance;
  19. bool _needShowPlaceholder = true; //是否展示LoadingView
  20. // Refresh 控制器
  21. final EasyRefreshController refreshController = EasyRefreshController(
  22. controlFinishRefresh: true, //允许刷新
  23. controlFinishLoad: true, //允许加载
  24. );
  25. NewsState initState() {
  26. return NewsState(
  27. list: []
  28. );
  29. }
  30. @override
  31. NewsState build(){
  32. // 引入数据仓库
  33. NewsRepositoryInstance = ref.read(newsRepositoryProvider);
  34. final state = initState();
  35. Log.d("--------------------------build---------------------");
  36. return state;
  37. }
  38. //刷新页面状态
  39. void changeLoadingState(LoadState loadState, String? errorMsg) {
  40. state = state.copyWith(
  41. loadingState: loadState,
  42. errorMessage: errorMsg
  43. );
  44. }
  45. // 初始化页面数据
  46. initPageData() {
  47. Log.d("----news_vm-----initPageData ${state.loadingState}");
  48. onRefresh();
  49. }
  50. // 上拉加载 更多
  51. Future loadMore() async {
  52. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
  53. if(isShowing){
  54. Log.d("----news_vm-----loadMore");
  55. // await Future.delayed(const Duration(seconds: 2));
  56. // if(state.list.length >= state.filterCount){
  57. // return;
  58. // }else {
  59. // int curPage = state.curPage + 1;
  60. // state = state.copyWith(curPage: curPage,);
  61. // getListData();
  62. // }
  63. // 检查 curPage 是否为 null,并初始化为 1
  64. int newCurPage = state.curPage ?? 1;
  65. state = state.copyWith(curPage: ++newCurPage);
  66. getListData();
  67. }else {
  68. refreshController.finishRefresh();
  69. }
  70. }
  71. // 下拉刷新
  72. Future onRefresh() async {
  73. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
  74. if(isShowing){
  75. // 当前pageView 页面正处于显示状态
  76. Log.d("----forsale_vm-----onRefresh ");
  77. // await Future.delayed(const Duration(seconds: 2));
  78. state = state.copyWith(curPage: 1);
  79. getListData();
  80. }else {
  81. refreshController.finishRefresh();
  82. // //更新展示的状态
  83. changeLoadingState(LoadState.State_Success, null);
  84. }
  85. }
  86. // 重试请求
  87. Future retryRequest() async {
  88. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
  89. if(isShowing){
  90. state = state.copyWith(curPage: 1);
  91. _needShowPlaceholder = true;
  92. getListData();
  93. }
  94. }
  95. // 获取list 列表数据
  96. Future getListData<T>() async {
  97. if (_needShowPlaceholder) {
  98. changeLoadingState(LoadState.State_Loading, null);
  99. }
  100. Log.d("for_sale加载listData数据---------------start--${state.curPage}---");
  101. // try {
  102. // //请求网络
  103. // Map<String, dynamic> params = {
  104. // "curPage": state.curPage,
  105. // "pageSize": state.pageSize,
  106. // };
  107. // Log.d("请求参数------$params");
  108. // final result = await propertyNewsRepository.fetchPropertyNewsList(params);
  109. // Log.d("请求完成结果------${result.data}");
  110. // //校验成功失败
  111. // if (result.isSuccess) {
  112. // // state = state.copyWith(serverTime: result.data);
  113. // state = state;
  114. // handleList(listResult.data?.rows);
  115. // ToastEngine.show("获取数据成功");
  116. // } else {
  117. // errorMessage = listResult.errorMsg;
  118. // changeLoadingState(LoadState.State_Error);
  119. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  120. // }
  121. // } catch (e) {
  122. // ToastEngine.show("Error: $e");
  123. // }
  124. await Future.delayed(const Duration(milliseconds: 1500));
  125. final List<Map<String, dynamic>> listData = [
  126. {
  127. 'id':1,
  128. 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  129. 'title': 'William Jefferson',
  130. 'isFollow': false,
  131. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  132. 'imageUrls': ['https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500','https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500','https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500'],
  133. 'time': 'June 17,2016 at 7:23 p.m.',
  134. 'isLike': true,
  135. 'likeno': 12
  136. },
  137. {
  138. 'id':2,
  139. 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  140. 'title': 'William fdsaf的飞洒发生的',
  141. 'isFollow': true,
  142. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  143. 'imageUrls': [],
  144. 'time': 'June 17,2016 at 7:23 p.m.',
  145. 'isLike': true,
  146. 'likeno': 12
  147. },
  148. {
  149. 'id':3,
  150. 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  151. 'title': 'Fsjfkds dfsk',
  152. 'isFollow': false,
  153. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  154. 'imageUrls': [],
  155. 'time': 'June 17,2016 at 7:23 p.m.',
  156. 'isLike': false,
  157. 'likeno': 12
  158. },
  159. {
  160. 'id':4,
  161. 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  162. 'title': 'Fsjfkds dfsk',
  163. 'isFollow': false,
  164. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  165. 'imageUrls': [],
  166. 'time': 'June 17,2016 at 7:23 p.m.',
  167. 'isLike': false,
  168. 'likeno': 12
  169. },
  170. {
  171. 'id':5,
  172. 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  173. 'title': 'Fsjfkds dfsk',
  174. 'isFollow': false,
  175. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  176. 'imageUrls': [],
  177. 'time': 'June 17,2016 at 7:23 p.m.',
  178. 'isLike': false,
  179. 'likeno': 12
  180. },
  181. {
  182. 'id':6,
  183. 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  184. 'title': 'Fsjfkds dfsk',
  185. 'isFollow': false,
  186. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  187. 'imageUrls': [],
  188. 'time': 'June 17,2016 at 7:23 p.m.',
  189. 'isLike': false,
  190. 'likeno': 12
  191. },
  192. {
  193. 'id':7,
  194. 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  195. 'title': '放大发大水',
  196. 'isFollow': false,
  197. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  198. 'imageUrls': [],
  199. 'time': 'June 17,2016 at 7:23 p.m.',
  200. 'isLike': false,
  201. 'likeno': 12
  202. },
  203. ];
  204. if (state.curPage == 1) {
  205. //刷新的方式
  206. state = state.copyWith(list: listData);
  207. refreshController.finishRefresh();
  208. // //更新展示的状态
  209. changeLoadingState(LoadState.State_Success, null);
  210. } else {
  211. //加载更多
  212. final allList = state.list;
  213. allList?.addAll(listData);
  214. state = state.copyWith(list: allList);
  215. refreshController.finishLoad();
  216. }
  217. // 最后赋值
  218. _needShowPlaceholder = false;
  219. }
  220. // 点击 like comments share
  221. void handlerClickActionBtn(String? actionStr, item){
  222. final id = item['id'];
  223. switch (actionStr) {
  224. case 'like':
  225. Log.d("点击了 点赞");
  226. handlerGotoDetail(id);
  227. break;
  228. case 'comments':
  229. Log.d("点击了 评论");
  230. handlerGotoDetail(id);
  231. break;
  232. case 'share':
  233. Log.d("点击了 分享");
  234. handlerGotoDetail(id);
  235. break;
  236. default:
  237. Log.d("点击了卡片");
  238. handlerGotoDetail(id);
  239. break;
  240. }
  241. }
  242. // 去详情页面
  243. void handlerGotoDetail(id){
  244. Log.d("去详情页面");
  245. appRouter.push(NewsfeedDetailPageRoute(id: id, type:'news'));
  246. }
  247. }