common_provider.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. },
  43. );
  44. if (jsonData != null ){
  45. showToast("评论成功");
  46. }
  47. }
  48. //回复别人的评论
  49. void addReply(String sourceId, int index, SubComment reply) async {
  50. final updatedComment = state.records?[index].copyWith(
  51. subComment: [...?state.records?[index].subComment, reply],
  52. );
  53. /* final newState = [...?state.records];
  54. newState[index] = updatedComment!;
  55. state.records?[index] = updatedComment;*/
  56. final jsonData = await HttpUtil().post(
  57. getPushCommentApi(),
  58. data: {
  59. "sourceId": sourceId,
  60. "content": reply.content,
  61. "commentId": updatedComment?.commentId,
  62. "replyId": "",
  63. },
  64. );
  65. if (jsonData != null ){
  66. showToast("评论成功");
  67. }
  68. }
  69. String getPushCommentApi() {
  70. switch (_commentType) {
  71. case CommentType.video:
  72. return apiVideoCommentPublish;
  73. case CommentType.topic:
  74. return apiTopicCommentPublish;
  75. case CommentType.activity || CommentType.activityVideo:
  76. return apiActivityCommentPublish;
  77. case CommentType.news:
  78. return apiNewsCommentPublish;
  79. }
  80. }
  81. String getCommentListApi() {
  82. switch (_commentType) {
  83. case CommentType.video:
  84. return apiVideoCommentList;
  85. case CommentType.topic:
  86. return apiTopicCommentList;
  87. case CommentType.activity || CommentType.activityVideo:
  88. return apiActivityCommentList;
  89. case CommentType.news:
  90. return apiNewsCommentList;
  91. }
  92. }
  93. String getKey() {
  94. switch (_commentType) {
  95. case CommentType.video:
  96. return "videoId";
  97. case CommentType.topic:
  98. return "articleId";
  99. case CommentType.activity || CommentType.activityVideo:
  100. return "contentId";
  101. case CommentType.news:
  102. return "contentId";
  103. }
  104. }
  105. Future<void> fetchComment({
  106. required int page,
  107. required String articleId,
  108. }) async {
  109. var jsonData = await HttpUtil().get(
  110. getCommentListApi(),
  111. queryParameters: {
  112. getKey(): articleId,
  113. "pn": page.toString(),
  114. "ps": 10.toString(),
  115. }, //"665168438702149"
  116. );
  117. final response = ModelParser.parseObject<NewCommentModel>(
  118. jsonData,
  119. NewCommentModel.fromJson,
  120. );
  121. state = response;
  122. }
  123. }