From 64958bb3910d63a33931e5304b493e5df0c802d6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 18 May 2019 16:04:21 +0200 Subject: [PATCH] Simplify like update code --- lib/helpers/posts_helper.dart | 2 +- lib/models/comment.dart | 3 ++- lib/models/like_element.dart | 11 +++++++++++ lib/models/post.dart | 9 +++++---- lib/ui/tiles/post_tile.dart | 35 +++++++++++++++-------------------- 5 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 lib/models/like_element.dart diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index 94e482d..dbb9293 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -85,7 +85,7 @@ class PostsHelper { linkDescription: map["link_description"], linkImage: map["link_image"], likes: map["likes"], - userLikes: map["userlike"], + userLike: map["userlike"], access: _APIUserAccessMap[map["user_access"]], comments: comments, ); diff --git a/lib/models/comment.dart b/lib/models/comment.dart index f44dc33..688be60 100644 --- a/lib/models/comment.dart +++ b/lib/models/comment.dart @@ -1,3 +1,4 @@ +import 'package:comunic/models/like_element.dart'; import 'package:meta/meta.dart'; /// Comments @@ -6,7 +7,7 @@ import 'package:meta/meta.dart'; /// /// @author Pierre HUBERT -class Comment { +class Comment implements LikeElement { final int id; final int userID; final int postID; diff --git a/lib/models/like_element.dart b/lib/models/like_element.dart new file mode 100644 index 0000000..2db9f7b --- /dev/null +++ b/lib/models/like_element.dart @@ -0,0 +1,11 @@ +/// Element that can be liked by the user +/// +/// @author Pierre HUBERT + +abstract class LikeElement { + + int get id; + bool userLike; + int likes; + +} \ No newline at end of file diff --git a/lib/models/post.dart b/lib/models/post.dart index 8afc0fd..08bb6ac 100644 --- a/lib/models/post.dart +++ b/lib/models/post.dart @@ -2,13 +2,14 @@ import 'package:comunic/enums/post_kind.dart'; import 'package:comunic/enums/post_visibility_level.dart'; import 'package:comunic/enums/user_access_levels.dart'; import 'package:comunic/lists/comments_list.dart'; +import 'package:comunic/models/like_element.dart'; import 'package:meta/meta.dart'; /// Single post information /// /// @author Pierre HUBERT -class Post { +class Post implements LikeElement { final int id; final int userID; final int userPageID; @@ -27,7 +28,7 @@ class Post { final String linkDescription; final String linkImage; int likes; - bool userLikes; + bool userLike; final UserAccessLevels access; final CommentsList comments; @@ -50,7 +51,7 @@ class Post { @required this.linkDescription, @required this.linkImage, @required this.likes, - @required this.userLikes, + @required this.userLike, @required this.access, @required this.comments }) : assert(id != null), @@ -62,7 +63,7 @@ class Post { assert(kind != null), assert(kind != PostKind.COUNTDOWN || timeEnd != null), assert(likes != null), - assert(userLikes != null), + assert(userLike != null), assert(access != null); bool get hasContent => content != null; diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 5b8b7ab..afa7be8 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -6,6 +6,7 @@ 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/like_element.dart'; import 'package:comunic/models/new_comment.dart'; import 'package:comunic/models/post.dart'; import 'package:comunic/models/user.dart'; @@ -126,7 +127,7 @@ class _PostTileState extends State { // Like button Center( child: InkWell( - onTap: () => _updateLike(), + onTap: () => _updatePostLike(), child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, @@ -135,7 +136,7 @@ class _PostTileState extends State { padding: const EdgeInsets.only(left: 8.0, right: 8.0), child: Icon( Icons.thumb_up, - color: widget.post.userLikes ? Colors.blue : null, + color: widget.post.userLike ? Colors.blue : null, ), ), Text(widget.post.likes < 2 @@ -339,34 +340,28 @@ class _PostTileState extends State { } /// Update like status - Future _updateLike() async { - // Update liking status - _likesHelper.setLiking( - type: LikesType.POST, - like: !widget.post.userLikes, - id: widget.post.id, - ); - - // Save new like status - setState(() { - widget.post.userLikes = !widget.post.userLikes; - widget.post.userLikes ? widget.post.likes++ : widget.post.likes--; - }); + Future _updatePostLike() async { + _updateElementLike(LikesType.POST, widget.post); } /// Invert comment like status Future _updateCommentLike(Comment comment) async { + _updateElementLike(LikesType.COMMENT, comment); + } + + /// Invert element like status + Future _updateElementLike(LikesType type, LikeElement element) async { // Update liking status _likesHelper.setLiking( - type: LikesType.COMMENT, - like: !comment.userLike, - id: comment.id, + type: type, + like: !element.userLike, + id: element.id, ); // Save new like status setState(() { - comment.userLike = !comment.userLike; - comment.userLike ? comment.likes++ : comment.likes--; + element.userLike = !element.userLike; + element.userLike ? element.likes++ : element.likes--; }); } }