123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- import 'package:cpt_community/respository/common_newsfeed.dart';
- import 'package:cs_resources/generated/assets.dart';
- import 'package:domain/entity/newsfeed_news_entity.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:plugin_basic/provider/user_config/user_config_service.dart';
- import 'package:plugin_platform/engine/toast/toast_engine.dart';
- import 'package:riverpod_annotation/riverpod_annotation.dart';
- import 'package:router/ext/auto_router_extensions.dart';
- import 'package:shared/utils/ext_dart.dart';
- import 'package:shared/utils/log_utils.dart';
- import 'package:widgets/load_state_layout.dart';
- import 'package:widgets/widget_export.dart';
- import 'package:easy_refresh/easy_paging.dart';
- import '../../../router/page/community_page_router.dart';
- import '../community_pageview_idx_data.dart';
- import '../community_vm.dart';
- import '../newsfeed_detail/newsfeed_detail_page.dart';
- import '../newsfeed_detail/newsfeed_detail_vm.dart';
- import 'news_state.dart';
- part 'news_vm.g.dart';
- @riverpod
- class NewsVm extends _$NewsVm {
- late CommonNewsFeedRespository commonNewsFeedRespositoryInstance;
- int _page = 1; // 当前页
- int _limit = 10; // 每页数量
- int _count = 0; // 总条数
- Map<String, dynamic> _queryParams = {
- 'keyword': null,
- 'is_liked': null,
- };
-
- bool _needShowPlaceholder = false; //是否展示LoadingView
- // Scroll 控制器
- final ScrollController scrollController = ScrollController();
- // Refresh 控制器
- final EasyRefreshController refreshController = EasyRefreshController(
- controlFinishRefresh: true, //允许刷新
- controlFinishLoad: true, //允许加载
- );
- NewsState initState() {
- return NewsState(
- list: []
- );
- }
- @override
- NewsState build(){
- // 引入数据仓库
- commonNewsFeedRespositoryInstance = ref.read(commonNewsFeedRespositoryProvider);
- final state = initState();
- Log.d("--------------------------build---------------------");
- return state;
- }
- //刷新页面状态
- void changeLoadingState(LoadState loadState, String? errorMsg) {
- state = state.copyWith(
- loadingState: loadState,
- errorMessage: errorMsg
- );
- }
- // 初始化页面数据
- initPageData() {
- Log.d("----news_vm-----initPageData ${state.loadingState}");
- onRefresh();
- }
- // 上拉加载 更多
- Future loadMore() async {
- Log.d("----news_vm-----loadMore");
- _page++;
- getListData(isLoadMore: true);
- }
- // 下拉刷新
- Future onRefresh() async {
- // 当前pageView 页面正处于显示状态
- Log.d("----news_vm-----onRefresh ");
- // await Future.delayed(const Duration(seconds: 2));
- _page = 1;
- getListData();
- }
- // 手动进行刷新
- Future triggerRefresh() async {
- Log.d("trggerRefresh");
- refreshController.callRefresh();
- }
- // 重试请求
- Future retryRequest() async {
- _page = 1;
- _needShowPlaceholder = true;
- getListData();
- // refreshController.callRefresh();
- }
- // 手动进行刷新
- Future directRefresh() async {
- state = state.copyWith(list:[]);
- // 注意:由于 nestedscrollview 嵌套easyfresh 组件 refreshController.callRefresh() 自动刷新只能滚动顶部但是不会触发下拉刷新,这里调用是 用到了将其滚动到顶部的作用,进而刷新操作主动掉接口
- // https://github.com/xuelongqy/flutter_easy_refresh/issues/692
- // refreshController.callRefresh();
- refreshController.resetFooter();
- _page = 1;
- _needShowPlaceholder = true;
- getListData();
- }
- // 获取list 列表数据
- Future getListData<T>({bool? isLoadMore}) async {
- Log.d("getListData _needShowPlaceholder: $_needShowPlaceholder");
- if (_needShowPlaceholder) {
- changeLoadingState(LoadState.State_Loading, null);
- }
- try {
- //请求网络
- Map<String, dynamic> params = {
- "page": _page,
- "limit": _limit,
- };
- Log.d("请求参数------$params");
- final result = await commonNewsFeedRespositoryInstance.fetchNewsList(params);
- //校验成功失败
- if (result.isSuccess) {
- handlerResultList((result.data as NewsfeedNewsEntity).list, isLoadMore ?? false);
- } else {
- String errorMessage = result.errorMsg!;
- changeLoadingState(LoadState.State_Error, errorMessage);
- ToastEngine.show(result.errorMsg ?? "Network Load Error");
- }
- } catch (e) {
- ToastEngine.show("Error: $e");
- }
- _needShowPlaceholder = false;
- }
- void handlerResultList(List<NewsfeedNewsList>? list, bool isLoadMore) {
- if (list != null && list.isNotEmpty) {
- //有数据,判断是刷新还是加载更多的数据
- if (_page == 1) {
- refreshController.resetFooter();
- //刷新的方式
- state.list!.clear();
- state.list!.addAll(list.map((item) => item.toJson()).toList());
- refreshController.finishRefresh();
- //更新展示的状态
- changeLoadingState(LoadState.State_Success, null);
- } else {
- //加载更多
- final allList = state.list;
- allList!.addAll(list.map((item) => item.toJson()).toList());
- state = state.copyWith(list: allList);
- refreshController.finishLoad();
- }
- } else {
- if (_page == 1) {
- //展示无数据的布局
- state.list!.clear();
- changeLoadingState(LoadState.State_Empty, null);
- refreshController.finishRefresh();
- } else {
- //展示加载完成,没有更多数据了
- if(state.list!.length == 0){
- changeLoadingState(LoadState.State_Empty, null);
- refreshController.finishLoad();
- }else {
- if(_needShowPlaceholder){
- changeLoadingState(LoadState.State_Success, null);
- }
- }
- //更新展示的状态
- refreshController.finishLoad(IndicatorResult.noMore);
- }
- }
- }
- // 点赞/取消点赞
- Future handlerLikeClick(int id, bool isLike, int? itemidx) async {
- try {
- List<Map<String, dynamic>> listCopyDta = List.from(state.list!);
- final result = await commonNewsFeedRespositoryInstance.fetchLikeClick({
- "id": id,
- });
- if (result.isSuccess) {
- if(itemidx != null){
- // 修改 listCopyDta[itemIdx] 中的 like 状态 和 likes_count
- listCopyDta![itemidx]['liked'] = !isLike;
- if(isLike){
- // 取消点赞
- if(listCopyDta![itemidx]['likes_count']>0){
- listCopyDta![itemidx]['likes_count'] = listCopyDta![itemidx]['likes_count'] - 1;
- }
- }else {
- listCopyDta![itemidx]['likes_count'] = listCopyDta![itemidx]['likes_count'] + 1;
- }
- }else {
- // 详情中的点赞 需要找到对应的 item 进行 修改 like 和 likes_count
- listCopyDta!.forEach((carditem) {
- if(carditem['id'] == id){
- carditem['liked'] = !isLike;
- if(isLike){
- // 取消点赞
- if(carditem['likes_count']>0){
- carditem['likes_count'] = carditem['likes_count'] - 1;
- }
- }else {
- carditem['likes_count'] = carditem['likes_count'] + 1;
- }
- }
- });
- }
- state = state.copyWith(list: listCopyDta);
- final String toastMsg = isLike ? "Cancel successfully": "Liked successfully";
- ToastEngine.show(toastMsg);
- return true;
- }else {
- return false;
- }
- }catch(error) {
- return false;
- }
- }
- // 点击 like comments share
- Future<bool?> handlerClickActionBtn(String? actionStr, Map<String, dynamic> item, int? itemidx) async{
- final id = item['id'];
- final liked = item.getValue('liked', false);
- switch (actionStr) {
- case 'like':
- return await handlerLikeClick(id, liked, itemidx);
- case 'comments':
- Log.d("点击了 评论");
- handlerGotoDetail(null, id);
- break;
- case 'share':
- Log.d("点击了 分享");
- handlerGotoDetail(null, id);
- break;
- default:
- Log.d("点击了卡片");
- handlerGotoDetail(null, id);
- break;
- }
- }
- // 关注/取消关注
- Future<bool> handlerFollow(BuildContext? context, int to_user_id,int cardId, bool isFollow) async{
- Log.d("点击了 关注");
- try {
- final result = await commonNewsFeedRespositoryInstance.handlerFollowOrCancel({
- "to_user_id": to_user_id,
- });
- if(result.isSuccess){
- // 修改cardId 对应的 card item 的 account 里面的 followed
- if(cardId!=null){
- final listCopyDta = List<Map<String, dynamic>>.from(state.list!);
- listCopyDta!.forEach((carditem) {
- if(carditem['id'] == cardId){
- carditem['account']['followed'] = !carditem['account']['followed'];
- }
- });
- state = state.copyWith(list: listCopyDta);
- }
- // 同步用户信息
- UserConfigService.getInstance().refreshUserInfo();
- return true;
- }else {
- return false;
- }
- }catch(error){
- Log.d("error: $error");
- return false;
- }
- }
- // 设置当前的 _queryParams
- setCurrentQueryParams(Map<String, dynamic> params){
- _queryParams.addAll(params);
- }
- // 获取当前的 _queryParams
- Map<String, dynamic> getCurrentQueryParams(String? key){
- if(key!=null && key!.isNotEmpty){
- return _queryParams[key]??{};
- }
- return _queryParams;
- }
- // 去详情页面
- void handlerGotoDetail(BuildContext? context, int id){
- Log.d("去详情页面");
- appRouter.push(NewsfeedDetailPageRoute(id: id, type: 'news'));
- }
- }
|