1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Make PostTile hosts all actions on posts

This commit is contained in:
Pierre HUBERT 2019-05-18 15:11:31 +02:00
parent 754745fd5f
commit 2e4d3119f8
2 changed files with 27 additions and 32 deletions

View File

@ -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,
const PostTile({
Key key,
@required this.post,
@required this.usersInfo,
@required this.onTapLike})
: assert(post != null),
}) : 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<PostTile> {
// Helpers
final _likesHelper = LikesHelper();
final _commentsHelper = CommentsHelper();
// Class members
@ -125,7 +125,7 @@ class _PostTileState extends State<PostTile> {
// 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<PostTile> {
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<PostTile> {
}
/// Pick an image
Future<void> _pickImage() async {
Future<void> _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<PostTile> {
widget.post.comments.add(newComment);
});
}
/// Update like status
Future<void> _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--;
});
}
}

View File

@ -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<PostsListWidget> {
// Helpers
final LikesHelper _likesHelper = LikesHelper();
final UsersHelper _usersHelper = UsersHelper();
// Class members
@ -77,7 +73,6 @@ class _PostsListWidgetState extends State<PostsListWidget> {
itemBuilder: (c, i) => PostTile(
post: _list[i],
usersInfo: _users,
onTapLike: _onUpdateLike,
),
);
}
@ -88,20 +83,4 @@ class _PostsListWidgetState extends State<PostsListWidget> {
if (_list == null) return buildCenteredProgressBar();
return _buildListView();
}
/// Update like status
Future<void> _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--;
});
}
}