1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 12:58:51 +00:00

Simplify like update code

This commit is contained in:
Pierre HUBERT 2019-05-18 16:04:21 +02:00
parent 9290617cd5
commit 64958bb391
5 changed files with 34 additions and 26 deletions

View File

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

View File

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

View File

@ -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;
}

View File

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

View File

@ -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<PostTile> {
// 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<PostTile> {
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<PostTile> {
}
/// 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--;
});
Future<void> _updatePostLike() async {
_updateElementLike(LikesType.POST, widget.post);
}
/// Invert comment like status
Future<void> _updateCommentLike(Comment comment) async {
_updateElementLike(LikesType.COMMENT, comment);
}
/// Invert element like status
Future<void> _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--;
});
}
}