mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-25 06:19:22 +00:00
Can like a text
This commit is contained in:
parent
315c61a212
commit
be53a73d9f
5
lib/enums/likes_type.dart
Normal file
5
lib/enums/likes_type.dart
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
/// Likes types
|
||||||
|
///
|
||||||
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
|
enum LikesType { USER, POST, COMMENT, GROUP }
|
35
lib/helpers/likes_helper.dart
Normal file
35
lib/helpers/likes_helper.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -25,8 +25,8 @@ class Post {
|
|||||||
final String linkTitle;
|
final String linkTitle;
|
||||||
final String linkDescription;
|
final String linkDescription;
|
||||||
final String linkImage;
|
final String linkImage;
|
||||||
final int likes;
|
int likes;
|
||||||
final bool userLikes;
|
bool userLikes;
|
||||||
final UserAccessLevels access;
|
final UserAccessLevels access;
|
||||||
|
|
||||||
Post({
|
Post({
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
import 'package:comunic/helpers/posts_helper.dart';
|
import 'package:comunic/helpers/posts_helper.dart';
|
||||||
import 'package:comunic/helpers/users_helper.dart';
|
import 'package:comunic/ui/widgets/posts_list_widget.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:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
/// Newest posts screen
|
/// Newest posts screen
|
||||||
@ -20,57 +14,13 @@ class NewestPostsScreen extends StatefulWidget {
|
|||||||
class _NewestPostsScreenState extends State<NewestPostsScreen> {
|
class _NewestPostsScreenState extends State<NewestPostsScreen> {
|
||||||
// Helpers
|
// Helpers
|
||||||
final PostsHelper _postsHelper = PostsHelper();
|
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
|
||||||
|
Widget build(BuildContext context) {
|
||||||
@override
|
return PostsListWidget(
|
||||||
void didChangeDependencies() {
|
getPostsList: _postsHelper.getLatest,
|
||||||
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) {
|
|
||||||
if (_error == ErrorLevel.MAJOR) return _buildErrorCard();
|
|
||||||
if (_list == null) return buildCenteredProgressBar();
|
|
||||||
return _buildListView();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import 'package:comunic/models/user.dart';
|
|||||||
import 'package:comunic/ui/widgets/account_image_widget.dart';
|
import 'package:comunic/ui/widgets/account_image_widget.dart';
|
||||||
import 'package:comunic/ui/widgets/network_image_widget.dart';
|
import 'package:comunic/ui/widgets/network_image_widget.dart';
|
||||||
import 'package:comunic/utils/date_utils.dart';
|
import 'package:comunic/utils/date_utils.dart';
|
||||||
|
import 'package:comunic/utils/intl_utils.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
/// Single posts tile
|
/// Single posts tile
|
||||||
@ -20,13 +21,16 @@ const TextStyle _userNameStyle = TextStyle(
|
|||||||
class PostTile extends StatelessWidget {
|
class PostTile extends StatelessWidget {
|
||||||
final Post post;
|
final Post post;
|
||||||
final UsersList usersInfo;
|
final UsersList usersInfo;
|
||||||
|
final void Function(Post) onTapLike;
|
||||||
|
|
||||||
const PostTile({
|
const PostTile(
|
||||||
Key key,
|
{Key key,
|
||||||
@required this.post,
|
@required this.post,
|
||||||
@required this.usersInfo,
|
@required this.usersInfo,
|
||||||
}) : assert(post != null),
|
@required this.onTapLike})
|
||||||
|
: assert(post != null),
|
||||||
assert(usersInfo != null),
|
assert(usersInfo != null),
|
||||||
|
assert(onTapLike != null),
|
||||||
super(key: key);
|
super(key: key);
|
||||||
|
|
||||||
User get _user => usersInfo.getUser(post.userID);
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Card(
|
return Card(
|
||||||
@ -95,6 +132,7 @@ class PostTile extends StatelessWidget {
|
|||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
_buildHeaderRow(),
|
_buildHeaderRow(),
|
||||||
_buildContentRow(),
|
_buildContentRow(),
|
||||||
|
_buildButtonsArea(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
107
lib/ui/widgets/posts_list_widget.dart
Normal file
107
lib/ui/widgets/posts_list_widget.dart
Normal 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--;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user