1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +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"], linkDescription: map["link_description"],
linkImage: map["link_image"], linkImage: map["link_image"],
likes: map["likes"], likes: map["likes"],
userLikes: map["userlike"], userLike: map["userlike"],
access: _APIUserAccessMap[map["user_access"]], access: _APIUserAccessMap[map["user_access"]],
comments: comments, comments: comments,
); );

View File

@ -1,3 +1,4 @@
import 'package:comunic/models/like_element.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
/// Comments /// Comments
@ -6,7 +7,7 @@ import 'package:meta/meta.dart';
/// ///
/// @author Pierre HUBERT /// @author Pierre HUBERT
class Comment { class Comment implements LikeElement {
final int id; final int id;
final int userID; final int userID;
final int postID; 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/post_visibility_level.dart';
import 'package:comunic/enums/user_access_levels.dart'; import 'package:comunic/enums/user_access_levels.dart';
import 'package:comunic/lists/comments_list.dart'; import 'package:comunic/lists/comments_list.dart';
import 'package:comunic/models/like_element.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
/// Single post information /// Single post information
/// ///
/// @author Pierre HUBERT /// @author Pierre HUBERT
class Post { class Post implements LikeElement {
final int id; final int id;
final int userID; final int userID;
final int userPageID; final int userPageID;
@ -27,7 +28,7 @@ class Post {
final String linkDescription; final String linkDescription;
final String linkImage; final String linkImage;
int likes; int likes;
bool userLikes; bool userLike;
final UserAccessLevels access; final UserAccessLevels access;
final CommentsList comments; final CommentsList comments;
@ -50,7 +51,7 @@ class Post {
@required this.linkDescription, @required this.linkDescription,
@required this.linkImage, @required this.linkImage,
@required this.likes, @required this.likes,
@required this.userLikes, @required this.userLike,
@required this.access, @required this.access,
@required this.comments @required this.comments
}) : assert(id != null), }) : assert(id != null),
@ -62,7 +63,7 @@ class Post {
assert(kind != null), assert(kind != null),
assert(kind != PostKind.COUNTDOWN || timeEnd != null), assert(kind != PostKind.COUNTDOWN || timeEnd != null),
assert(likes != null), assert(likes != null),
assert(userLikes != null), assert(userLike != null),
assert(access != null); assert(access != null);
bool get hasContent => content != 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/helpers/likes_helper.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/comment.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/new_comment.dart';
import 'package:comunic/models/post.dart'; import 'package:comunic/models/post.dart';
import 'package:comunic/models/user.dart'; import 'package:comunic/models/user.dart';
@ -126,7 +127,7 @@ class _PostTileState extends State<PostTile> {
// Like button // Like button
Center( Center(
child: InkWell( child: InkWell(
onTap: () => _updateLike(), onTap: () => _updatePostLike(),
child: Row( child: Row(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@ -135,7 +136,7 @@ class _PostTileState extends State<PostTile> {
padding: const EdgeInsets.only(left: 8.0, right: 8.0), padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: Icon( child: Icon(
Icons.thumb_up, Icons.thumb_up,
color: widget.post.userLikes ? Colors.blue : null, color: widget.post.userLike ? Colors.blue : null,
), ),
), ),
Text(widget.post.likes < 2 Text(widget.post.likes < 2
@ -339,34 +340,28 @@ class _PostTileState extends State<PostTile> {
} }
/// Update like status /// Update like status
Future<void> _updateLike() async { Future<void> _updatePostLike() async {
// Update liking status _updateElementLike(LikesType.POST, widget.post);
_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--;
});
} }
/// Invert comment like status /// Invert comment like status
Future<void> _updateCommentLike(Comment comment) async { 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 // Update liking status
_likesHelper.setLiking( _likesHelper.setLiking(
type: LikesType.COMMENT, type: type,
like: !comment.userLike, like: !element.userLike,
id: comment.id, id: element.id,
); );
// Save new like status // Save new like status
setState(() { setState(() {
comment.userLike = !comment.userLike; element.userLike = !element.userLike;
comment.userLike ? comment.likes++ : comment.likes--; element.userLike ? element.likes++ : element.likes--;
}); });
} }
} }