following_vm.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import 'package:cpt_community/respository/newsfeed_following_repository.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:domain/entity/newsfeed_following_entity.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  6. import 'package:riverpod_annotation/riverpod_annotation.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:shared/utils/ext_dart.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:widgets/load_state_layout.dart';
  11. import 'package:widgets/widget_export.dart';
  12. import '../../../router/page/community_page_router.dart';
  13. import '../community_pageview_idx_data.dart';
  14. import '../community_vm.dart';
  15. import 'following_state.dart';
  16. part 'following_vm.g.dart';
  17. @riverpod
  18. class FollowingVm extends _$FollowingVm {
  19. late NewsFeedFollowingRepository repositoryInstance;
  20. bool _needShowPlaceholder = false; //是否展示LoadingView
  21. // Refresh 控制器
  22. final EasyRefreshController refreshController = EasyRefreshController(
  23. controlFinishRefresh: true, //允许刷新
  24. controlFinishLoad: true, //允许加载
  25. );
  26. FollowingState initState() {
  27. return FollowingState(
  28. list: []
  29. );
  30. }
  31. @override
  32. FollowingState build(){
  33. // 引入数据仓库
  34. repositoryInstance = ref.read(newsFeedFollowingRepositoryProvider);
  35. final state = initState();
  36. Log.d("--------------------------build---------------------");
  37. return state;
  38. }
  39. //刷新页面状态
  40. void changeLoadingState(LoadState loadState, String? errorMsg) {
  41. state = state.copyWith(
  42. loadingState: loadState,
  43. errorMessage: errorMsg
  44. );
  45. }
  46. // 初始化页面数据
  47. initPageData() {
  48. Log.d("----following_vm-----initPageData ${state.loadingState}");
  49. onRefresh();
  50. }
  51. // 上拉加载 更多
  52. Future loadMore() async {
  53. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.following);
  54. if(isShowing){
  55. Log.d("----following_vm-----loadMore");
  56. // await Future.delayed(const Duration(seconds: 2));
  57. // if(state.list.length >= state.count){
  58. // return;
  59. // }else {
  60. // int page = state.page + 1;
  61. // state = state.copyWith(page: page,);
  62. // getListData();
  63. // }
  64. // 检查 page 是否为 null,并初始化为 1
  65. int newCurPage = state.page ?? 1;
  66. state = state.copyWith(page: ++newCurPage);
  67. getListData();
  68. }else {
  69. refreshController.finishRefresh();
  70. }
  71. }
  72. // 下拉刷新
  73. Future onRefresh() async {
  74. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.following);
  75. if(isShowing){
  76. // 当前pageView 页面正处于显示状态
  77. Log.d("----following_vm-----onRefresh ");
  78. // await Future.delayed(const Duration(seconds: 2));
  79. state = state.copyWith(page: 1);
  80. getListData();
  81. }else {
  82. refreshController.finishRefresh();
  83. }
  84. }
  85. // 重试请求
  86. Future retryRequest() async {
  87. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.following);
  88. if(isShowing){
  89. state = state.copyWith(page: 1);
  90. _needShowPlaceholder = true;
  91. getListData();
  92. }
  93. }
  94. // 获取list 列表数据
  95. Future getListData<T>() async {
  96. if (_needShowPlaceholder) {
  97. changeLoadingState(LoadState.State_Loading, null);
  98. }
  99. Log.d("加载listData数据---------------start--${state.page}---");
  100. try {
  101. //请求网络
  102. Map<String, dynamic> params = {
  103. "page": state.page,
  104. "limit": state.limit,
  105. };
  106. Log.d("请求参数------$params");
  107. final result = await repositoryInstance.fetchFollowingList(params);
  108. //校验成功失败
  109. if (result.isSuccess) {
  110. handlerResultList((result.data as NewsfeedFollowingEntity).list);
  111. } else {
  112. String errorMessage = result.errorMsg!;
  113. changeLoadingState(LoadState.State_Error, errorMessage);
  114. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  115. }
  116. } catch (e) {
  117. ToastEngine.show("Error: $e");
  118. }
  119. // // 最后赋值
  120. _needShowPlaceholder = false;
  121. }
  122. void handlerResultList(List<NewsfeedFollowingList>? list) {
  123. if (list != null && list.isNotEmpty) {
  124. //有数据,判断是刷新还是加载更多的数据
  125. if (state.page == 1) {
  126. //刷新的方式
  127. state.list!.clear();
  128. state.list!.addAll(list.map((item) => item.toJson()).toList());
  129. refreshController.finishRefresh();
  130. //更新展示的状态
  131. changeLoadingState(LoadState.State_Success, null);
  132. } else {
  133. //加载更多
  134. final allList = state.list;
  135. state = state.copyWith(list: allList);
  136. refreshController.finishLoad();
  137. }
  138. } else {
  139. if (state.page == 1) {
  140. //展示无数据的布局
  141. state.list!.clear();
  142. changeLoadingState(LoadState.State_Empty, null);
  143. refreshController.finishRefresh();
  144. } else {
  145. //展示加载完成,没有更多数据了
  146. if(state.list!.length == 0){
  147. changeLoadingState(LoadState.State_Empty, null);
  148. }else {
  149. if(_needShowPlaceholder){
  150. changeLoadingState(LoadState.State_Success, null);
  151. }
  152. }
  153. refreshController.finishLoad(IndicatorResult.noMore);
  154. }
  155. }
  156. }
  157. // 点赞/取消点赞
  158. Future handlerLikeClick(int id, bool isLike, int itemidx) async {
  159. try {
  160. final result = await repositoryInstance.fetchLikeClick({
  161. "id": id,
  162. });
  163. if (result.isSuccess) {
  164. //重新赋值data或list
  165. // final json = result.getDataJson();
  166. // var data = NewsfeedNewsEntity.fromJson(json!);
  167. //重新赋值data或list
  168. // state.list![id].liked = data.list![0].liked;
  169. // state.list![id].likeno = data.list![0].likeno;
  170. final String toastMsg = isLike ? "Cancel successfully": "Liked successfully";
  171. ToastEngine.show(toastMsg);
  172. // 修改 state.list[itemIdx] 中的 like 状态 和 likes_count
  173. state.list![itemidx]['liked'] = !isLike;
  174. if(isLike){
  175. // 取消点赞
  176. if(state.list![itemidx]['likes_count']>0){
  177. state.list![itemidx]['likes_count'] = state.list![itemidx]['likes_count'] - 1;
  178. }
  179. }else {
  180. state.list![itemidx]['likes_count'] = state.list![itemidx]['likes_count'] + 1;
  181. }
  182. //重新赋值data或list
  183. changeLoadingState(LoadState.State_Success, null);
  184. return true;
  185. }else {
  186. return false;
  187. }
  188. }catch(error) {
  189. return false;
  190. }
  191. }
  192. // 点击 like comments share
  193. Future<bool?> handlerClickActionBtn(String? actionStr, Map<String, dynamic> item, int itemidx) async{
  194. final id = item['id'];
  195. final liked = item.getValue('liked', false);
  196. switch (actionStr) {
  197. case 'like':
  198. return await handlerLikeClick(id, liked, itemidx);
  199. case 'comments':
  200. Log.d("点击了 评论");
  201. handlerGotoDetail(null, id);
  202. break;
  203. case 'share':
  204. Log.d("点击了 分享");
  205. handlerGotoDetail(null, id);
  206. break;
  207. default:
  208. Log.d("点击了卡片");
  209. handlerGotoDetail(null, id);
  210. break;
  211. }
  212. }
  213. Future handlerFollow(BuildContext context, bool isFollow) async{
  214. Log.d("点击了 关注");
  215. // try {
  216. // //请求网络
  217. // Map<String, dynamic> params = {
  218. //
  219. // };
  220. // Log.d("请求参数------$params");
  221. // final result = await repositoryInstance.fetchNewsList(params);
  222. // //校验成功失败
  223. // if (result.isSuccess) {
  224. // handlerResultList((result.data as NewsfeedNewsEntity).list);
  225. // } else {
  226. // String errorMessage = result.errorMsg!;
  227. // changeLoadingState(LoadState.State_Error, errorMessage);
  228. // ToastEngine.show(result.errorMsg ?? "Network Load Error");
  229. // }
  230. // } catch (e) {
  231. // ToastEngine.show("Error: $e");
  232. // }
  233. }
  234. // 去详情页面
  235. void handlerGotoDetail(BuildContext? context, int id){
  236. Log.d("去详情页面");
  237. appRouter.push(NewsfeedDetailPageRoute(id: id, type:'following'));
  238. }
  239. }