mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-12-26 04:48:51 +00:00
Simplify use of like widget
This commit is contained in:
parent
2d26395d73
commit
dde909457d
@ -1,3 +1,4 @@
|
||||
import 'package:comunic/enums/likes_type.dart';
|
||||
import 'package:comunic/models/like_element.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:comunic/utils/account_utils.dart' as account;
|
||||
@ -40,4 +41,7 @@ class Comment implements LikeElement {
|
||||
bool get hasImage => imageURL != null;
|
||||
|
||||
bool get isOwner => userID == account.userID();
|
||||
|
||||
@override
|
||||
LikesType get likeType => LikesType.COMMENT;
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
import 'package:comunic/enums/likes_type.dart';
|
||||
|
||||
/// Element that can be liked by the user
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
abstract class LikeElement {
|
||||
LikesType get likeType;
|
||||
|
||||
int get id;
|
||||
|
||||
bool userLike;
|
||||
int likes;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import 'package:comunic/enums/likes_type.dart';
|
||||
import 'package:comunic/enums/post_kind.dart';
|
||||
import 'package:comunic/enums/post_visibility_level.dart';
|
||||
import 'package:comunic/enums/user_access_levels.dart';
|
||||
@ -83,4 +84,7 @@ class Post implements LikeElement {
|
||||
bool get canDelete =>
|
||||
access == UserAccessLevels.FULL ||
|
||||
access == UserAccessLevels.INTERMEDIATE;
|
||||
|
||||
@override
|
||||
LikesType get likeType => LikesType.POST;
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
import 'package:comunic/enums/likes_type.dart';
|
||||
import 'package:comunic/models/comment.dart';
|
||||
import 'package:comunic/models/user.dart';
|
||||
import 'package:comunic/ui/widgets/account_image_widget.dart';
|
||||
@ -100,15 +99,7 @@ class CommentTile extends StatelessWidget {
|
||||
),
|
||||
|
||||
// Comment likes
|
||||
LikeWidget(
|
||||
likeType: LikesType.COMMENT,
|
||||
likeID: comment.id,
|
||||
likesCount: comment.likes,
|
||||
isLiking: comment.userLike,
|
||||
onUpdatedLikings: (count, isLiking) {
|
||||
comment.likes = count;
|
||||
comment.userLike = isLiking;
|
||||
}),
|
||||
LikeWidget(likeElement: comment),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:comunic/enums/likes_type.dart';
|
||||
import 'package:comunic/enums/post_kind.dart';
|
||||
import 'package:comunic/enums/post_visibility_level.dart';
|
||||
import 'package:comunic/helpers/comments_helper.dart';
|
||||
import 'package:comunic/helpers/likes_helper.dart';
|
||||
import 'package:comunic/helpers/posts_helper.dart';
|
||||
import 'package:comunic/lists/groups_list.dart';
|
||||
import 'package:comunic/lists/users_list.dart';
|
||||
@ -211,15 +209,8 @@ class _PostTileState extends State<PostTile> {
|
||||
children: <Widget>[
|
||||
// Like button
|
||||
Center(
|
||||
child: LikeWidget(
|
||||
likeType: LikesType.POST,
|
||||
likeID: widget.post.id,
|
||||
likesCount: widget.post.likes,
|
||||
isLiking: widget.post.userLike,
|
||||
onUpdatedLikings: (num, userLike) {
|
||||
widget.post.likes = num;
|
||||
widget.post.userLike = userLike;
|
||||
})),
|
||||
child: LikeWidget(likeElement: widget.post),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import 'package:comunic/enums/likes_type.dart';
|
||||
import 'package:comunic/helpers/likes_helper.dart';
|
||||
import 'package:comunic/models/like_element.dart';
|
||||
import 'package:comunic/ui/widgets/safe_state.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@ -14,24 +14,12 @@ import 'package:flutter/material.dart';
|
||||
typedef UpdatedLikingCallBack = Function(int, bool);
|
||||
|
||||
class LikeWidget extends StatefulWidget {
|
||||
final LikesType likeType;
|
||||
final int likeID;
|
||||
int likesCount;
|
||||
bool isLiking;
|
||||
final UpdatedLikingCallBack onUpdatedLikings;
|
||||
final LikeElement likeElement;
|
||||
|
||||
LikeWidget({
|
||||
const LikeWidget({
|
||||
Key key,
|
||||
@required this.likeType,
|
||||
@required this.likeID,
|
||||
@required this.likesCount,
|
||||
@required this.isLiking,
|
||||
@required this.onUpdatedLikings,
|
||||
}) : assert(likeType != null),
|
||||
assert(likeID != null),
|
||||
assert(likesCount != null),
|
||||
assert(isLiking != null),
|
||||
assert(onUpdatedLikings != null),
|
||||
@required this.likeElement,
|
||||
}) : assert(likeElement != null),
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
@ -39,8 +27,10 @@ class LikeWidget extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _LikeWidgetState extends SafeState<LikeWidget> {
|
||||
LikeElement get elem => widget.likeElement;
|
||||
|
||||
String get _likeString {
|
||||
switch (widget.likesCount) {
|
||||
switch (elem.likes) {
|
||||
case 0:
|
||||
return tr("Like");
|
||||
|
||||
@ -48,7 +38,7 @@ class _LikeWidgetState extends SafeState<LikeWidget> {
|
||||
return tr("1 Like");
|
||||
|
||||
default:
|
||||
return tr("%num% likes", args: {"num": widget.likesCount.toString()});
|
||||
return tr("%num% likes", args: {"num": elem.likes.toString()});
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,7 +52,7 @@ class _LikeWidgetState extends SafeState<LikeWidget> {
|
||||
children: <Widget>[
|
||||
Icon(
|
||||
Icons.thumb_up,
|
||||
color: widget.isLiking ? Colors.blue : null,
|
||||
color: elem.userLike ? Colors.blue : null,
|
||||
size: 15.0,
|
||||
),
|
||||
SizedBox(
|
||||
@ -79,14 +69,12 @@ class _LikeWidgetState extends SafeState<LikeWidget> {
|
||||
/// Toggle like status
|
||||
void _toggleLike() async {
|
||||
// As like are not really important, we ignore failures
|
||||
if (await LikesHelper().setLiking(
|
||||
type: widget.likeType, like: !widget.isLiking, id: widget.likeID)) {
|
||||
if (await LikesHelper()
|
||||
.setLiking(type: elem.likeType, like: !elem.userLike, id: elem.id)) {
|
||||
setState(() {
|
||||
widget.isLiking = !widget.isLiking;
|
||||
elem.userLike = !elem.userLike;
|
||||
|
||||
widget.likesCount += widget.isLiking ? 1 : -1;
|
||||
|
||||
widget.onUpdatedLikings(widget.likesCount, widget.isLiking);
|
||||
elem.likes += elem.userLike ? 1 : -1;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user