import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:news_app/constant/api_const.dart'; import '../http/http_util.dart'; import '../http/model_parser.dart'; import '../model/new_comment_model.dart'; import '../ui/video/comment_page.dart'; import '../util/toast_util.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/17 12:51 /// @description: class CommentNotifier extends FamilyNotifier { late final CommentType _commentType; //type 1为视频评论,2为话题评论 3为活动评论 4.新闻评论 @override NewCommentModel build(CommentType commentType) { _commentType = commentType; return NewCommentModel(); } Future reportComment(String type,String resourceId) async { final jsonData = await HttpUtil().post( apiCommentReport, data: { "type": type, "resourceId": resourceId, }, ); if (jsonData != null ){ showToast("举报成功"); } } //新增的评论 void addComment(String sourceId, Comment comment) async { // state.records?.add(comment); final jsonData = await HttpUtil().post( getPushCommentApi(), data: { "sourceId": sourceId, "content": comment.content, "commentId": "", "replyId": "", "resources": comment.resourceUrls?.join(','), }, ); if (jsonData != null ){ showToast("评论成功"); } } //回复别人的评论 void addReply(String sourceId, int index, SubComment reply) async { final updatedComment = state.records?[index].copyWith( subComment: [...?state.records?[index].subComment, reply], ); /* final newState = [...?state.records]; newState[index] = updatedComment!; state.records?[index] = updatedComment;*/ final jsonData = await HttpUtil().post( getPushCommentApi(), data: { "sourceId": sourceId, "content": reply.content, "commentId": updatedComment?.commentId, "replyId": "", }, ); if (jsonData != null ){ showToast("评论成功"); } } String getPushCommentApi() { switch (_commentType) { case CommentType.video: return apiVideoCommentPublish; case CommentType.topic: return apiTopicCommentPublish; case CommentType.activity || CommentType.activityVideo: return apiActivityCommentPublish; case CommentType.news: return apiNewsCommentPublish; } } String getCommentListApi() { switch (_commentType) { case CommentType.video: return apiVideoCommentList; case CommentType.topic: return apiTopicCommentList; case CommentType.activity || CommentType.activityVideo: return apiActivityCommentList; case CommentType.news: return apiNewsCommentList; } } String getKey() { switch (_commentType) { case CommentType.video: return "videoId"; case CommentType.topic: return "articleId"; case CommentType.activity || CommentType.activityVideo: return "contentId"; case CommentType.news: return "contentId"; } } Future fetchComment({ required int page, required String articleId, }) async { var jsonData = await HttpUtil().get( getCommentListApi(), queryParameters: { getKey(): articleId, "pn": page.toString(), "ps": 10.toString(), }, //"665168438702149" ); final response = ModelParser.parseObject( jsonData, NewCommentModel.fromJson, ); state = response; } }