news_vm.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import 'package:cs_resources/generated/assets.dart';
  2. import 'package:domain/entity/newsfeed_news_entity.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  5. import 'package:riverpod_annotation/riverpod_annotation.dart';
  6. import 'package:router/ext/auto_router_extensions.dart';
  7. import 'package:shared/utils/ext_dart.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:widgets/load_state_layout.dart';
  10. import 'package:widgets/widget_export.dart';
  11. import '../../../respository/newsfeed_news_repository.dart';
  12. import '../../../router/page/community_page_router.dart';
  13. import '../community_pageview_idx_data.dart';
  14. import '../community_vm.dart';
  15. import '../newsfeed_detail/newsfeed_detail_page.dart';
  16. import 'news_state.dart';
  17. part 'news_vm.g.dart';
  18. @riverpod
  19. class NewsVm extends _$NewsVm {
  20. late NewsFeedNewsRepository repositoryInstance;
  21. bool _needShowPlaceholder = false; //是否展示LoadingView
  22. // Refresh 控制器
  23. final EasyRefreshController refreshController = EasyRefreshController(
  24. controlFinishRefresh: true, //允许刷新
  25. controlFinishLoad: true, //允许加载
  26. );
  27. NewsState initState() {
  28. return NewsState(
  29. list: []
  30. );
  31. }
  32. @override
  33. NewsState build(){
  34. // 引入数据仓库
  35. repositoryInstance = ref.read(newsFeedNewsRepositoryProvider);
  36. final state = initState();
  37. Log.d("--------------------------build---------------------");
  38. return state;
  39. }
  40. //刷新页面状态
  41. void changeLoadingState(LoadState loadState, String? errorMsg) {
  42. state = state.copyWith(
  43. loadingState: loadState,
  44. errorMessage: errorMsg
  45. );
  46. }
  47. // 初始化页面数据
  48. initPageData() {
  49. Log.d("----news_vm-----initPageData ${state.loadingState}");
  50. onRefresh();
  51. }
  52. // 上拉加载 更多
  53. Future loadMore() async {
  54. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
  55. if(isShowing){
  56. Log.d("----news_vm-----loadMore");
  57. // await Future.delayed(const Duration(seconds: 2));
  58. // if(state.list.length >= state.count){
  59. // return;
  60. // }else {
  61. // int page = state.page + 1;
  62. // state = state.copyWith(page: page,);
  63. // getListData();
  64. // }
  65. // 检查 page 是否为 null,并初始化为 1
  66. int newCurPage = state.page ?? 1;
  67. state = state.copyWith(page: ++newCurPage);
  68. getListData();
  69. }else {
  70. refreshController.finishRefresh();
  71. }
  72. }
  73. // 下拉刷新
  74. Future onRefresh() async {
  75. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
  76. if(isShowing){
  77. // 当前pageView 页面正处于显示状态
  78. Log.d("----forsale_vm-----onRefresh ");
  79. // await Future.delayed(const Duration(seconds: 2));
  80. state = state.copyWith(page: 1);
  81. getListData();
  82. }else {
  83. refreshController.finishRefresh();
  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(page: 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("加载listData数据---------------start--${state.page}---");
  101. // try {
  102. // //请求网络
  103. // Map<String, dynamic> params = {
  104. // "page": state.page,
  105. // "limit": state.limit,
  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. //
  205. // if (state.page == 1) {
  206. // //刷新的方式
  207. // state = state.copyWith(list: listData);
  208. // refreshController.finishRefresh();
  209. // // //更新展示的状态
  210. // changeLoadingState(LoadState.State_Success, null);
  211. // } else {
  212. // //加载更多
  213. // final allList = state.list;
  214. // allList?.addAll(listData);
  215. // state = state.copyWith(list: allList);
  216. // refreshController.finishLoad();
  217. //
  218. // }
  219. //
  220. // // 最后赋值
  221. // _needShowPlaceholder = false;
  222. try {
  223. //请求网络
  224. Map<String, dynamic> params = {
  225. "page": state.page,
  226. "limit": state.limit,
  227. };
  228. Log.d("请求参数------$params");
  229. final result = await repositoryInstance.fetchNewsList(params);
  230. //校验成功失败
  231. if (result.isSuccess) {
  232. handlerResultList((result.data as NewsfeedNewsEntity).list);
  233. } else {
  234. String errorMessage = result.errorMsg!;
  235. changeLoadingState(LoadState.State_Error, errorMessage);
  236. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  237. }
  238. } catch (e) {
  239. ToastEngine.show("Error: $e");
  240. }
  241. }
  242. void handlerResultList(List<NewsfeedNewsList>? list) {
  243. if (list != null && list.isNotEmpty) {
  244. //有数据,判断是刷新还是加载更多的数据
  245. if (state.page == 1) {
  246. //刷新的方式
  247. state.list!.clear();
  248. state.list!.addAll(list.map((item) => item.toJson()).toList());
  249. refreshController.finishRefresh();
  250. //更新展示的状态
  251. changeLoadingState(LoadState.State_Success, null);
  252. } else {
  253. //加载更多
  254. final allList = state.list;
  255. state = state.copyWith(list: allList);
  256. refreshController.finishLoad();
  257. }
  258. } else {
  259. if (state.page == 1) {
  260. //展示无数据的布局
  261. state.list!.clear();
  262. changeLoadingState(LoadState.State_Empty, null);
  263. refreshController.finishRefresh();
  264. } else {
  265. //展示加载完成,没有更多数据了
  266. if(state.list!.length == 0){
  267. changeLoadingState(LoadState.State_Empty, null);
  268. }else {
  269. if(_needShowPlaceholder){
  270. changeLoadingState(LoadState.State_Success, null);
  271. }
  272. }
  273. refreshController.finishLoad(IndicatorResult.noMore);
  274. }
  275. }
  276. }
  277. // 点赞/取消点赞
  278. Future handlerLikeClick(int id, bool isLike, int itemidx) async {
  279. try {
  280. final result = await repositoryInstance.fetchLikeClick({
  281. "id": id,
  282. });
  283. if (result.isSuccess) {
  284. //重新赋值data或list
  285. // final json = result.getDataJson();
  286. // var data = NewsfeedNewsEntity.fromJson(json!);
  287. //重新赋值data或list
  288. // state.list![id].liked = data.list![0].liked;
  289. // state.list![id].likeno = data.list![0].likeno;
  290. final String toastMsg = isLike ? "Cancel successfully": "Liked successfully";
  291. ToastEngine.show(toastMsg);
  292. // 修改 state.list[itemIdx] 中的 like 状态 和 likes_count
  293. state.list![itemidx]['liked'] = !isLike;
  294. if(isLike){
  295. // 取消点赞
  296. if(state.list![itemidx]['likes_count']>0){
  297. state.list![itemidx]['likes_count'] = state.list![itemidx]['likes_count'] - 1;
  298. }
  299. }else {
  300. state.list![itemidx]['likes_count'] = state.list![itemidx]['likes_count'] + 1;
  301. }
  302. //重新赋值data或list
  303. changeLoadingState(LoadState.State_Success, null);
  304. return true;
  305. }else {
  306. return false;
  307. }
  308. }catch(error) {
  309. return false;
  310. }
  311. }
  312. // 点击 like comments share
  313. Future<bool?> handlerClickActionBtn(String? actionStr, Map<String, dynamic> item, int itemidx) async{
  314. final id = item['id'];
  315. final liked = item.getValue('liked', false);
  316. switch (actionStr) {
  317. case 'like':
  318. return await handlerLikeClick(id, liked, itemidx);
  319. case 'comments':
  320. Log.d("点击了 评论");
  321. handlerGotoDetail(null, id);
  322. break;
  323. case 'share':
  324. Log.d("点击了 分享");
  325. handlerGotoDetail(null, id);
  326. break;
  327. default:
  328. Log.d("点击了卡片");
  329. handlerGotoDetail(null, id);
  330. break;
  331. }
  332. }
  333. Future handlerFollow(BuildContext context, bool isFollow) async{
  334. Log.d("点击了 关注");
  335. // try {
  336. // //请求网络
  337. // Map<String, dynamic> params = {
  338. //
  339. // };
  340. // Log.d("请求参数------$params");
  341. // final result = await repositoryInstance.fetchNewsList(params);
  342. // //校验成功失败
  343. // if (result.isSuccess) {
  344. // handlerResultList((result.data as NewsfeedNewsEntity).list);
  345. // } else {
  346. // String errorMessage = result.errorMsg!;
  347. // changeLoadingState(LoadState.State_Error, errorMessage);
  348. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  349. // }
  350. // } catch (e) {
  351. // ToastEngine.show("Error: $e");
  352. // }
  353. }
  354. // 去详情页面
  355. void handlerGotoDetail(BuildContext? context, int id){
  356. Log.d("去详情页面");
  357. appRouter.push(NewsfeedDetailPageRoute(id: id, type:'news'));
  358. }
  359. }