import 'package:plugin_platform/http/http_result.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:shared/utils/log_utils.dart'; import 'package:plugin_platform/engine/toast/toast_engine.dart'; import 'package:widgets/load_state_layout.dart'; import 'package:widgets/widget_export.dart'; import '../page/property_news_state.dart'; import '../repository/property_news_repository.dart'; part 'property_news_vm.g.dart'; @riverpod class PropertyNewsVm extends _$PropertyNewsVm { late PropertyNewsRepository propertyNewsRepository; bool _needShowPlaceholder = true; //是否展示LoadingView // Refresh 控制器 final EasyRefreshController refreshController = EasyRefreshController( controlFinishRefresh: true, //允许刷新 controlFinishLoad: true, //允许加载 ); PropertyNewsState initState() { return PropertyNewsState( list: [], ); } @override PropertyNewsState build() { // 引入数据仓库 propertyNewsRepository = ref.read(propertyNewsRepositoryProvider); // 初始化状态 PropertyNewsState state = initState(); // 初始化列表数据 return state; } //刷新页面状态 void changeLoadingState(LoadState loadState, String? errorMsg) { state = state.copyWith( loadingState: loadState, errorMessage: errorMsg ); } // 初始化页面数据 initPageData() { Log.d("----property_news_vm-----initPageData"); onRefresh(); } // 上拉加载 更多 Future loadMore() async { Log.d("----property_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(); } // 下拉刷新 Future onRefresh() async { Log.d("----property_news_vm-----onRefresh "); // await Future.delayed(const Duration(seconds: 2)); state = state.copyWith(curPage: 1); getListData(); } // 重试请求 Future retryRequest() async { state = state.copyWith(curPage: 1); _needShowPlaceholder = true; getListData(); } // 获取list 列表数据 Future getListData() async { if (_needShowPlaceholder) { changeLoadingState(LoadState.State_Loading, null); } Log.d("加载listData数据---------------start--${state.curPage}---"); // try { // //请求网络 // Map 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> listData = [ { "id": 1, "title": "fkladsfk fldask fldsakfllfkaslsd", "description": "fsklfdsk罚款乱收费上课了发送卡", "time": "2024-02-15 12:00:00", "isCollection": true, "pic": "" }, { "id": 2, "title": "JHKFDSAJKjfkdsfjkasjkjklfajfkajifwoqirujweiqofjndsaikfniasdhfiasdhfiadshfifjadslfjkdlsafjlkadsj", "description": "oifosjf fjdskafj hjiwehfriohjfiash", "time": "2024-10-16 12:00:00", "isCollection": false, "pic": "" }, ]; if (state.curPage == 1) { //刷新的方式 state = state.copyWith(list: listData); refreshController.finishRefresh(); //更新展示的状态 changeLoadingState(LoadState.State_Success, null); } else { //加载更多 final allList = state.list; allList.addAll(listData); refreshController.finishLoad(); state = state.copyWith(list: allList); } // 最后赋值 _needShowPlaceholder = false; } // 去新闻详情页 void goNewsDetail(String item) { Log.d("goNewsDetail"); // PropertyPage.startInstance(context: context, item: item); } // 收藏/取消收藏 void handlerCollection(curItem, bool isCollection){ List> newList = state.list.map((item) { if(item['id'] == curItem['id']){ return { ...item, 'isCollection': !isCollection }; } return item; }).toList(); // Log.d("handlerCollection $newList"); state = state.copyWith(list: newList); if(isCollection){ ToastEngine.show("取消收藏"); }else { ToastEngine.show("收藏成功"); } } // 处理数据与展示的逻辑 // void handleList(List? list) { // if (list != null && list.isNotEmpty) { // //有数据,判断是刷新还是加载更多的数据 // if (_curPage == 1) { // //刷新的方式 // state.list.clear(); // state.list.addAll(list); // refreshController.finishRefresh(); // // //更新展示的状态 // changeLoadingState(LoadState.State_Success); // } else { // //加载更多 // state.list.addAll(list); // refreshController.finishLoad(); // update(); // } // } else { // if (_curPage == 1) { // //展示无数据的布局 // state.list.clear(); // changeLoadingState(LoadState.State_Empty); // refreshController.finishRefresh(); // } else { // //展示加载完成,没有更多数据了 // refreshController.finishLoad(IndicatorResult.noMore); // } // } // } }