newsfeed_vm.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import 'package:cpt_community/modules/newsfeed/newsfeed_post/newsfeed_post_page.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  5. import 'package:riverpod_annotation/riverpod_annotation.dart';
  6. import 'package:router/componentRouter/component_service_manager.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:router/path/router_path.dart';
  9. import 'package:shared/utils/color_utils.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. import 'package:auto_route/auto_route.dart';
  12. import '../../router/page/community_page_router.dart';
  13. import 'newsfeed_detail/newsfeed_detail_page.dart';
  14. import 'newsfeed_state.dart';
  15. import 'newsfeed_repository.dart';
  16. part 'newsfeed_vm.g.dart';
  17. @riverpod
  18. class NewsfeedVm extends _$NewsfeedVm {
  19. late NewsfeedRepository newsfeedRepository;
  20. NewsfeedState initState() {
  21. return NewsfeedState(
  22. useTag: 0, // 当前 大类目的 标签 (News Feed / Garage Sale)
  23. activeIndex: 0, // 当前tab 索引
  24. tabsList: [
  25. {
  26. 'title': 'News',
  27. 'icon': null,
  28. 'active': true,
  29. 'activeTitleColor': Colors.white,
  30. 'activeTitleFontSize': 16,
  31. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  32. },
  33. {
  34. 'title': 'Following',
  35. 'icon': null,
  36. 'active': false,
  37. 'activeTitleColor': Colors.white,
  38. 'activeTitleFontSize': 16,
  39. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  40. },
  41. {
  42. 'title': 'For You',
  43. 'icon': null,
  44. 'activeTitleColor': Colors.white,
  45. 'activeTitleFontSize': 16,
  46. 'activeTitleBackgroundColor': ColorUtils.string2Color("#4161D0"),
  47. }
  48. ],
  49. list: [
  50. {
  51. 'id':1,
  52. 'avator': Assets.communityCamera,
  53. 'title': 'William Jefferson',
  54. 'isFollow': false,
  55. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  56. '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'],
  57. 'time': 'June 17,2016 at 7:23 p.m.',
  58. 'isLike': true,
  59. 'likeno': 12
  60. },
  61. {
  62. 'id':2,
  63. 'avator': Assets.communityCamera,
  64. 'title': 'William fdsaf的飞洒发生的',
  65. 'isFollow': true,
  66. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  67. 'imageUrls': [],
  68. 'time': 'June 17,2016 at 7:23 p.m.',
  69. 'isLike': true,
  70. 'likeno': 12
  71. },
  72. {
  73. 'id':3,
  74. 'avator': Assets.communityCamera,
  75. 'title': 'Fsjfkds dfsk',
  76. 'isFollow': false,
  77. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  78. 'imageUrls': [],
  79. 'time': 'June 17,2016 at 7:23 p.m.',
  80. 'isLike': false,
  81. 'likeno': 12
  82. },
  83. {
  84. 'id':4,
  85. 'avator': Assets.communityCamera,
  86. 'title': 'Fsjfkds dfsk',
  87. 'isFollow': false,
  88. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  89. 'imageUrls': [],
  90. 'time': 'June 17,2016 at 7:23 p.m.',
  91. 'isLike': false,
  92. 'likeno': 12
  93. },
  94. {
  95. 'id':5,
  96. 'avator': Assets.communityCamera,
  97. 'title': 'Fsjfkds dfsk',
  98. 'isFollow': false,
  99. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  100. 'imageUrls': [],
  101. 'time': 'June 17,2016 at 7:23 p.m.',
  102. 'isLike': false,
  103. 'likeno': 12
  104. },
  105. {
  106. 'id':6,
  107. 'avator': Assets.communityCamera,
  108. 'title': 'Fsjfkds dfsk',
  109. 'isFollow': false,
  110. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  111. 'imageUrls': [],
  112. 'time': 'June 17,2016 at 7:23 p.m.',
  113. 'isLike': false,
  114. 'likeno': 12
  115. },
  116. {
  117. 'id':7,
  118. 'avator': Assets.communityCamera,
  119. 'title': '放大发大水',
  120. 'isFollow': false,
  121. 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
  122. 'imageUrls': [],
  123. 'time': 'June 17,2016 at 7:23 p.m.',
  124. 'isLike': false,
  125. 'likeno': 12
  126. },
  127. ]
  128. );
  129. }
  130. @override
  131. NewsfeedState build(){
  132. // 引入数据仓库
  133. newsfeedRepository = ref.read(newsfeedRepositoryProvider);
  134. // 初始化状态
  135. NewsfeedState state = initState();
  136. return state;
  137. }
  138. // 上拉加载
  139. Future onLoadData() async {
  140. Log.d("----newsfeed_vm-----initListData");
  141. // await Future.delayed(const Duration(seconds: 2));
  142. // if(state.list.length >= state.filterCount){
  143. // return;
  144. // }else {
  145. // int curPage = state.curPage + 1;
  146. // state = state.copyWith(curPage: curPage,);
  147. // getListData();
  148. // }
  149. // getListData();
  150. }
  151. // 获取list 列表数据
  152. void getListData<T>() async {
  153. Log.d("加载listData数据---------------start-----");
  154. try {
  155. //请求网络
  156. Map<String, dynamic> params = {
  157. "curPage": state.curPage,
  158. "pageSize": state.pageSize,
  159. };
  160. Log.d("请求参数------$params");
  161. final result = await newsfeedRepository.fetchNewsfeedList(params);
  162. Log.d("请求完成结果------${result.data}");
  163. //校验成功失败
  164. if (result.isSuccess) {
  165. // state = state.copyWith(serverTime: result.data);
  166. state = state;
  167. ToastEngine.show("获取数据成功");
  168. } else {
  169. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  170. }
  171. } catch (e) {
  172. ToastEngine.show("Error: $e");
  173. }
  174. }
  175. // 下拉刷新
  176. Future refreshListData() async {
  177. Log.d("----property_news_vm-----refreshListData ");
  178. // await Future.delayed(const Duration(seconds: 2));
  179. state = state.copyWith(curPage: 1, pageSize: 10);
  180. // ref.invalidateSelf();
  181. // ref.invalidate(newsfeedVmProvider);
  182. getListData();
  183. }
  184. // 点击发布的按钮 跳转到发布的页面
  185. void handlerGotoPost(context){
  186. // ComponentServiceManager().communityService.startCommunityPage();
  187. // AutoRouter.of(context).pushNamed(RouterPath.newsFeedPost);
  188. NewsfeedPostPage.startInstance();
  189. }
  190. // 点击tab (news 、 following 、 for you)
  191. void handlerClickTab(int index, item){
  192. // 修改当前tab 的索引
  193. state = state.copyWith(activeIndex: index);
  194. print("切换后新的 sate, ${state.tabsList}");
  195. // ref.invalidate(customTabsVmProvider);
  196. }
  197. // 点击 like comments share
  198. void handlerClickActionBtn(String? actionStr, item){
  199. final id = item['id'];
  200. switch (actionStr) {
  201. case 'like':
  202. Log.d("点击了 点赞");
  203. handlerGotoDetail(id);
  204. break;
  205. case 'comments':
  206. Log.d("点击了 评论");
  207. handlerGotoDetail(id);
  208. break;
  209. case 'share':
  210. Log.d("点击了 分享");
  211. handlerGotoDetail(id);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. // 去详情页面
  218. void handlerGotoDetail(id){
  219. Log.d("去详情页面");
  220. appRouter.push(NewsfeedDetailPageRoute(id: id));
  221. }
  222. }