1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-24 22:09:21 +00:00

Can like a text

This commit is contained in:
Pierre HUBERT 2019-05-11 15:35:07 +02:00
parent 315c61a212
commit be53a73d9f
6 changed files with 197 additions and 62 deletions

View File

@ -0,0 +1,5 @@
/// Likes types
///
/// @author Pierre HUBERT
enum LikesType { USER, POST, COMMENT, GROUP }

View File

@ -0,0 +1,35 @@
import 'package:comunic/enums/likes_type.dart';
import 'package:comunic/models/api_request.dart';
import 'package:meta/meta.dart';
/// Likes helper
///
/// @author Pierre HUBERT
const LikesAPIMap = {
LikesType.USER: "user",
LikesType.POST: "post",
LikesType.COMMENT: "comment",
LikesType.GROUP: "group",
};
class LikesHelper {
/// Update liking status of an element
Future<bool> setLiking({
@required LikesType type,
@required bool like,
@required int id,
}) async {
return (await APIRequest(
uri: "likes/update",
needLogin: true,
args: {
"type": LikesAPIMap[type],
"like": like.toString(),
"id": id.toString(),
},
).exec())
.code ==
200;
}
}

View File

@ -25,8 +25,8 @@ class Post {
final String linkTitle;
final String linkDescription;
final String linkImage;
final int likes;
final bool userLikes;
int likes;
bool userLikes;
final UserAccessLevels access;
Post({

View File

@ -1,11 +1,5 @@
import 'package:comunic/helpers/posts_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/ui/screens/conversation_screen.dart';
import 'package:comunic/ui/tiles/post_tile.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:comunic/ui/widgets/posts_list_widget.dart';
import 'package:flutter/material.dart';
/// Newest posts screen
@ -20,57 +14,13 @@ class NewestPostsScreen extends StatefulWidget {
class _NewestPostsScreenState extends State<NewestPostsScreen> {
// Helpers
final PostsHelper _postsHelper = PostsHelper();
final UsersHelper _usersHelper = UsersHelper();
// Class members
PostsList _list;
UsersList _users;
ErrorLevel _error = ErrorLevel.NONE;
set error(ErrorLevel err) => setState(() => _error = err);
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadPostsList();
}
void _loadError() =>
error = _list == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR;
Future<void> _loadPostsList() async {
final list = await _postsHelper.getLatest();
if (list == null) return _loadError();
final users = await _usersHelper.getList(list.usersID);
if (users == null) return _loadError();
setState(() {
_list = list;
_users = users;
});
}
Widget _buildErrorCard() {
return buildErrorCard(tr("Could not get the list of posts !"));
}
Widget _buildListView() {
return ListView.builder(
itemCount: _list.length,
itemBuilder: (c, i) => PostTile(
post: _list[i],
usersInfo: _users,
),
@override
Widget build(BuildContext context) {
return PostsListWidget(
getPostsList: _postsHelper.getLatest,
);
}
@override
Widget build(BuildContext context) {
if (_error == ErrorLevel.MAJOR) return _buildErrorCard();
if (_list == null) return buildCenteredProgressBar();
return _buildListView();
}
}

View File

@ -5,6 +5,7 @@ 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/date_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
/// Single posts tile
@ -20,13 +21,16 @@ const TextStyle _userNameStyle = TextStyle(
class PostTile extends StatelessWidget {
final Post post;
final UsersList usersInfo;
final void Function(Post) onTapLike;
const PostTile({
Key key,
@required this.post,
@required this.usersInfo,
}) : assert(post != null),
const PostTile(
{Key key,
@required this.post,
@required this.usersInfo,
@required this.onTapLike})
: assert(post != null),
assert(usersInfo != null),
assert(onTapLike != null),
super(key: key);
User get _user => usersInfo.getUser(post.userID);
@ -83,6 +87,39 @@ class PostTile extends StatelessWidget {
);
}
Widget _buildButtonsArea() {
return Padding(
padding:
const EdgeInsets.only(top: 16.0, left: 8.0, right: 8.0, bottom: 16.0),
child: Column(
children: <Widget>[
// Like button
Center(
child: InkWell(
onTap: () => onTapLike(post),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: Icon(
Icons.thumb_up,
color: post.userLikes ? Colors.blue : null,
),
),
Text(post.likes < 2
? tr("%num% like", args: {"num": post.likes.toString()})
: tr("%num% likes", args: {"num": post.likes.toString()}))
],
),
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Card(
@ -95,6 +132,7 @@ class PostTile extends StatelessWidget {
children: <Widget>[
_buildHeaderRow(),
_buildContentRow(),
_buildButtonsArea(),
],
),
),

View File

@ -0,0 +1,107 @@
import 'package:comunic/enums/likes_type.dart';
import 'package:comunic/helpers/likes_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/post.dart';
import 'package:comunic/ui/screens/conversation_screen.dart';
import 'package:comunic/ui/tiles/post_tile.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Posts list widget
///
/// Displays a list of posts
///
/// @author Pierre HUBERT
class PostsListWidget extends StatefulWidget {
final Future<PostsList> Function() getPostsList;
const PostsListWidget({
Key key,
@required this.getPostsList,
}) : assert(getPostsList != null),
super(key: key);
@override
State<StatefulWidget> createState() => _PostsListWidgetState();
}
class _PostsListWidgetState extends State<PostsListWidget> {
// Helpers
final LikesHelper _likesHelper = LikesHelper();
final UsersHelper _usersHelper = UsersHelper();
// Class members
PostsList _list;
UsersList _users;
set error(ErrorLevel err) => setState(() => _error = err);
ErrorLevel _error = ErrorLevel.NONE;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadPostsList();
}
void _loadError() =>
error = _list == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR;
/// Load the list of posts
Future<void> _loadPostsList() async {
final list = await widget.getPostsList();
if (list == null) return _loadError();
final users = await _usersHelper.getList(list.usersID);
if (users == null) return _loadError();
setState(() {
_list = list;
_users = users;
});
}
Widget _buildErrorCard() {
return buildErrorCard(tr("Could not get the list of posts !"));
}
Widget _buildListView() {
return ListView.builder(
itemCount: _list.length,
itemBuilder: (c, i) => PostTile(
post: _list[i],
usersInfo: _users,
onTapLike: _onUpdateLike,
),
);
}
@override
Widget build(BuildContext context) {
if (_error == ErrorLevel.MAJOR) return _buildErrorCard();
if (_list == null) return buildCenteredProgressBar();
return _buildListView();
}
/// Update like status
Future<void> _onUpdateLike(Post post) async {
// Update liking status
_likesHelper.setLiking(
type: LikesType.POST,
like: !post.userLikes,
id: post.id,
);
// Save new like status
setState(() {
post.userLikes = !post.userLikes;
post.userLikes ? post.likes++ : post.likes--;
});
}
}