diff --git a/lib/views/home/comments/comments_state.dart b/lib/views/home/comments/comments_state.dart index a817171..4a0594c 100644 --- a/lib/views/home/comments/comments_state.dart +++ b/lib/views/home/comments/comments_state.dart @@ -32,6 +32,7 @@ class CommentsState extends CoreProvier { ); await service.httpGet(); if (service.isSuccess) { + comments.clear(); final messages = service.result['comments']; for (var i = 0; i < messages.length; i++) { comments.add(CommentData.fromJson(messages[i])); @@ -126,9 +127,6 @@ class CommentsState extends CoreProvier { ); } - _count++; - onCommentsChanged(_count); - final body = {}; if (commentId != null) { @@ -175,36 +173,35 @@ class CommentsState extends CoreProvier { ); } - void deleteComment(int id, int? rootId) { + void deleteComment(int id, int status, int? rootId) async { final service = RequestService(RequestHelper.deleteComment(id)); service.delete(); if (rootId == null) { final comment = comments.firstWhere((element) => element.id == id); if (comment.replies.isNotEmpty) { - comments.insertAll( - comments.indexOf(comment), - comment.replies.map( - (rep) => CommentData( - id: rep.id, - text: rep.text, - createdAt: rep.createdAt, - liked: rep.liked, - disliked: rep.disliked, - feedback: rep.feedback, - user: rep.user, - replies: [], - status: 2, - ), - ), - ); + _count = 0; + await getComments(); + onCommentsChanged(_count); + } else { + comments.remove(comment); + + if (status != 2) { + _count--; + onCommentsChanged(_count); + } } - comments.remove(comment); } else { comments .firstWhere((element) => element.id == rootId) .replies .removeWhere((element) => element.id == id); + + if (status != 2) { + _count--; + onCommentsChanged(_count); + } } + notifyListeners(); } } diff --git a/lib/views/home/comments/widgets/comment.dart b/lib/views/home/comments/widgets/comment.dart index 8ca602c..44ee164 100644 --- a/lib/views/home/comments/widgets/comment.dart +++ b/lib/views/home/comments/widgets/comment.dart @@ -141,64 +141,67 @@ class CommentState extends State { ], ), const SizedBox(height: 8), - Row( - children: [ - InkWrapper( - onPressed: () { - state.commentId = _comment.id; - state.replyingTo = comment.user; - state.showReplyBox = true; - state.update(); - widget.focusNode.requestFocus(); - }, - child: DidvanText( - 'پاسخ', - style: Theme.of(context).textTheme.bodyLarge, - color: Theme.of(context).colorScheme.primary, - ), - ), - if (!isReply) const SizedBox(width: 20), - if (!isReply && comment.replies.isNotEmpty) + if (_comment.status != 2) + Row( + children: [ InkWrapper( - onPressed: () => setState( - () => _showSubComments = !_showSubComments, + onPressed: () { + state.commentId = _comment.id; + state.replyingTo = comment.user; + state.showReplyBox = true; + state.update(); + widget.focusNode.requestFocus(); + }, + child: DidvanText( + 'پاسخ', + style: Theme.of(context).textTheme.bodyLarge, + color: Theme.of(context).colorScheme.primary, ), - child: Row( - children: [ - DidvanText( - 'پاسخ‌ها(${comment.replies.length})', - style: Theme.of(context).textTheme.bodyLarge, - color: Theme.of(context).colorScheme.primary, - ), - AnimatedRotation( - duration: DesignConfig.lowAnimationDuration, - turns: _showSubComments ? 0.5 : 0, - child: Icon( - DidvanIcons.angle_down_regular, + ), + if (!isReply) const SizedBox(width: 20), + if (!isReply && comment.replies.isNotEmpty) + InkWrapper( + onPressed: () => setState( + () => _showSubComments = !_showSubComments, + ), + child: Row( + children: [ + DidvanText( + 'پاسخ‌ها(${comment.replies.length})', + style: Theme.of(context).textTheme.bodyLarge, color: Theme.of(context).colorScheme.primary, ), - ), - ], + AnimatedRotation( + duration: DesignConfig.lowAnimationDuration, + turns: _showSubComments ? 0.5 : 0, + child: Icon( + DidvanIcons.angle_down_regular, + color: + Theme.of(context).colorScheme.primary, + ), + ), + ], + ), + ), + const Spacer(), + _FeedbackButtons( + likeCount: comment.feedback.like, + dislikeCount: comment.feedback.dislike, + likeValue: comment.liked, + dislikeValue: comment.disliked, + onFeedback: + (like, dislike, likeCount, dislikeCount) => + state.feedback( + id: _comment.id, + like: like, + dislike: dislike, + likeCount: likeCount, + dislikeCount: dislikeCount, + replyId: isReply ? comment.id : null, ), ), - const Spacer(), - _FeedbackButtons( - likeCount: comment.feedback.like, - dislikeCount: comment.feedback.dislike, - likeValue: comment.liked, - dislikeValue: comment.disliked, - onFeedback: (like, dislike, likeCount, dislikeCount) => - state.feedback( - id: _comment.id, - like: like, - dislike: dislike, - likeCount: likeCount, - dislikeCount: dislikeCount, - replyId: isReply ? comment.id : null, - ), - ), - ], - ), + ], + ), ], ), ), @@ -228,6 +231,7 @@ class CommentState extends State { onTap: () { state.deleteComment( comment.id, + comment.status, comment.runtimeType == Reply ? _comment.id : null, ); ActionSheetUtils.pop();