user_favorite_provider.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:news_app/extension/base.dart';
  3. import 'package:news_app/model/activity_model.dart';
  4. import 'package:news_app/model/news_data_model.dart';
  5. import '../constant/api_const.dart';
  6. import '../http/http_util.dart';
  7. import '../http/model_parser.dart';
  8. import '../model/video_new_model.dart';
  9. /// @author: bo.zeng
  10. /// @email: cnhbwds@gmail.com
  11. /// @date: 2025 2025/4/22 16:03
  12. /// @description:
  13. class FavoriteNewsProvider extends Notifier<UserNews> {
  14. @override
  15. UserNews build() {
  16. return UserNews();
  17. }
  18. //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video
  19. Future<void> fetchUserFavorite({required int pageNum}) async {
  20. final jsonData = await HttpUtil().get(
  21. apiMemberFavorite,
  22. queryParameters: {
  23. "type": "cms_content",
  24. "pageSize": 10.toString(),
  25. "pageNum": pageNum.toString(),
  26. },
  27. );
  28. final response = ModelParser.parseObject<UserNews>(
  29. jsonData,
  30. UserNews.fromJson,
  31. );
  32. state.total = response.total;
  33. if (pageNum == 0) {
  34. state = response;
  35. } else {
  36. //把新的列表添加到旧的列表中
  37. state = UserNews(
  38. total: response.total,
  39. rows: [...state.rows ?? [], ...response.rows ?? []],
  40. );
  41. }
  42. }
  43. void removeItemByContentId(String contentId) {
  44. final List<NewsRecord?>? currentRows = state.rows;
  45. if (currentRows == null) return;
  46. // 过滤掉 contentId 相同的项
  47. final List<NewsRecord?> newRows = currentRows
  48. .where((item) => item?.contentId != contentId)
  49. .toList();
  50. // 如果过滤后的列表长度与原列表一致,说明没有匹配项,无需更新状态
  51. if (newRows.length == currentRows.length) return;
  52. // 更新 total 并设置新的 state
  53. state = UserNews(
  54. total: (state.total ?? 0) - 1, // total 减 1
  55. rows: newRows,
  56. );
  57. }
  58. }
  59. class FavoriteActivityProvider extends Notifier<UserActivity> {
  60. @override
  61. UserActivity build() {
  62. return UserActivity();
  63. }
  64. //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video
  65. Future<void> fetchUserActivity({required int pageNum}) async {
  66. final jsonData = await HttpUtil().get(
  67. apiMemberFavorite,
  68. queryParameters: {
  69. "type": "cms_content_activity",
  70. "pageSize": 10.toString(),
  71. "pageNum": pageNum.toString(),
  72. },
  73. );
  74. final response = ModelParser.parseObject<UserActivity>(
  75. jsonData,
  76. UserActivity.fromJson,
  77. );
  78. state.total = response.total;
  79. if (pageNum == 0) {
  80. state = response;
  81. } else {
  82. //把新的列表添加到旧的列表中
  83. state = UserActivity(
  84. total: response.total,
  85. rows: [...state.rows ?? [], ...response.rows ?? []],
  86. );
  87. }
  88. }
  89. void removeItemByContentId(String contentId) {
  90. final List<ActivityModelRecord?>? currentRows = state.rows;
  91. if (currentRows == null) return;
  92. // 过滤掉 contentId 相同的项
  93. final List<ActivityModelRecord?> newRows = currentRows
  94. .where((item) => item?.contentId != contentId)
  95. .toList();
  96. // 如果过滤后的列表长度与原列表一致,说明没有匹配项,无需更新状态
  97. if (newRows.length == currentRows.length) return;
  98. // 更新 total 并设置新的 state
  99. state = UserActivity(
  100. total: (state.total ?? 0) - 1, // total 减 1
  101. rows: newRows,
  102. );
  103. }
  104. }
  105. class FavoriteVideoProvider extends Notifier<UserVideo> {
  106. @override
  107. UserVideo build() {
  108. return UserVideo();
  109. }
  110. //文章)cms_content,(活动)cms_content_activity,(视频)cms_content_video
  111. Future<void> fetchUserVideo({required int pageNum}) async {
  112. final jsonData = await HttpUtil().get(
  113. apiMemberFavorite,
  114. queryParameters: {
  115. "type": "cms_content_video",
  116. "pageSize": 10.toString(),
  117. "pageNum": pageNum.toString(),
  118. },
  119. );
  120. final response = ModelParser.parseObject<UserVideo>(
  121. jsonData,
  122. UserVideo.fromJson,
  123. );
  124. state.total = response.total;
  125. if (pageNum == 0) {
  126. state = response;
  127. } else {
  128. //把新的列表添加到旧的列表中
  129. state = UserVideo(
  130. total: response.total,
  131. rows: [...state.rows ?? [], ...response.rows ?? []],
  132. );
  133. }
  134. }
  135. void removeItemByContentId(String contentId) {
  136. final List<VideoNewModel?>? currentRows = state.rows;
  137. if (currentRows == null) return;
  138. // 过滤掉 contentId 相同的项
  139. final List<VideoNewModel?> newRows = currentRows
  140. .where((item) => item?.contentId != contentId)
  141. .toList();
  142. // 如果过滤后的列表长度与原列表一致,说明没有匹配项,无需更新状态
  143. if (newRows.length == currentRows.length) return;
  144. // 更新 total 并设置新的 state
  145. state = UserVideo(
  146. total: (state.total ?? 0) - 1, // total 减 1
  147. rows: newRows,
  148. );
  149. }
  150. }
  151. class UserNews {
  152. List<NewsRecord?>? rows;
  153. int? total;
  154. UserNews({this.rows, this.total});
  155. UserNews.fromJson(Map<String, dynamic> json) {
  156. if (json['rows'] != null) {
  157. final v = json['rows'];
  158. final arr0 = <NewsRecord>[];
  159. v.forEach((v) {
  160. arr0.add(NewsRecord.fromJson(v));
  161. });
  162. rows = arr0;
  163. }
  164. total = json['total']?.toString().convertInt;
  165. }
  166. Map<String, dynamic> toJson() {
  167. final data = <String, dynamic>{};
  168. if (rows != null) {
  169. final v = rows;
  170. final arr0 = [];
  171. for (var v in v!) {
  172. arr0.add(v!.toJson());
  173. }
  174. data['rows'] = arr0;
  175. }
  176. data['total'] = total;
  177. return data;
  178. }
  179. }
  180. class UserActivity {
  181. List<ActivityModelRecord?>? rows;
  182. int? total;
  183. UserActivity({this.rows, this.total});
  184. UserActivity.fromJson(Map<String, dynamic> json) {
  185. if (json['rows'] != null) {
  186. final v = json['rows'];
  187. final arr0 = <ActivityModelRecord>[];
  188. v.forEach((v) {
  189. arr0.add(ActivityModelRecord.fromJson(v));
  190. });
  191. rows = arr0;
  192. }
  193. total = json['total']?.toString().convertInt;
  194. }
  195. Map<String, dynamic> toJson() {
  196. final data = <String, dynamic>{};
  197. if (rows != null) {
  198. final v = rows;
  199. final arr0 = [];
  200. for (var v in v!) {
  201. arr0.add(v!.toJson());
  202. }
  203. data['rows'] = arr0;
  204. }
  205. data['total'] = total;
  206. return data;
  207. }
  208. }
  209. class UserVideo {
  210. List<VideoNewModel?>? rows;
  211. int? total;
  212. UserVideo({this.rows, this.total});
  213. UserVideo.fromJson(Map<String, dynamic> json) {
  214. if (json['rows'] != null) {
  215. final v = json['rows'];
  216. final arr0 = <VideoNewModel>[];
  217. v.forEach((v) {
  218. arr0.add(VideoNewModel.fromJson(v));
  219. });
  220. rows = arr0;
  221. }
  222. total = json['total']?.toString().convertInt;
  223. }
  224. Map<String, dynamic> toJson() {
  225. final data = <String, dynamic>{};
  226. if (rows != null) {
  227. final v = rows;
  228. final arr0 = [];
  229. for (var v in v!) {
  230. arr0.add(v!.toJson());
  231. }
  232. data['rows'] = arr0;
  233. }
  234. data['total'] = total;
  235. return data;
  236. }
  237. }