| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- 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<UserNews> {
- @override
- UserNews build() {
- return UserNews();
- }
- //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video
- Future<void> 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<UserNews>(
- 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<NewsRecord?>? currentRows = state.rows;
- if (currentRows == null) return;
- // 过滤掉 contentId 相同的项
- final List<NewsRecord?> 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<UserActivity> {
- @override
- UserActivity build() {
- return UserActivity();
- }
- //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video
- Future<void> 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<UserActivity>(
- 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<ActivityModelRecord?>? currentRows = state.rows;
- if (currentRows == null) return;
- // 过滤掉 contentId 相同的项
- final List<ActivityModelRecord?> 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<UserVideo> {
- @override
- UserVideo build() {
- return UserVideo();
- }
- //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video
- Future<void> 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<UserVideo>(
- 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<VideoNewModel?>? currentRows = state.rows;
- if (currentRows == null) return;
- // 过滤掉 contentId 相同的项
- final List<VideoNewModel?> 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<NewsRecord?>? rows;
- int? total;
- UserNews({this.rows, this.total});
- UserNews.fromJson(Map<String, dynamic> json) {
- if (json['rows'] != null) {
- final v = json['rows'];
- final arr0 = <NewsRecord>[];
- v.forEach((v) {
- arr0.add(NewsRecord.fromJson(v));
- });
- rows = arr0;
- }
- total = json['total']?.toString().convertInt;
- }
- Map<String, dynamic> toJson() {
- final data = <String, dynamic>{};
- 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<ActivityModelRecord?>? rows;
- int? total;
- UserActivity({this.rows, this.total});
- UserActivity.fromJson(Map<String, dynamic> json) {
- if (json['rows'] != null) {
- final v = json['rows'];
- final arr0 = <ActivityModelRecord>[];
- v.forEach((v) {
- arr0.add(ActivityModelRecord.fromJson(v));
- });
- rows = arr0;
- }
- total = json['total']?.toString().convertInt;
- }
- Map<String, dynamic> toJson() {
- final data = <String, dynamic>{};
- 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<VideoNewModel?>? rows;
- int? total;
- UserVideo({this.rows, this.total});
- UserVideo.fromJson(Map<String, dynamic> json) {
- if (json['rows'] != null) {
- final v = json['rows'];
- final arr0 = <VideoNewModel>[];
- v.forEach((v) {
- arr0.add(VideoNewModel.fromJson(v));
- });
- rows = arr0;
- }
- total = json['total']?.toString().convertInt;
- }
- Map<String, dynamic> toJson() {
- final data = <String, dynamic>{};
- 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;
- }
- }
|