news_vm.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 '../../../router/page/community_page_router.dart';
  8. import '../community_vm.dart';
  9. import '../newsfeed_detail/newsfeed_detail_page.dart';
  10. import 'news_respository.dart';
  11. import 'news_state.dart';
  12. part 'news_vm.g.dart';
  13. @riverpod
  14. class NewsVm extends _$NewsVm {
  15. late NewsRepository NewsRepositoryInstance;
  16. NewsState initState() {
  17. return NewsState(
  18. list: [
  19. {
  20. 'id':1,
  21. 'avator': Assets.communityCamera,
  22. 'title': 'William Jefferson',
  23. 'isFollow': false,
  24. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  25. 'imageUrls': ['https://img.alicdn.com/tfs/TB1h.o9O4MPMeJjy1XaXXcSsFXa-640-360.jpg','https://img.alicdn.com/tfs/TB1h.o9O4MPMeJjy1XaXXcSsFXa-640-360.jpg','https://img.alicdn.com/tfs/TB1h.o9O4MPMeJjy1XaXXcSsFXa-640-360.jpg'],
  26. 'time': 'June 17,2016 at 7:23 p.m.',
  27. 'isLike': true,
  28. 'likeno': 12
  29. },
  30. {
  31. 'id':2,
  32. 'avator': Assets.communityCamera,
  33. 'title': 'William fdsaf的飞洒发生的',
  34. 'isFollow': true,
  35. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  36. 'imageUrls': [],
  37. 'time': 'June 17,2016 at 7:23 p.m.',
  38. 'isLike': true,
  39. 'likeno': 12
  40. },
  41. {
  42. 'id':3,
  43. 'avator': Assets.communityCamera,
  44. 'title': 'Fsjfkds dfsk',
  45. 'isFollow': false,
  46. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  47. 'imageUrls': [],
  48. 'time': 'June 17,2016 at 7:23 p.m.',
  49. 'isLike': false,
  50. 'likeno': 12
  51. },
  52. {
  53. 'id':4,
  54. 'avator': Assets.communityCamera,
  55. 'title': 'Fsjfkds dfsk',
  56. 'isFollow': false,
  57. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  58. 'imageUrls': [],
  59. 'time': 'June 17,2016 at 7:23 p.m.',
  60. 'isLike': false,
  61. 'likeno': 12
  62. },
  63. {
  64. 'id':5,
  65. 'avator': Assets.communityCamera,
  66. 'title': 'Fsjfkds dfsk',
  67. 'isFollow': false,
  68. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  69. 'imageUrls': [],
  70. 'time': 'June 17,2016 at 7:23 p.m.',
  71. 'isLike': false,
  72. 'likeno': 12
  73. },
  74. {
  75. 'id':6,
  76. 'avator': Assets.communityCamera,
  77. 'title': 'Fsjfkds dfsk',
  78. 'isFollow': false,
  79. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  80. 'imageUrls': [],
  81. 'time': 'June 17,2016 at 7:23 p.m.',
  82. 'isLike': false,
  83. 'likeno': 12
  84. },
  85. {
  86. 'id':7,
  87. 'avator': Assets.communityCamera,
  88. 'title': '放大发大水',
  89. 'isFollow': false,
  90. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  91. 'imageUrls': [],
  92. 'time': 'June 17,2016 at 7:23 p.m.',
  93. 'isLike': false,
  94. 'likeno': 12
  95. },
  96. ]
  97. );
  98. }
  99. @override
  100. NewsState build(){
  101. // 引入数据仓库
  102. NewsRepositoryInstance = ref.read(newsRepositoryProvider);
  103. final state = initState();
  104. Log.d("--------------------------build---------------------");
  105. return state;
  106. }
  107. // 设置当前的 tabsRouter 和 pageController
  108. Future setTabsRouterAndPageController(dynamic tabsRouter, dynamic pageController) async{
  109. Log.d("setTabsRouterAndPageController---:$tabsRouter");
  110. }
  111. // 上拉加载
  112. Future onLoadData() async {
  113. Log.d("----News_vm-----initListData");
  114. // await Future.delayed(const Duration(seconds: 2));
  115. // if(state.list.length >= state.filterCount){
  116. // return;
  117. // }else {
  118. // int curPage = state.curPage + 1;
  119. // state = state.copyWith(curPage: curPage,);
  120. // getListData();
  121. // }
  122. // getListData();
  123. }
  124. // 获取list 列表数据
  125. void getListData<T>() async {
  126. Log.d("加载listData数据---------------start-----");
  127. try {
  128. //请求网络
  129. Map<String, dynamic> params = {
  130. "curPage": state.curPage,
  131. "pageSize": state.pageSize,
  132. };
  133. Log.d("请求参数------$params");
  134. final result = await NewsRepositoryInstance.fetchNewsList(params);
  135. Log.d("请求完成结果------${result.data}");
  136. //校验成功失败
  137. if (result.isSuccess) {
  138. // state = state.copyWith(serverTime: result.data);
  139. state = state;
  140. ToastEngine.show("获取数据成功");
  141. } else {
  142. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  143. }
  144. } catch (e) {
  145. ToastEngine.show("Error: $e");
  146. }
  147. }
  148. // 下拉刷新
  149. Future refreshListData() async {
  150. Log.d("----property_news_vm-----refreshListData ");
  151. // await Future.delayed(const Duration(seconds: 2));
  152. state = state.copyWith(curPage: 1, pageSize: 10);
  153. // ref.invalidateSelf();
  154. // ref.invalidate(NewsVmProvider);
  155. getListData();
  156. }
  157. // 点击 like comments share
  158. void handlerClickActionBtn(String? actionStr, item){
  159. final id = item['id'];
  160. switch (actionStr) {
  161. case 'like':
  162. Log.d("点击了 点赞");
  163. handlerGotoDetail(id);
  164. break;
  165. case 'comments':
  166. Log.d("点击了 评论");
  167. handlerGotoDetail(id);
  168. break;
  169. case 'share':
  170. Log.d("点击了 分享");
  171. handlerGotoDetail(id);
  172. break;
  173. default:
  174. Log.d("点击了卡片");
  175. handlerGotoDetail(id);
  176. break;
  177. }
  178. }
  179. // 去详情页面
  180. void handlerGotoDetail(id){
  181. Log.d("去详情页面");
  182. appRouter.push(NewsfeedDetailPageRoute(id: id, type:'news'));
  183. }
  184. }