fixed bugs in the comments section

This commit is contained in:
Amir Hossein Mousavi 2023-09-05 16:40:20 +03:30
parent c94f043f3f
commit 4922f517ad
2 changed files with 74 additions and 73 deletions

View File

@ -32,6 +32,7 @@ class CommentsState extends CoreProvier {
); );
await service.httpGet(); await service.httpGet();
if (service.isSuccess) { if (service.isSuccess) {
comments.clear();
final messages = service.result['comments']; final messages = service.result['comments'];
for (var i = 0; i < messages.length; i++) { for (var i = 0; i < messages.length; i++) {
comments.add(CommentData.fromJson(messages[i])); comments.add(CommentData.fromJson(messages[i]));
@ -126,9 +127,6 @@ class CommentsState extends CoreProvier {
); );
} }
_count++;
onCommentsChanged(_count);
final body = {}; final body = {};
if (commentId != null) { 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)); final service = RequestService(RequestHelper.deleteComment(id));
service.delete(); service.delete();
if (rootId == null) { if (rootId == null) {
final comment = comments.firstWhere((element) => element.id == id); final comment = comments.firstWhere((element) => element.id == id);
if (comment.replies.isNotEmpty) { if (comment.replies.isNotEmpty) {
comments.insertAll( _count = 0;
comments.indexOf(comment), await getComments();
comment.replies.map( onCommentsChanged(_count);
(rep) => CommentData( } else {
id: rep.id, comments.remove(comment);
text: rep.text,
createdAt: rep.createdAt, if (status != 2) {
liked: rep.liked, _count--;
disliked: rep.disliked, onCommentsChanged(_count);
feedback: rep.feedback, }
user: rep.user,
replies: [],
status: 2,
),
),
);
} }
comments.remove(comment);
} else { } else {
comments comments
.firstWhere((element) => element.id == rootId) .firstWhere((element) => element.id == rootId)
.replies .replies
.removeWhere((element) => element.id == id); .removeWhere((element) => element.id == id);
if (status != 2) {
_count--;
onCommentsChanged(_count);
}
} }
notifyListeners(); notifyListeners();
} }
} }

View File

@ -141,64 +141,67 @@ class CommentState extends State<Comment> {
], ],
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
Row( if (_comment.status != 2)
children: [ Row(
InkWrapper( children: [
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)
InkWrapper( InkWrapper(
onPressed: () => setState( onPressed: () {
() => _showSubComments = !_showSubComments, 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: [ if (!isReply) const SizedBox(width: 20),
DidvanText( if (!isReply && comment.replies.isNotEmpty)
'پاسخ‌ها(${comment.replies.length})', InkWrapper(
style: Theme.of(context).textTheme.bodyLarge, onPressed: () => setState(
color: Theme.of(context).colorScheme.primary, () => _showSubComments = !_showSubComments,
), ),
AnimatedRotation( child: Row(
duration: DesignConfig.lowAnimationDuration, children: [
turns: _showSubComments ? 0.5 : 0, DidvanText(
child: Icon( 'پاسخ‌ها(${comment.replies.length})',
DidvanIcons.angle_down_regular, style: Theme.of(context).textTheme.bodyLarge,
color: Theme.of(context).colorScheme.primary, 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<Comment> {
onTap: () { onTap: () {
state.deleteComment( state.deleteComment(
comment.id, comment.id,
comment.status,
comment.runtimeType == Reply ? _comment.id : null, comment.runtimeType == Reply ? _comment.id : null,
); );
ActionSheetUtils.pop(); ActionSheetUtils.pop();