123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import 'package:cs_resources/generated/assets.dart';
- import 'package:flutter/cupertino.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/log_utils.dart';
- import 'package:widgets/load_state_layout.dart';
- import 'package:widgets/widget_export.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 'news_respository.dart';
- import 'news_state.dart';
- part 'news_vm.g.dart';
- @riverpod
- class NewsVm extends _$NewsVm {
- late NewsRepository NewsRepositoryInstance;
- bool _needShowPlaceholder = true; //是否展示LoadingView
- // Refresh 控制器
- final EasyRefreshController refreshController = EasyRefreshController(
- controlFinishRefresh: true, //允许刷新
- controlFinishLoad: true, //允许加载
- );
- NewsState initState() {
- return NewsState(
- list: []
- );
- }
- @override
- NewsState build(){
- // 引入数据仓库
- NewsRepositoryInstance = ref.read(newsRepositoryProvider);
- 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 {
- bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
- if(isShowing){
- Log.d("----news_vm-----loadMore");
- // await Future.delayed(const Duration(seconds: 2));
- // if(state.list.length >= state.filterCount){
- // return;
- // }else {
- // int curPage = state.curPage + 1;
- // state = state.copyWith(curPage: curPage,);
- // getListData();
- // }
- // 检查 curPage 是否为 null,并初始化为 1
- int newCurPage = state.curPage ?? 1;
- state = state.copyWith(curPage: ++newCurPage);
- getListData();
- }else {
- refreshController.finishRefresh();
- }
- }
- // 下拉刷新
- Future onRefresh() async {
- bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
- if(isShowing){
- // 当前pageView 页面正处于显示状态
- Log.d("----forsale_vm-----onRefresh ");
- // await Future.delayed(const Duration(seconds: 2));
- state = state.copyWith(curPage: 1);
- getListData();
- }else {
- refreshController.finishRefresh();
- // //更新展示的状态
- changeLoadingState(LoadState.State_Success, null);
- }
- }
- // 重试请求
- Future retryRequest() async {
- bool isShowing = await ref.read(communityVmProvider.notifier).isCurrentPageViewShowing(CommunityPageViewIdxData.news);
- if(isShowing){
- state = state.copyWith(curPage: 1);
- _needShowPlaceholder = true;
- getListData();
- }
- }
- // 获取list 列表数据
- Future getListData<T>() async {
- if (_needShowPlaceholder) {
- changeLoadingState(LoadState.State_Loading, null);
- }
- Log.d("for_sale加载listData数据---------------start--${state.curPage}---");
- // try {
- // //请求网络
- // Map<String, dynamic> params = {
- // "curPage": state.curPage,
- // "pageSize": state.pageSize,
- // };
- // Log.d("请求参数------$params");
- // final result = await propertyNewsRepository.fetchPropertyNewsList(params);
- // Log.d("请求完成结果------${result.data}");
- // //校验成功失败
- // if (result.isSuccess) {
- // // state = state.copyWith(serverTime: result.data);
- // state = state;
- // handleList(listResult.data?.rows);
- // ToastEngine.show("获取数据成功");
- // } else {
- // errorMessage = listResult.errorMsg;
- // changeLoadingState(LoadState.State_Error);
- // ToastEngine.show(result.errorMsg ?? "Network Load Error");
- // }
- // } catch (e) {
- // ToastEngine.show("Error: $e");
- // }
- await Future.delayed(const Duration(milliseconds: 1500));
- final List<Map<String, dynamic>> listData = [
- {
- 'id':1,
- 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
- 'title': 'William Jefferson',
- 'isFollow': false,
- 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
- 'imageUrls': ['https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500','https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500','https://img2.baidu.com/it/u=3489233687,2364672159&fm=253&fmt=auto&app=120&f=JPEG?w=507&h=500'],
- 'time': 'June 17,2016 at 7:23 p.m.',
- 'isLike': true,
- 'likeno': 12
- },
- {
- 'id':2,
- 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
- 'title': 'William fdsaf的飞洒发生的',
- 'isFollow': true,
- 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
- 'imageUrls': [],
- 'time': 'June 17,2016 at 7:23 p.m.',
- 'isLike': true,
- 'likeno': 12
- },
- {
- 'id':3,
- 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
- 'title': 'Fsjfkds dfsk',
- 'isFollow': false,
- 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
- 'imageUrls': [],
- 'time': 'June 17,2016 at 7:23 p.m.',
- 'isLike': false,
- 'likeno': 12
- },
- {
- 'id':4,
- 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
- 'title': 'Fsjfkds dfsk',
- 'isFollow': false,
- 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
- 'imageUrls': [],
- 'time': 'June 17,2016 at 7:23 p.m.',
- 'isLike': false,
- 'likeno': 12
- },
- {
- 'id':5,
- 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
- 'title': 'Fsjfkds dfsk',
- 'isFollow': false,
- 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
- 'imageUrls': [],
- 'time': 'June 17,2016 at 7:23 p.m.',
- 'isLike': false,
- 'likeno': 12
- },
- {
- 'id':6,
- 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
- 'title': 'Fsjfkds dfsk',
- 'isFollow': false,
- 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
- 'imageUrls': [],
- 'time': 'June 17,2016 at 7:23 p.m.',
- 'isLike': false,
- 'likeno': 12
- },
- {
- 'id':7,
- 'avator': 'https://img1.baidu.com/it/u=3890726495,1572750319&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
- 'title': '放大发大水',
- 'isFollow': false,
- 'content': 'She said YES and our lives changed.Thank you all for coming to my propose today.We hope everyone can ……[More]',
- 'imageUrls': [],
- 'time': 'June 17,2016 at 7:23 p.m.',
- 'isLike': false,
- 'likeno': 12
- },
- ];
- if (state.curPage == 1) {
- //刷新的方式
- state = state.copyWith(list: listData);
- refreshController.finishRefresh();
- // //更新展示的状态
- changeLoadingState(LoadState.State_Success, null);
- } else {
- //加载更多
- final allList = state.list;
- allList?.addAll(listData);
- state = state.copyWith(list: allList);
- refreshController.finishLoad();
- }
- // 最后赋值
- _needShowPlaceholder = false;
- }
- // 点击 like comments share
- void handlerClickActionBtn(String? actionStr, item){
- final id = item['id'];
- switch (actionStr) {
- case 'like':
- Log.d("点击了 点赞");
- handlerGotoDetail(id);
- break;
- case 'comments':
- Log.d("点击了 评论");
- handlerGotoDetail(id);
- break;
- case 'share':
- Log.d("点击了 分享");
- handlerGotoDetail(id);
- break;
- default:
- Log.d("点击了卡片");
- handlerGotoDetail(id);
- break;
- }
- }
- // 去详情页面
- void handlerGotoDetail(id){
- Log.d("去详情页面");
- appRouter.push(NewsfeedDetailPageRoute(id: id, type:'news'));
- }
- }
|