following_vm.dart 9.2 KB

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