common_provider.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'package:news_app/constant/api_const.dart';
  3. import '../http/http_util.dart';
  4. import '../http/model_parser.dart';
  5. import '../model/new_comment_model.dart';
  6. import '../ui/video/comment_page.dart';
  7. import '../util/toast_util.dart';
  8. /// @author: bo.zeng
  9. /// @email: cnhbwds@gmail.com
  10. /// @date: 2025 2025/4/17 12:51
  11. /// @description:
  12. class CommentNotifier extends FamilyNotifier<NewCommentModel, CommentType> {
  13. late final CommentType _commentType;
  14. //type 1为视频评论,2为话题评论 3为活动评论 4.新闻评论
  15. @override
  16. NewCommentModel build(CommentType commentType) {
  17. _commentType = commentType;
  18. return NewCommentModel();
  19. }
  20. Future<void> reportComment(String type,String resourceId) async {
  21. final jsonData = await HttpUtil().post(
  22. apiCommentReport,
  23. data: {
  24. "type": type,
  25. "resourceId": resourceId,
  26. },
  27. );
  28. if (jsonData != null ){
  29. showToast("举报成功");
  30. }
  31. }
  32. //新增的评论
  33. void addComment(String sourceId, Comment comment) async {
  34. // state.records?.add(comment);
  35. final jsonData = await HttpUtil().post(
  36. getPushCommentApi(),
  37. data: {
  38. "sourceId": sourceId,
  39. "content": comment.content,
  40. "commentId": "",
  41. "replyId": "",
  42. "resources": comment.resourceUrls?.join(','),
  43. },
  44. );
  45. if (jsonData != null ){
  46. showToast("评论成功");
  47. }
  48. }
  49. //回复别人的评论
  50. void addReply(String sourceId, int index, SubComment reply) async {
  51. final updatedComment = state.records?[index].copyWith(
  52. subComment: [...?state.records?[index].subComment, reply],
  53. );
  54. /* final newState = [...?state.records];
  55. newState[index] = updatedComment!;
  56. state.records?[index] = updatedComment;*/
  57. final jsonData = await HttpUtil().post(
  58. getPushCommentApi(),
  59. data: {
  60. "sourceId": sourceId,
  61. "content": reply.content,
  62. "commentId": updatedComment?.commentId,
  63. "replyId": "",
  64. },
  65. );
  66. if (jsonData != null ){
  67. showToast("评论成功");
  68. }
  69. }
  70. String getPushCommentApi() {
  71. switch (_commentType) {
  72. case CommentType.video:
  73. return apiVideoCommentPublish;
  74. case CommentType.topic:
  75. return apiTopicCommentPublish;
  76. case CommentType.activity || CommentType.activityVideo:
  77. return apiActivityCommentPublish;
  78. case CommentType.news:
  79. return apiNewsCommentPublish;
  80. }
  81. }
  82. String getCommentListApi() {
  83. switch (_commentType) {
  84. case CommentType.video:
  85. return apiVideoCommentList;
  86. case CommentType.topic:
  87. return apiTopicCommentList;
  88. case CommentType.activity || CommentType.activityVideo:
  89. return apiActivityCommentList;
  90. case CommentType.news:
  91. return apiNewsCommentList;
  92. }
  93. }
  94. String getKey() {
  95. switch (_commentType) {
  96. case CommentType.video:
  97. return "videoId";
  98. case CommentType.topic:
  99. return "articleId";
  100. case CommentType.activity || CommentType.activityVideo:
  101. return "contentId";
  102. case CommentType.news:
  103. return "contentId";
  104. }
  105. }
  106. Future<void> fetchComment({
  107. required int page,
  108. required String articleId,
  109. }) async {
  110. var jsonData = await HttpUtil().get(
  111. getCommentListApi(),
  112. queryParameters: {
  113. getKey(): articleId,
  114. "pn": page.toString(),
  115. "ps": 10.toString(),
  116. }, //"665168438702149"
  117. );
  118. final response = ModelParser.parseObject<NewCommentModel>(
  119. jsonData,
  120. NewCommentModel.fromJson,
  121. );
  122. state = response;
  123. }
  124. }