From 302e5f22ce576dada42a72c5df540f5022f2d9ca Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 16 Apr 2020 09:17:10 +0200 Subject: [PATCH] Can load older posts on user page --- lib/helpers/posts_helper.dart | 7 +++--- lib/ui/routes/user_page_route.dart | 36 +++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index 8bdac36..0f554e2 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -67,10 +67,9 @@ 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()}) + final response = await (APIRequest(uri: "posts/get_user", needLogin: true) + ..addInt("userID", userID) + ..addInt("startFrom", from == 0 ? 0 : from - 1)) .exec(); if (response.code != 200) return null; diff --git a/lib/ui/routes/user_page_route.dart b/lib/ui/routes/user_page_route.dart index d4d2f74..2b47e68 100644 --- a/lib/ui/routes/user_page_route.dart +++ b/lib/ui/routes/user_page_route.dart @@ -7,6 +7,7 @@ import 'package:comunic/ui/routes/user_access_denied_route.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart'; import 'package:comunic/ui/widgets/post_create_form_widget.dart'; import 'package:comunic/ui/widgets/posts_list_widget.dart'; +import 'package:comunic/ui/widgets/scroll_watcher.dart'; import 'package:comunic/utils/conversations_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; @@ -43,8 +44,19 @@ class _UserPageRouteState extends State { GlobalKey _refreshIndicatorKey = GlobalKey(); + // Scroll detection (to load more user posts automatically) + final _postListKey = GlobalKey(); + ScrollWatcher _scrollWatcher; + _setStatus(_PageStatus s) => setState(() => _status = s); + @override + void initState() { + _scrollWatcher = ScrollWatcher( + onReachBottom: () => _postListKey.currentState.reachedPostsBottom()); + super.initState(); + } + @override void didChangeDependencies() { super.didChangeDependencies(); @@ -69,8 +81,8 @@ class _UserPageRouteState extends State { Navigator.of(context).pushReplacement( MaterialPageRoute( builder: (c) => UserAccessDeniedRoute( - userID: widget.userID, - ), + userID: widget.userID, + ), ), ); } @@ -88,6 +100,7 @@ class _UserPageRouteState extends State { child: CustomScrollView( slivers: [_buildHeader(), _buildBody()], physics: AlwaysScrollableScrollPhysics(), + controller: _scrollWatcher, ), onRefresh: _getUserInfo, ), @@ -165,12 +178,12 @@ class _UserPageRouteState extends State { ), PopupMenuButton<_MenuOptions>( itemBuilder: (c) => [ - PopupMenuItem( - child: Text(tr("Friends")), - enabled: _userInfo != null, - value: _MenuOptions.FRIENDS_LIST, - ) - ], + PopupMenuItem( + child: Text(tr("Friends")), + enabled: _userInfo != null, + value: _MenuOptions.FRIENDS_LIST, + ) + ], onSelected: _selectedMenuOption, ), ], @@ -192,7 +205,10 @@ class _UserPageRouteState extends State { // Posts list PostsListWidget( + key: _postListKey, getPostsList: () => _postsHelper.getUserPosts(widget.userID), + getOlder: (from) => + _postsHelper.getUserPosts(widget.userID, from: from), showPostsTarget: false, buildListView: false, ), @@ -208,8 +224,8 @@ class _UserPageRouteState extends State { Navigator.of(context).push( MaterialPageRoute( builder: (c) => OtherUserFriendsListRoute( - user: _userInfo, - ), + user: _userInfo, + ), ), ); break;