following_vm.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_basic/provider/user_config/user_config_service.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 '../../../respository/common_newsfeed.dart';
  13. import '../../../router/page/community_page_router.dart';
  14. import '../community_pageview_idx_data.dart';
  15. import '../community_vm.dart';
  16. import 'following_state.dart';
  17. part 'following_vm.g.dart';
  18. @riverpod
  19. class FollowingVm extends _$FollowingVm {
  20. late CommonNewsFeedRespository commonNewsFeedRespositoryInstance;
  21. int _page = 1; // 当前页
  22. int _limit = 10; // 每页数量
  23. int _count = 0; // 总条数
  24. Map<String, dynamic> _queryParams = {
  25. 'keyword': null,
  26. 'is_liked': null,
  27. };
  28. bool _needShowPlaceholder = false; //是否展示LoadingView
  29. // Refresh 控制器
  30. final EasyRefreshController refreshController = EasyRefreshController(
  31. controlFinishRefresh: true, //允许刷新
  32. controlFinishLoad: true, //允许加载
  33. );
  34. FollowingState initState() {
  35. return FollowingState(
  36. list: []
  37. );
  38. }
  39. @override
  40. FollowingState build(){
  41. // 引入数据仓库
  42. commonNewsFeedRespositoryInstance = ref.read(commonNewsFeedRespositoryProvider);
  43. final state = initState();
  44. Log.d("---------------following-----------build---------------------");
  45. return state;
  46. }
  47. //刷新页面状态
  48. void changeLoadingState(LoadState loadState, String? errorMsg) {
  49. state = state.copyWith(
  50. loadingState: loadState,
  51. errorMessage: errorMsg
  52. );
  53. }
  54. // 初始化页面数据
  55. initPageData() {
  56. Log.d("----following_vm-----initPageData ${state.loadingState}");
  57. onRefresh();
  58. }
  59. // 上拉加载 更多
  60. Future loadMore() async {
  61. Log.d("----following_vm-----loadMore");
  62. _page++;
  63. getListData();
  64. }
  65. // 下拉刷新
  66. Future onRefresh() async {
  67. // 当前pageView 页面正处于显示状态
  68. Log.d("----following_vm-----onRefresh ");
  69. // await Future.delayed(const Duration(seconds: 2));
  70. _page = 1;
  71. getListData();
  72. }
  73. // 手动进行刷新
  74. Future triggerRefresh() async {
  75. Log.d("trggerRefresh");
  76. refreshController.callRefresh();
  77. }
  78. // 手动进行刷新
  79. Future directRefresh() async {
  80. state = state.copyWith(list:[]);
  81. // 注意:由于 nestedscrollview 嵌套easyfresh 组件 refreshController.callRefresh() 自动刷新只能滚动顶部但是不会触发下拉刷新,这里调用是 用到了将其滚动到顶部的作用,进而刷新操作主动掉接口
  82. // https://github.com/xuelongqy/flutter_easy_refresh/issues/692
  83. refreshController.callRefresh();
  84. refreshController.resetFooter();
  85. _page = 1;
  86. _needShowPlaceholder = true;
  87. getListData();
  88. }
  89. // 重试请求
  90. Future retryRequest() async {
  91. Log.d("重新加载数据---9999-----");
  92. _page = 1;
  93. refreshController.resetFooter();
  94. _needShowPlaceholder = false;
  95. getListData();
  96. }
  97. // 获取list 列表数据
  98. Future getListData<T>() async {
  99. if (_needShowPlaceholder) {
  100. Log.d("修改加载状态---LoadState.State_Loading-----");
  101. changeLoadingState(LoadState.State_Loading, null);
  102. }
  103. try {
  104. //请求网络
  105. Map<String, dynamic> params = {
  106. "page": _page,
  107. "limit": _limit,
  108. };
  109. Log.d("请求参数------$params");
  110. final result = await commonNewsFeedRespositoryInstance.fetchFollowingList(params);
  111. //校验成功失败
  112. if (result.isSuccess) {
  113. handlerResultList((result.data as NewsfeedFollowingEntity).list);
  114. } else {
  115. String errorMessage = result.errorMsg!;
  116. changeLoadingState(LoadState.State_Error, errorMessage);
  117. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  118. }
  119. } catch (e) {
  120. ToastEngine.show("Error: $e");
  121. }
  122. // 最后赋值
  123. _needShowPlaceholder = false;
  124. }
  125. void handlerResultList(List<NewsfeedFollowingList>? list) {
  126. if (list != null && list.isNotEmpty) {
  127. //有数据,判断是刷新还是加载更多的数据
  128. if (_page == 1) {
  129. //刷新的方式
  130. state.list!.clear();
  131. state.list!.addAll(list.map((item) => item.toJson()).toList());
  132. refreshController.finishRefresh();
  133. //更新展示的状态
  134. changeLoadingState(LoadState.State_Success, null);
  135. } else {
  136. //加载更多
  137. final allList = state.list;
  138. allList!.addAll(list.map((item) => item.toJson()).toList());
  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. List<Map<String, dynamic>> listCopyDta = List.from(state.list!);
  167. final result = await commonNewsFeedRespositoryInstance.fetchLikeClick({
  168. "id": id,
  169. });
  170. if (result.isSuccess) {
  171. if(itemidx != null){
  172. // 修改 listCopyDta[itemIdx] 中的 like 状态 和 likes_count
  173. listCopyDta![itemidx]['liked'] = !isLike;
  174. if(isLike){
  175. // 取消点赞
  176. if(listCopyDta![itemidx]['likes_count']>0){
  177. listCopyDta![itemidx]['likes_count'] = listCopyDta![itemidx]['likes_count'] - 1;
  178. }
  179. }else {
  180. listCopyDta![itemidx]['likes_count'] = listCopyDta![itemidx]['likes_count'] + 1;
  181. }
  182. }else {
  183. // 详情中的点赞 需要找到对应的 item 进行 修改 like 和 likes_count
  184. listCopyDta!.forEach((carditem) {
  185. if(carditem['id'] == id){
  186. carditem['liked'] = !isLike;
  187. if(isLike){
  188. // 取消点赞
  189. if(carditem['likes_count']>0){
  190. carditem['likes_count'] = carditem['likes_count'] - 1;
  191. }
  192. }else {
  193. carditem['likes_count'] = carditem['likes_count'] + 1;
  194. }
  195. }
  196. });
  197. }
  198. state = state.copyWith(list: listCopyDta);
  199. final String toastMsg = isLike ? "Cancel successfully": "Liked successfully";
  200. ToastEngine.show(toastMsg);
  201. return true;
  202. }else {
  203. return false;
  204. }
  205. }catch(error) {
  206. return false;
  207. }
  208. }
  209. // 点击 like comments share
  210. Future<bool?> handlerClickActionBtn(String? actionStr, Map<String, dynamic> item, int itemidx) async{
  211. final id = item['id'];
  212. final liked = item.getValue('liked', false);
  213. switch (actionStr) {
  214. case 'like':
  215. return await handlerLikeClick(id, liked, itemidx);
  216. case 'comments':
  217. Log.d("点击了 评论");
  218. handlerGotoDetail(null, id);
  219. break;
  220. case 'share':
  221. Log.d("点击了 分享");
  222. handlerGotoDetail(null, id);
  223. break;
  224. default:
  225. Log.d("点击了卡片");
  226. handlerGotoDetail(null, id);
  227. break;
  228. }
  229. }
  230. // 关注/取消关注
  231. Future<bool> handlerFollow(BuildContext? context, int to_user_id,int cardId, bool isFollow) async{
  232. Log.d("点击了 关注");
  233. try {
  234. final result = await commonNewsFeedRespositoryInstance.handlerFollowOrCancel({
  235. "to_user_id": to_user_id,
  236. });
  237. if(result.isSuccess){
  238. // 修改cardId 对应的 card item 的 account 里面的 followed
  239. if(cardId!=null){
  240. final listCopyDta = List<Map<String, dynamic>>.from(state.list!);
  241. listCopyDta!.forEach((carditem) {
  242. if(carditem['id'] == cardId){
  243. carditem['account']['followed'] = !carditem['account']['followed'];
  244. }
  245. });
  246. state = state.copyWith(list: listCopyDta);
  247. }
  248. // 同步用户信息
  249. UserConfigService.getInstance().refreshUserInfo();
  250. return true;
  251. }else {
  252. return false;
  253. }
  254. }catch(error){
  255. Log.d("error: $error");
  256. return false;
  257. }
  258. }
  259. // 设置当前的 _queryParams
  260. setCurrentQueryParams(Map<String, dynamic> params){
  261. _queryParams.addAll(params);
  262. }
  263. // 获取当前的 _queryParams
  264. Map<String, dynamic> getCurrentQueryParams(String? key){
  265. if(key!=null && key!.isNotEmpty){
  266. return _queryParams[key];
  267. }
  268. return _queryParams;
  269. }
  270. // 去详情页面
  271. void handlerGotoDetail(BuildContext? context, int id){
  272. Log.d("去详情页面");
  273. appRouter.push(NewsfeedDetailPageRoute(id: id, type:'following'));
  274. }
  275. }