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

Can update like status

This commit is contained in:
Pierre HUBERT 2019-05-18 15:54:10 +02:00
parent 2e4d3119f8
commit 9290617cd5
3 changed files with 73 additions and 6 deletions

View File

@ -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,

View File

@ -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: <Widget>[
Icon(
Icons.thumb_up,
color: comment.userLike ? Colors.blue : null,
size: 15.0,
),
SizedBox(
width: 8.0,
),
Text(_likeString),
],
),
),
);
}
}

View File

@ -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<PostTile> {
(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<PostTile> {
widget.post.userLikes ? widget.post.likes++ : widget.post.likes--;
});
}
/// Invert comment like status
Future<void> _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--;
});
}
}