| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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<List<VideoNewModel>> {
- @override
- List<VideoNewModel> build() {
- return List<VideoNewModel>.empty();
- }
- Future<void> fetchRecommendVideos() async {
- final jsonData = await HttpUtil().get(apiVideoRecommend);
- final data = ModelParser.parseList<VideoNewModel>(
- jsonData,
- VideoNewModel.fromJson,
- );
- state = data;
- }
- Future<void> 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<void> 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<void> 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();
- }
- }
- }
|