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

Display user posts

This commit is contained in:
Pierre HUBERT 2019-06-10 14:47:27 +02:00
parent f374215c0c
commit dd1a130436
3 changed files with 59 additions and 10 deletions

View File

@ -59,6 +59,28 @@ class PostsHelper {
}
}
/// Get the list of posts of a user
Future<PostsList> getUserPosts(int userID, {int from = 0}) async {
final response = await APIRequest(
uri: "posts/get_user",
needLogin: true,
args: {
"userID": userID.toString(),
"startFrom": from.toString()
}
).exec();
if (response.code != 200) return null;
try {
// Parse & return the list of posts
return PostsList()..addAll(response.getArray().map((f) => _apiToPost(f)));
} catch (e) {
print(e.toString());
return null;
}
}
/// Update a post content
Future<bool> updateContent(int id, String newContent) async {
return (await APIRequest(

View File

@ -1,6 +1,8 @@
import 'package:comunic/helpers/posts_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/models/advanced_user_info.dart';
import 'package:comunic/ui/widgets/network_image_widget.dart';
import 'package:comunic/ui/widgets/posts_list_widget.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
@ -25,6 +27,7 @@ class UserPageRoute extends StatefulWidget {
class _UserPageRouteState extends State<UserPageRoute> {
// Helpers
final usersHelper = UsersHelper();
final PostsHelper _postsHelper = PostsHelper();
// Objects members
final double _appBarHeight = 256.0;
@ -132,7 +135,15 @@ class _UserPageRouteState extends State<UserPageRoute> {
Widget _buildBody() {
return SliverList(
delegate: SliverChildListDelegate(<Widget>[]),
delegate: SliverChildListDelegate(
<Widget>[
PostsListWidget(
getPostsList: () => _postsHelper.getUserPosts(widget.userID),
showPostsTarget: false,
buildListView: false,
),
],
),
);
}
}

View File

@ -19,13 +19,16 @@ import 'package:flutter/material.dart';
class PostsListWidget extends StatefulWidget {
final Future<PostsList> Function() getPostsList;
final bool showPostsTarget;
final bool buildListView;
const PostsListWidget({
Key key,
@required this.getPostsList,
@required this.showPostsTarget,
this.buildListView = true,
}) : assert(getPostsList != null),
assert(showPostsTarget != null),
assert(buildListView != null),
super(key: key);
@override
@ -67,7 +70,7 @@ class _PostsListWidgetState extends State<PostsListWidget> {
final groups = await _groupsHelper.getList(list.groupsID);
if(groups == null) return _loadError();
if (groups == null) return _loadError();
setState(() {
_list = list;
@ -83,13 +86,26 @@ class _PostsListWidgetState extends State<PostsListWidget> {
Widget _buildListView() {
return ListView.builder(
itemCount: _list.length,
itemBuilder: (c, i) => PostTile(
post: _list[i],
usersInfo: _users,
groupsInfo: _groups,
onDeletedPost: _removePost,
showPostTarget: widget.showPostsTarget,
),
itemBuilder: _buildItem,
);
}
Widget _buildColumn() {
return Column(
children: List.generate(
_list.length,
(i) => _buildItem(null, i),
),
);
}
Widget _buildItem(BuildContext context, int index) {
return PostTile(
post: _list[index],
usersInfo: _users,
groupsInfo: _groups,
onDeletedPost: _removePost,
showPostTarget: widget.showPostsTarget,
);
}
@ -97,7 +113,7 @@ class _PostsListWidgetState extends State<PostsListWidget> {
Widget build(BuildContext context) {
if (_error == ErrorLevel.MAJOR) return _buildErrorCard();
if (_list == null) return buildCenteredProgressBar();
return _buildListView();
return widget.buildListView ? _buildListView() : _buildColumn();
}
void _removePost(Post post) => setState(() => _list.remove(post));