| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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<NewCommentModel, CommentType> {
- late final CommentType _commentType;
- //type 1为视频评论,2为话题评论 3为活动评论 4.新闻评论
- @override
- NewCommentModel build(CommentType commentType) {
- _commentType = commentType;
- return NewCommentModel();
- }
- Future<void> 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<void> 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<NewCommentModel>(
- jsonData,
- NewCommentModel.fromJson,
- );
- state = response;
- }
- }
|