From 9290617cd54a046d10cf5fc9ed7ce1c7dea4acf6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 May 2019 15:54:10 +0200 Subject: [PATCH] Can update like status --- lib/models/comment.dart | 4 +-- lib/ui/tiles/comment_tile.dart | 57 +++++++++++++++++++++++++++++++--- lib/ui/tiles/post_tile.dart | 18 +++++++++++ 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/lib/models/comment.dart b/lib/models/comment.dart index acfd733..f44dc33 100644 --- a/lib/models/comment.dart +++ b/lib/models/comment.dart @@ -13,8 +13,8 @@ class Comment { final int timeSent; final String content; final String imageURL; - final int likes; - final bool userLike; + int likes; + bool userLike; Comment({ @required this.id, diff --git a/lib/ui/tiles/comment_tile.dart b/lib/ui/tiles/comment_tile.dart index 42f6511..a5770f3 100644 --- a/lib/ui/tiles/comment_tile.dart +++ b/lib/ui/tiles/comment_tile.dart @@ -2,6 +2,7 @@ import 'package:comunic/models/comment.dart'; import 'package:comunic/models/user.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart'; +import 'package:comunic/utils/intl_utils.dart'; import 'package:flutter/material.dart'; /// Single comment tile @@ -11,10 +12,16 @@ import 'package:flutter/material.dart'; class CommentTile extends StatelessWidget { final Comment comment; final User user; + final void Function(Comment) onUpdateLike; - const CommentTile({Key key, this.comment, this.user}) - : assert(comment != null), + const CommentTile({ + Key key, + @required this.comment, + @required this.user, + @required this.onUpdateLike, + }) : assert(comment != null), assert(user != null), + assert(onUpdateLike != null), super(key: key); @override @@ -23,7 +30,9 @@ class CommentTile extends StatelessWidget { leading: AccountImageWidget( user: user, ), - title: Text(user.displayName,), + title: Text( + user.displayName, + ), subtitle: _buildCommentContent(), ); } @@ -39,15 +48,55 @@ class CommentTile extends StatelessWidget { url: comment.imageURL, allowFullScreen: true, height: 100.0, + width: null, ) : null, ), // Comment text Container( - child: comment.hasContent ? Text(comment.content,) : null, + child: comment.hasContent + ? Text( + comment.content, + ) + : null, ), + + // Comment likes + _buildLikeButton(), ], ); } + + String get _likeString { + if (comment.likes == 0) return tr("Like"); + if (comment.likes == 1) + return tr("1 Like"); + else + return tr("%num% likes", args: {"num": comment.likes.toString()}); + } + + /// Build like button associated to this post + Widget _buildLikeButton() { + return Align( + alignment: AlignmentDirectional.topStart, + child: FlatButton( + padding: EdgeInsets.only(left: 0.0), + onPressed: () => onUpdateLike(comment), + child: Row( + children: [ + Icon( + Icons.thumb_up, + color: comment.userLike ? Colors.blue : null, + size: 15.0, + ), + SizedBox( + width: 8.0, + ), + Text(_likeString), + ], + ), + ), + ); + } } diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 4a934ba..5b8b7ab 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -5,6 +5,7 @@ import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/helpers/comments_helper.dart'; import 'package:comunic/helpers/likes_helper.dart'; import 'package:comunic/lists/users_list.dart'; +import 'package:comunic/models/comment.dart'; import 'package:comunic/models/new_comment.dart'; import 'package:comunic/models/post.dart'; import 'package:comunic/models/user.dart'; @@ -192,6 +193,7 @@ class _PostTileState extends State { (num) => CommentTile( comment: widget.post.comments[num], user: widget.usersInfo.getUser(widget.post.comments[num].userID), + onUpdateLike: _updateCommentLike, ), ); @@ -351,4 +353,20 @@ class _PostTileState extends State { widget.post.userLikes ? widget.post.likes++ : widget.post.likes--; }); } + + /// Invert comment like status + Future _updateCommentLike(Comment comment) async { + // Update liking status + _likesHelper.setLiking( + type: LikesType.COMMENT, + like: !comment.userLike, + id: comment.id, + ); + + // Save new like status + setState(() { + comment.userLike = !comment.userLike; + comment.userLike ? comment.likes++ : comment.likes--; + }); + } }