following_vm.dart 8.3 KB

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