following_vm.dart 10 KB

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