video_commend_provider.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 List<VideoNewModel>.empty();
  15. }
  16. Future<void> fetchRecommendVideos() async {
  17. final jsonData = await HttpUtil().get(apiVideoRecommend);
  18. final data = ModelParser.parseList<VideoNewModel>(
  19. jsonData,
  20. VideoNewModel.fromJson,
  21. );
  22. state = data;
  23. }
  24. Future<void> fetchVideoLike({
  25. required String? videoId,
  26. required bool current,
  27. }) async {
  28. if (videoId == null || videoId.isEmpty) {
  29. return;
  30. }
  31. final jsonData = await HttpUtil().get(
  32. current ? apiVideoCancelLike : apiVideoLike,
  33. queryParameters: {"videoId": videoId},
  34. );
  35. if (jsonData == true) {
  36. //修改成功后,把state列表中的item likeCount+1
  37. if (current) {
  38. state =
  39. state.map((video) {
  40. if (video.contentId == videoId) {
  41. return video.copyWith(
  42. likeCount: (video.likeCount ?? 1) - 1,
  43. isLiked: false,
  44. );
  45. }
  46. return video;
  47. }).toList();
  48. } else {
  49. state =
  50. state.map((video) {
  51. if (video.contentId == videoId) {
  52. return video.copyWith(
  53. likeCount: (video.likeCount ?? 0) + 1,
  54. isLiked: true,
  55. );
  56. }
  57. return video;
  58. }).toList();
  59. }
  60. }
  61. }
  62. Future<void> fetchVideoFavorite({
  63. required String? videoId,
  64. required bool current,
  65. }) async {
  66. if (videoId == null || videoId.isEmpty) {
  67. return;
  68. }
  69. final jsonData = await HttpUtil().get(
  70. current ? apiVideoCancelFavorite : apiVideoFavorite,
  71. queryParameters: {"videoId": videoId},
  72. );
  73. if (jsonData == true) {
  74. //修改成功后,把state列表中的item likeCount+1
  75. if (current) {
  76. state =
  77. state.map((video) {
  78. if (video.contentId == videoId) {
  79. return video.copyWith(
  80. favoriteCount: (video.favoriteCount ?? 1) - 1,
  81. isFavorite: false,
  82. );
  83. }
  84. return video;
  85. }).toList();
  86. ref.read(favoriteVideoProvider.notifier).removeItemByContentId(videoId);
  87. } else {
  88. state =
  89. state.map((video) {
  90. if (video.contentId == videoId) {
  91. return video.copyWith(
  92. favoriteCount: (video.favoriteCount ?? 0) + 1,
  93. isFavorite: true,
  94. );
  95. }
  96. return video;
  97. }).toList();
  98. }
  99. }
  100. }
  101. Future<void> fetchVideoShare({required String? contentId}) async {
  102. if (contentId == null || contentId.isEmpty) {
  103. return;
  104. }
  105. final jsonData = await HttpUtil().get(
  106. apiVideoShare,
  107. queryParameters: {"contentId": contentId},
  108. );
  109. if (jsonData == true) {
  110. state =
  111. state.map((video) {
  112. if (video.contentId == contentId) {
  113. return video.copyWith(shareCount: (video.shareCount ?? 0) + 1);
  114. }
  115. return video;
  116. }).toList();
  117. }
  118. }
  119. }