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