video_commend_provider.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:news_app/http/http_util.dart';
  3. import 'package:news_app/http/model_parser.dart';
  4. import '../constant/api_const.dart';
  5. import '../model/video_new_model.dart';
  6. import '../ui/me/user_favorite_page.dart';
  7. /// @author: bo.zeng
  8. /// @email: cnhbwds@gmail.com
  9. /// @date: 2025 2025/4/21 11:40
  10. /// @description:
  11. class VideoRecommendProvider extends Notifier<List<VideoNewModel>> {
  12. @override
  13. List<VideoNewModel> build() {
  14. return const <VideoNewModel>[];
  15. }
  16. Future<void> fetchRecommendVideos() async {
  17. try {
  18. final jsonData = await HttpUtil().get(apiVideoRecommend);
  19. // 确保 jsonData 是一个 List
  20. if (jsonData == null) {
  21. state = const <VideoNewModel>[];
  22. return;
  23. }
  24. // 如果 jsonData 是 Map 且有 data 字段,提取 data
  25. final List<dynamic> dataList;
  26. if (jsonData is Map) {
  27. final data = jsonData['data'];
  28. if (data is List) {
  29. dataList = data;
  30. } else {
  31. state = const <VideoNewModel>[];
  32. return;
  33. }
  34. } else if (jsonData is List) {
  35. dataList = jsonData;
  36. } else {
  37. state = const <VideoNewModel>[];
  38. return;
  39. }
  40. final data = ModelParser.parseList<VideoNewModel>(
  41. dataList,
  42. VideoNewModel.fromJson,
  43. );
  44. state = data;
  45. } catch (e) {
  46. // 出错时保持空列表状态
  47. state = const <VideoNewModel>[];
  48. }
  49. }
  50. Future<void> fetchVideoLike({
  51. required String? videoId,
  52. required bool current,
  53. }) async {
  54. if (videoId == null || videoId.isEmpty) {
  55. return;
  56. }
  57. final jsonData = await HttpUtil().get(
  58. current ? apiVideoCancelLike : apiVideoLike,
  59. queryParameters: {"videoId": videoId},
  60. );
  61. if (jsonData == true) {
  62. //修改成功后,把state列表中的item likeCount+1
  63. if (current) {
  64. state =
  65. state.map((video) {
  66. if (video.contentId == videoId) {
  67. return video.copyWith(
  68. likeCount: (video.likeCount ?? 1) - 1,
  69. isLiked: false,
  70. );
  71. }
  72. return video;
  73. }).toList();
  74. } else {
  75. state =
  76. state.map((video) {
  77. if (video.contentId == videoId) {
  78. return video.copyWith(
  79. likeCount: (video.likeCount ?? 0) + 1,
  80. isLiked: true,
  81. );
  82. }
  83. return video;
  84. }).toList();
  85. }
  86. }
  87. }
  88. Future<void> fetchVideoFavorite({
  89. required String? videoId,
  90. required bool current,
  91. }) async {
  92. if (videoId == null || videoId.isEmpty) {
  93. return;
  94. }
  95. final jsonData = await HttpUtil().get(
  96. current ? apiVideoCancelFavorite : apiVideoFavorite,
  97. queryParameters: {"videoId": videoId},
  98. );
  99. if (jsonData == true) {
  100. //修改成功后,把state列表中的item likeCount+1
  101. if (current) {
  102. state =
  103. state.map((video) {
  104. if (video.contentId == videoId) {
  105. return video.copyWith(
  106. favoriteCount: (video.favoriteCount ?? 1) - 1,
  107. isFavorite: false,
  108. );
  109. }
  110. return video;
  111. }).toList();
  112. ref.read(favoriteVideoProvider.notifier).removeItemByContentId(videoId);
  113. } else {
  114. state =
  115. state.map((video) {
  116. if (video.contentId == videoId) {
  117. return video.copyWith(
  118. favoriteCount: (video.favoriteCount ?? 0) + 1,
  119. isFavorite: true,
  120. );
  121. }
  122. return video;
  123. }).toList();
  124. }
  125. }
  126. }
  127. Future<void> fetchVideoShare({required String? contentId}) async {
  128. if (contentId == null || contentId.isEmpty) {
  129. return;
  130. }
  131. final jsonData = await HttpUtil().get(
  132. apiVideoShare,
  133. queryParameters: {"contentId": contentId},
  134. );
  135. if (jsonData == true) {
  136. state =
  137. state.map((video) {
  138. if (video.contentId == contentId) {
  139. return video.copyWith(shareCount: (video.shareCount ?? 0) + 1);
  140. }
  141. return video;
  142. }).toList();
  143. }
  144. }
  145. }