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