From c8d40adec3a852d2e1ee1f42736dfe74faf52baa Mon Sep 17 00:00:00 2001 From: MohammadTaha Basiri Date: Thu, 31 Mar 2022 00:55:36 +0430 Subject: [PATCH] comments bug fixed --- lib/models/comment/comment.dart | 6 ++-- lib/models/comment/feedback.dart | 6 ++-- lib/providers/user.dart | 3 +- lib/views/home/comments/comments.dart | 4 +++ lib/views/home/comments/comments_state.dart | 29 +++++++++++++-- .../home/comments/widgets/comment_item.dart | 35 ++++++++++++------- 6 files changed, 59 insertions(+), 24 deletions(-) diff --git a/lib/models/comment/comment.dart b/lib/models/comment/comment.dart index b4f43ea..0d58166 100644 --- a/lib/models/comment/comment.dart +++ b/lib/models/comment/comment.dart @@ -6,9 +6,9 @@ class CommentData { int id; final String text; final String createdAt; - final bool liked; - final bool disliked; - FeedbackData feedback; + bool liked; + bool disliked; + final FeedbackData feedback; final UserOverview user; final List replies; diff --git a/lib/models/comment/feedback.dart b/lib/models/comment/feedback.dart index d51d67f..31696ba 100644 --- a/lib/models/comment/feedback.dart +++ b/lib/models/comment/feedback.dart @@ -1,8 +1,8 @@ class FeedbackData { - final int like; - final int dislike; + int like; + int dislike; - const FeedbackData({required this.like, required this.dislike}); + FeedbackData({required this.like, required this.dislike}); factory FeedbackData.fromJson(Map json) => FeedbackData( like: json['like'], diff --git a/lib/providers/user.dart b/lib/providers/user.dart index ef44802..ec703e1 100644 --- a/lib/providers/user.dart +++ b/lib/providers/user.dart @@ -44,9 +44,8 @@ class UserProvider extends CoreProvier { Future _registerFirebaseToken() async { final service = RequestService(RequestHelper.firebaseToken, body: { - 'firebaseToken': AppInitializer.fcmToken, + 'token': AppInitializer.fcmToken, }); - await service.put(); } diff --git a/lib/views/home/comments/comments.dart b/lib/views/home/comments/comments.dart index d4c36aa..6e03d73 100644 --- a/lib/views/home/comments/comments.dart +++ b/lib/views/home/comments/comments.dart @@ -85,6 +85,10 @@ class _CommentsState extends State { title: 'اولین نظر را بنویسید...', ), builder: (context, state, index) => Comment( + key: ValueKey( + state.comments[index].id.toString() + + state.comments[index].text, + ), focusNode: _focusNode, comment: state.comments[index], ), diff --git a/lib/views/home/comments/comments_state.dart b/lib/views/home/comments/comments_state.dart index e93f04b..4b02d00 100644 --- a/lib/views/home/comments/comments_state.dart +++ b/lib/views/home/comments/comments_state.dart @@ -44,8 +44,31 @@ class CommentsState extends CoreProvier { appState = AppState.failed; } - Future feedback(int id, bool like, bool dislike) async { + Future feedback({ + required int id, + required bool like, + required bool dislike, + required int likeCount, + required int dislikeCount, + int? replyId, + }) async { _feedbackQueue.addAll({id: MapEntry(like, dislike)}); + dynamic comment; + if (replyId == null) { + comment = comments.firstWhere((comment) => comment.id == id); + } else { + comment = comments + .firstWhere((comment) => comment.id == id) + .replies + .firstWhere((element) => element.id == replyId); + } + + if (comment != null) { + comment.feedback.like = likeCount; + comment.feedback.dislike = dislikeCount; + comment.disliked = dislike; + comment.liked = like; + } Future.delayed(const Duration(milliseconds: 500), () async { if (!_feedbackQueue.containsKey(id)) return; final service = RequestService( @@ -70,7 +93,7 @@ class CommentsState extends CoreProvier { createdAt: DateTime.now().toString(), liked: false, disliked: false, - feedback: const FeedbackData(like: 0, dislike: 0), + feedback: FeedbackData(like: 0, dislike: 0), toUser: replyingTo!, user: UserOverview( id: user.id, @@ -88,7 +111,7 @@ class CommentsState extends CoreProvier { createdAt: DateTime.now().toString(), liked: false, disliked: false, - feedback: const FeedbackData(like: 0, dislike: 0), + feedback: FeedbackData(like: 0, dislike: 0), user: UserOverview( id: user.id, fullName: user.fullName, diff --git a/lib/views/home/comments/widgets/comment_item.dart b/lib/views/home/comments/widgets/comment_item.dart index 62fc0a2..4748031 100644 --- a/lib/views/home/comments/widgets/comment_item.dart +++ b/lib/views/home/comments/widgets/comment_item.dart @@ -49,7 +49,7 @@ class CommentState extends State { duration: DesignConfig.lowAnimationDuration, isVisible: _showSubComments, child: _commentBuilder( - isSubComment: true, + isReply: true, comment: _comment.replies[i], ), ), @@ -57,11 +57,10 @@ class CommentState extends State { ); } - Widget _commentBuilder({required comment, bool isSubComment = false}) => - Container( + Widget _commentBuilder({required comment, bool isReply = false}) => Container( decoration: BoxDecoration( border: Border( - right: isSubComment + right: isReply ? BorderSide(color: Theme.of(context).colorScheme.caption) : BorderSide.none, ), @@ -69,7 +68,7 @@ class CommentState extends State { child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ - if (isSubComment) const SizedBox(width: 12), + if (isReply) const SizedBox(width: 12), if (comment.user.photo == null) const Icon(DidvanIcons.avatar_light), if (comment.user.photo != null) @@ -99,7 +98,7 @@ class CommentState extends State { ], ), const SizedBox(height: 8), - if (isSubComment) + if (isReply) DidvanText( 'پاسخ به ${comment.toUser.fullName}', style: Theme.of(context).textTheme.caption, @@ -124,8 +123,8 @@ class CommentState extends State { color: Theme.of(context).colorScheme.primary, ), ), - if (!isSubComment) const SizedBox(width: 20), - if (!isSubComment && comment.replies.isNotEmpty) + if (!isReply) const SizedBox(width: 20), + if (!isReply && comment.replies.isNotEmpty) InkWrapper( onPressed: () => setState( () => _showSubComments = !_showSubComments, @@ -154,8 +153,15 @@ class CommentState extends State { dislikeCount: comment.feedback.dislike, likeValue: comment.liked, dislikeValue: comment.disliked, - onFeedback: (like, dislike) => - state.feedback(comment.id, like, dislike), + onFeedback: (like, dislike, likeCount, dislikeCount) => + state.feedback( + id: _comment.id, + like: like, + dislike: dislike, + likeCount: likeCount, + dislikeCount: dislikeCount, + replyId: isReply ? comment.id : null, + ), ), ], ), @@ -172,7 +178,8 @@ class _FeedbackButtons extends StatefulWidget { final int dislikeCount; final bool likeValue; final bool dislikeValue; - final void Function(bool like, bool dislike) onFeedback; + final void Function(bool like, bool dislike, int likeCount, int dislikeCount) + onFeedback; const _FeedbackButtons({ Key? key, required this.onFeedback, @@ -228,7 +235,8 @@ class _FeedbackButtonsState extends State<_FeedbackButtons> { } _likeValue = !_likeValue; }); - widget.onFeedback(_likeValue, _dislikeValue); + widget.onFeedback( + _likeValue, _dislikeValue, _likeCount, _dislikeCount); }, ), const SizedBox(width: 16), @@ -257,7 +265,8 @@ class _FeedbackButtonsState extends State<_FeedbackButtons> { } _dislikeValue = !_dislikeValue; }); - widget.onFeedback(_likeValue, _dislikeValue); + widget.onFeedback( + _likeValue, _dislikeValue, _likeCount, _dislikeCount); }, ), ],