following_vm.dart 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. _page++;
  64. getListData();
  65. }else {
  66. refreshController.finishLoad();
  67. }
  68. }
  69. // 下拉刷新
  70. Future onRefresh() async {
  71. Log.d("--following--following_vm-----onRefresh ");
  72. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.following);
  73. if(isShowing){
  74. // 当前pageView 页面正处于显示状态
  75. Log.d("----following_vm-----onRefresh ");
  76. // await Future.delayed(const Duration(seconds: 2));
  77. _page = 1;
  78. getListData();
  79. }else {
  80. refreshController.finishRefresh();
  81. }
  82. }
  83. // 手动进行刷新
  84. Future triggerRefresh() async {
  85. Log.d("trggerRefresh");
  86. refreshController.callRefresh();
  87. }
  88. // 手动进行刷新
  89. Future directRefresh() async {
  90. state = state.copyWith(list:[]);
  91. // 注意:由于 nestedscrollview 嵌套easyfresh 组件 refreshController.callRefresh() 自动刷新只能滚动顶部但是不会触发下拉刷新,这里调用是 用到了将其滚动到顶部的作用,进而刷新操作主动掉接口
  92. // https://github.com/xuelongqy/flutter_easy_refresh/issues/692
  93. refreshController.callRefresh();
  94. refreshController.resetFooter();
  95. _page = 1;
  96. _needShowPlaceholder = true;
  97. getListData();
  98. }
  99. // 重试请求
  100. Future retryRequest() async {
  101. bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.following);
  102. if(isShowing){
  103. Log.d("重新加载数据---9999-----");
  104. _page = 1;
  105. refreshController.resetFooter();
  106. _needShowPlaceholder = false;
  107. getListData();
  108. }else {
  109. refreshController.finishRefresh();
  110. }
  111. }
  112. // 获取list 列表数据
  113. Future getListData<T>() async {
  114. if (_needShowPlaceholder) {
  115. Log.d("修改加载状态---LoadState.State_Loading-----");
  116. changeLoadingState(LoadState.State_Loading, null);
  117. }
  118. try {
  119. //请求网络
  120. Map<String, dynamic> params = {
  121. "page": _page,
  122. "limit": _limit,
  123. };
  124. Log.d("请求参数------$params");
  125. final result = await commonNewsFeedRespositoryInstance.fetchFollowingList(params);
  126. //校验成功失败
  127. if (result.isSuccess) {
  128. handlerResultList((result.data as NewsfeedFollowingEntity).list);
  129. } else {
  130. String errorMessage = result.errorMsg!;
  131. changeLoadingState(LoadState.State_Error, errorMessage);
  132. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  133. }
  134. } catch (e) {
  135. ToastEngine.show("Error: $e");
  136. }
  137. // 最后赋值
  138. _needShowPlaceholder = false;
  139. }
  140. void handlerResultList(List<NewsfeedFollowingList>? list) {
  141. if (list != null && list.isNotEmpty) {
  142. //有数据,判断是刷新还是加载更多的数据
  143. if (_page == 1) {
  144. //刷新的方式
  145. state.list!.clear();
  146. state.list!.addAll(list.map((item) => item.toJson()).toList());
  147. refreshController.finishRefresh();
  148. //更新展示的状态
  149. changeLoadingState(LoadState.State_Success, null);
  150. } else {
  151. //加载更多
  152. final allList = state.list;
  153. allList!.addAll(list.map((item) => item.toJson()).toList());
  154. state = state.copyWith(list: allList);
  155. refreshController.finishLoad();
  156. }
  157. } else {
  158. if (_page == 1) {
  159. //展示无数据的布局
  160. state.list!.clear();
  161. changeLoadingState(LoadState.State_Empty, null);
  162. refreshController.finishRefresh();
  163. } else {
  164. //展示加载完成,没有更多数据了
  165. if(state.list!.length == 0){
  166. changeLoadingState(LoadState.State_Empty, null);
  167. refreshController.finishLoad();
  168. }else {
  169. if(_needShowPlaceholder){
  170. changeLoadingState(LoadState.State_Success, null);
  171. }
  172. }
  173. //更新展示的状态
  174. refreshController.finishLoad(IndicatorResult.noMore);
  175. }
  176. }
  177. }
  178. // 点赞/取消点赞
  179. Future handlerLikeClick(int id, bool isLike, int? itemidx) async {
  180. try {
  181. List<Map<String, dynamic>> listCopyDta = List.from(state.list!);
  182. final result = await commonNewsFeedRespositoryInstance.fetchLikeClick({
  183. "id": id,
  184. });
  185. if (result.isSuccess) {
  186. if(itemidx != null){
  187. // 修改 listCopyDta[itemIdx] 中的 like 状态 和 likes_count
  188. listCopyDta![itemidx]['liked'] = !isLike;
  189. if(isLike){
  190. // 取消点赞
  191. if(listCopyDta![itemidx]['likes_count']>0){
  192. listCopyDta![itemidx]['likes_count'] = listCopyDta![itemidx]['likes_count'] - 1;
  193. }
  194. }else {
  195. listCopyDta![itemidx]['likes_count'] = listCopyDta![itemidx]['likes_count'] + 1;
  196. }
  197. }else {
  198. // 详情中的点赞 需要找到对应的 item 进行 修改 like 和 likes_count
  199. listCopyDta!.forEach((carditem) {
  200. if(carditem['id'] == id){
  201. carditem['liked'] = !isLike;
  202. if(isLike){
  203. // 取消点赞
  204. if(carditem['likes_count']>0){
  205. carditem['likes_count'] = carditem['likes_count'] - 1;
  206. }
  207. }else {
  208. carditem['likes_count'] = carditem['likes_count'] + 1;
  209. }
  210. }
  211. });
  212. }
  213. state = state.copyWith(list: listCopyDta);
  214. final String toastMsg = isLike ? "Cancel successfully": "Liked successfully";
  215. ToastEngine.show(toastMsg);
  216. return true;
  217. }else {
  218. return false;
  219. }
  220. }catch(error) {
  221. return false;
  222. }
  223. }
  224. // 点击 like comments share
  225. Future<bool?> handlerClickActionBtn(String? actionStr, Map<String, dynamic> item, int itemidx) async{
  226. final id = item['id'];
  227. final liked = item.getValue('liked', false);
  228. switch (actionStr) {
  229. case 'like':
  230. return await handlerLikeClick(id, liked, itemidx);
  231. case 'comments':
  232. Log.d("点击了 评论");
  233. handlerGotoDetail(null, id);
  234. break;
  235. case 'share':
  236. Log.d("点击了 分享");
  237. handlerGotoDetail(null, id);
  238. break;
  239. default:
  240. Log.d("点击了卡片");
  241. handlerGotoDetail(null, id);
  242. break;
  243. }
  244. }
  245. // 关注/取消关注
  246. Future<bool> handlerFollow(BuildContext? context, int to_user_id,int cardId, bool isFollow) async{
  247. Log.d("点击了 关注");
  248. try {
  249. final result = await commonNewsFeedRespositoryInstance.handlerFollowOrCancel({
  250. "to_user_id": to_user_id,
  251. });
  252. if(result.isSuccess){
  253. // 修改cardId 对应的 card item 的 account 里面的 followed
  254. if(cardId!=null){
  255. final listCopyDta = List<Map<String, dynamic>>.from(state.list!);
  256. listCopyDta!.forEach((carditem) {
  257. if(carditem['id'] == cardId){
  258. carditem['account']['followed'] = !carditem['account']['followed'];
  259. }
  260. });
  261. state = state.copyWith(list: listCopyDta);
  262. }
  263. return true;
  264. }else {
  265. return false;
  266. }
  267. }catch(error){
  268. Log.d("error: $error");
  269. return false;
  270. }
  271. }
  272. // 设置当前的 _queryParams
  273. setCurrentQueryParams(Map<String, dynamic> params){
  274. _queryParams.addAll(params);
  275. }
  276. // 获取当前的 _queryParams
  277. Map<String, dynamic> getCurrentQueryParams(String? key){
  278. if(key!=null && key!.isNotEmpty){
  279. return _queryParams[key];
  280. }
  281. return _queryParams;
  282. }
  283. // 去详情页面
  284. void handlerGotoDetail(BuildContext? context, int id){
  285. Log.d("去详情页面");
  286. appRouter.push(NewsfeedDetailPageRoute(id: id, type:'following'));
  287. }
  288. }