import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:news_app/http/http_util.dart'; import 'package:news_app/http/model_parser.dart'; import '../constant/api_const.dart'; import '../model/video_new_model.dart'; import '../ui/me/user_favorite_page.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/21 11:40 /// @description: class VideoRecommendProvider extends Notifier> { @override List build() { return const []; } Future fetchRecommendVideos() async { try { final jsonData = await HttpUtil().get(apiVideoRecommend); // 确保 jsonData 是一个 List if (jsonData == null) { state = const []; return; } // 如果 jsonData 是 Map 且有 data 字段,提取 data final List dataList; if (jsonData is Map) { final data = jsonData['data']; if (data is List) { dataList = data; } else { state = const []; return; } } else if (jsonData is List) { dataList = jsonData; } else { state = const []; return; } final data = ModelParser.parseList( dataList, VideoNewModel.fromJson, ); state = data; } catch (e) { // 出错时保持空列表状态 state = const []; } } Future fetchVideoLike({ required String? videoId, required bool current, }) async { if (videoId == null || videoId.isEmpty) { return; } final jsonData = await HttpUtil().get( current ? apiVideoCancelLike : apiVideoLike, queryParameters: {"videoId": videoId}, ); if (jsonData == true) { //修改成功后,把state列表中的item likeCount+1 if (current) { state = state.map((video) { if (video.contentId == videoId) { return video.copyWith( likeCount: (video.likeCount ?? 1) - 1, isLiked: false, ); } return video; }).toList(); } else { state = state.map((video) { if (video.contentId == videoId) { return video.copyWith( likeCount: (video.likeCount ?? 0) + 1, isLiked: true, ); } return video; }).toList(); } } } Future fetchVideoFavorite({ required String? videoId, required bool current, }) async { if (videoId == null || videoId.isEmpty) { return; } final jsonData = await HttpUtil().get( current ? apiVideoCancelFavorite : apiVideoFavorite, queryParameters: {"videoId": videoId}, ); if (jsonData == true) { //修改成功后,把state列表中的item likeCount+1 if (current) { state = state.map((video) { if (video.contentId == videoId) { return video.copyWith( favoriteCount: (video.favoriteCount ?? 1) - 1, isFavorite: false, ); } return video; }).toList(); ref.read(favoriteVideoProvider.notifier).removeItemByContentId(videoId); } else { state = state.map((video) { if (video.contentId == videoId) { return video.copyWith( favoriteCount: (video.favoriteCount ?? 0) + 1, isFavorite: true, ); } return video; }).toList(); } } } Future fetchVideoShare({required String? contentId}) async { if (contentId == null || contentId.isEmpty) { return; } final jsonData = await HttpUtil().get( apiVideoShare, queryParameters: {"contentId": contentId}, ); if (jsonData == true) { state = state.map((video) { if (video.contentId == contentId) { return video.copyWith(shareCount: (video.shareCount ?? 0) + 1); } return video; }).toList(); } } }