following_vm.dart 9.1 KB

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