diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 6a04f5e..4a934ba 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -1,7 +1,9 @@ import 'dart:io'; +import 'package:comunic/enums/likes_type.dart'; 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/new_comment.dart'; import 'package:comunic/models/post.dart'; @@ -28,16 +30,13 @@ const TextStyle _userNameStyle = TextStyle( class PostTile extends StatefulWidget { final Post post; final UsersList usersInfo; - final void Function(Post) onTapLike; - const PostTile( - {Key key, - @required this.post, - @required this.usersInfo, - @required this.onTapLike}) - : assert(post != null), + const PostTile({ + Key key, + @required this.post, + @required this.usersInfo, + }) : assert(post != null), assert(usersInfo != null), - assert(onTapLike != null), super(key: key); @override @@ -46,6 +45,7 @@ class PostTile extends StatefulWidget { class _PostTileState extends State { // Helpers + final _likesHelper = LikesHelper(); final _commentsHelper = CommentsHelper(); // Class members @@ -125,7 +125,7 @@ class _PostTileState extends State { // Like button Center( child: InkWell( - onTap: () => widget.onTapLike(widget.post), + onTap: () => _updateLike(), child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, @@ -250,7 +250,7 @@ class _PostTileState extends State { width: 30, child: FlatButton( padding: EdgeInsets.only(), - onPressed: _pickImage, + onPressed: _pickImageForComment, child: Icon( Icons.image, color: _hasImage ? Colors.blue : Colors.grey, @@ -284,7 +284,7 @@ class _PostTileState extends State { } /// Pick an image - Future _pickImage() async { + Future _pickImageForComment() async { // Ask the user to confirm image removal if there is already one selected if (_hasImage) { if (await askUserConfirmation( @@ -335,4 +335,20 @@ class _PostTileState extends State { widget.post.comments.add(newComment); }); } + + /// 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--; + }); + } } diff --git a/lib/ui/widgets/posts_list_widget.dart b/lib/ui/widgets/posts_list_widget.dart index 4386bb8..b5bd2af 100644 --- a/lib/ui/widgets/posts_list_widget.dart +++ b/lib/ui/widgets/posts_list_widget.dart @@ -1,9 +1,6 @@ -import 'package:comunic/enums/likes_type.dart'; -import 'package:comunic/helpers/likes_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/posts_list.dart'; import 'package:comunic/lists/users_list.dart'; -import 'package:comunic/models/post.dart'; import 'package:comunic/ui/screens/conversation_screen.dart'; import 'package:comunic/ui/tiles/post_tile.dart'; import 'package:comunic/utils/intl_utils.dart'; @@ -31,7 +28,6 @@ class PostsListWidget extends StatefulWidget { class _PostsListWidgetState extends State { // Helpers - final LikesHelper _likesHelper = LikesHelper(); final UsersHelper _usersHelper = UsersHelper(); // Class members @@ -77,7 +73,6 @@ class _PostsListWidgetState extends State { itemBuilder: (c, i) => PostTile( post: _list[i], usersInfo: _users, - onTapLike: _onUpdateLike, ), ); } @@ -88,20 +83,4 @@ class _PostsListWidgetState extends State { if (_list == null) return buildCenteredProgressBar(); return _buildListView(); } - - /// Update like status - Future _onUpdateLike(Post post) async { - // Update liking status - _likesHelper.setLiking( - type: LikesType.POST, - like: !post.userLikes, - id: post.id, - ); - - // Save new like status - setState(() { - post.userLikes = !post.userLikes; - post.userLikes ? post.likes++ : post.likes--; - }); - } }