video_commend_provider.dart 4.6 KB

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