From dd1a130436faaa0e861d2135b29052af70a63270 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 10 Jun 2019 14:47:27 +0200 Subject: [PATCH] Display user posts --- lib/helpers/posts_helper.dart | 22 +++++++++++++++++ lib/ui/routes/user_page_route.dart | 13 +++++++++- lib/ui/widgets/posts_list_widget.dart | 34 ++++++++++++++++++++------- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index cf497f3..76352c7 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -59,6 +59,28 @@ class PostsHelper { } } + /// Get the list of posts of a user + Future 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 updateContent(int id, String newContent) async { return (await APIRequest( diff --git a/lib/ui/routes/user_page_route.dart b/lib/ui/routes/user_page_route.dart index ee7e1cf..fc864c2 100644 --- a/lib/ui/routes/user_page_route.dart +++ b/lib/ui/routes/user_page_route.dart @@ -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 { // Helpers final usersHelper = UsersHelper(); + final PostsHelper _postsHelper = PostsHelper(); // Objects members final double _appBarHeight = 256.0; @@ -132,7 +135,15 @@ class _UserPageRouteState extends State { Widget _buildBody() { return SliverList( - delegate: SliverChildListDelegate([]), + delegate: SliverChildListDelegate( + [ + PostsListWidget( + getPostsList: () => _postsHelper.getUserPosts(widget.userID), + showPostsTarget: false, + buildListView: false, + ), + ], + ), ); } } diff --git a/lib/ui/widgets/posts_list_widget.dart b/lib/ui/widgets/posts_list_widget.dart index 7827dae..e31c99a 100644 --- a/lib/ui/widgets/posts_list_widget.dart +++ b/lib/ui/widgets/posts_list_widget.dart @@ -19,13 +19,16 @@ import 'package:flutter/material.dart'; class PostsListWidget extends StatefulWidget { final Future 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 { 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 { 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 { 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));