import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:news_app/extension/base.dart'; import 'package:news_app/model/activity_model.dart'; import 'package:news_app/model/news_data_model.dart'; import '../constant/api_const.dart'; import '../http/http_util.dart'; import '../http/model_parser.dart'; import '../model/video_new_model.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/22 16:03 /// @description: class FavoriteNewsProvider extends Notifier { @override UserNews build() { return UserNews(); } //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video Future fetchUserFavorite({required int pageNum}) async { final jsonData = await HttpUtil().get( apiMemberFavorite, queryParameters: { "type": "cms_content", "pageSize": 10.toString(), "pageNum": pageNum.toString(), }, ); final response = ModelParser.parseObject( jsonData, UserNews.fromJson, ); state.total = response.total; if (pageNum == 0) { state = response; } else { //把新的列表添加到旧的列表中 state = UserNews( total: response.total, rows: [...state.rows ?? [], ...response.rows ?? []], ); } } void removeItemByContentId(String contentId) { final List? currentRows = state.rows; if (currentRows == null) return; // 过滤掉 contentId 相同的项 final List newRows = currentRows .where((item) => item?.contentId != contentId) .toList(); // 如果过滤后的列表长度与原列表一致,说明没有匹配项,无需更新状态 if (newRows.length == currentRows.length) return; // 更新 total 并设置新的 state state = UserNews( total: (state.total ?? 0) - 1, // total 减 1 rows: newRows, ); } } class FavoriteActivityProvider extends Notifier { @override UserActivity build() { return UserActivity(); } //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video Future fetchUserActivity({required int pageNum}) async { final jsonData = await HttpUtil().get( apiMemberFavorite, queryParameters: { "type": "cms_content_activity", "pageSize": 10.toString(), "pageNum": pageNum.toString(), }, ); final response = ModelParser.parseObject( jsonData, UserActivity.fromJson, ); state.total = response.total; if (pageNum == 0) { state = response; } else { //把新的列表添加到旧的列表中 state = UserActivity( total: response.total, rows: [...state.rows ?? [], ...response.rows ?? []], ); } } void removeItemByContentId(String contentId) { final List? currentRows = state.rows; if (currentRows == null) return; // 过滤掉 contentId 相同的项 final List newRows = currentRows .where((item) => item?.contentId != contentId) .toList(); // 如果过滤后的列表长度与原列表一致,说明没有匹配项,无需更新状态 if (newRows.length == currentRows.length) return; // 更新 total 并设置新的 state state = UserActivity( total: (state.total ?? 0) - 1, // total 减 1 rows: newRows, ); } } class FavoriteVideoProvider extends Notifier { @override UserVideo build() { return UserVideo(); } //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video Future fetchUserVideo({required int pageNum}) async { final jsonData = await HttpUtil().get( apiMemberFavorite, queryParameters: { "type": "cms_content_video", "pageSize": 10.toString(), "pageNum": pageNum.toString(), }, ); final response = ModelParser.parseObject( jsonData, UserVideo.fromJson, ); state.total = response.total; if (pageNum == 0) { state = response; } else { //把新的列表添加到旧的列表中 state = UserVideo( total: response.total, rows: [...state.rows ?? [], ...response.rows ?? []], ); } } void removeItemByContentId(String contentId) { final List? currentRows = state.rows; if (currentRows == null) return; // 过滤掉 contentId 相同的项 final List newRows = currentRows .where((item) => item?.contentId != contentId) .toList(); // 如果过滤后的列表长度与原列表一致,说明没有匹配项,无需更新状态 if (newRows.length == currentRows.length) return; // 更新 total 并设置新的 state state = UserVideo( total: (state.total ?? 0) - 1, // total 减 1 rows: newRows, ); } } class UserNews { List? rows; int? total; UserNews({this.rows, this.total}); UserNews.fromJson(Map json) { if (json['rows'] != null) { final v = json['rows']; final arr0 = []; v.forEach((v) { arr0.add(NewsRecord.fromJson(v)); }); rows = arr0; } total = json['total']?.toString().convertInt; } Map toJson() { final data = {}; if (rows != null) { final v = rows; final arr0 = []; for (var v in v!) { arr0.add(v!.toJson()); } data['rows'] = arr0; } data['total'] = total; return data; } } class UserActivity { List? rows; int? total; UserActivity({this.rows, this.total}); UserActivity.fromJson(Map json) { if (json['rows'] != null) { final v = json['rows']; final arr0 = []; v.forEach((v) { arr0.add(ActivityModelRecord.fromJson(v)); }); rows = arr0; } total = json['total']?.toString().convertInt; } Map toJson() { final data = {}; if (rows != null) { final v = rows; final arr0 = []; for (var v in v!) { arr0.add(v!.toJson()); } data['rows'] = arr0; } data['total'] = total; return data; } } class UserVideo { List? rows; int? total; UserVideo({this.rows, this.total}); UserVideo.fromJson(Map json) { if (json['rows'] != null) { final v = json['rows']; final arr0 = []; v.forEach((v) { arr0.add(VideoNewModel.fromJson(v)); }); rows = arr0; } total = json['total']?.toString().convertInt; } Map toJson() { final data = {}; if (rows != null) { final v = rows; final arr0 = []; for (var v in v!) { arr0.add(v!.toJson()); } data['rows'] = arr0; } data['total'] = total; return data; } }