1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Improve user page performances

This commit is contained in:
Pierre HUBERT 2020-05-08 09:23:02 +02:00
parent edff7868f2
commit f41983304a
2 changed files with 26 additions and 42 deletions

View File

@ -6,7 +6,6 @@ import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/post_create_form_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/posts_list_widget.dart';
import 'package:comunic/ui/widgets/scroll_watcher.dart';
import 'package:comunic/utils/conversations_utils.dart'; import 'package:comunic/utils/conversations_utils.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart'; import 'package:comunic/utils/ui_utils.dart';
@ -42,19 +41,8 @@ class _UserPageScreenState extends State<UserPageScreen> {
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>(); GlobalKey<RefreshIndicatorState>();
// Scroll detection (to load more user posts automatically)
final _postListKey = GlobalKey<PostsListWidgetState>();
ScrollWatcher _scrollWatcher;
_setStatus(_PageStatus s) => setState(() => _status = s); _setStatus(_PageStatus s) => setState(() => _status = s);
@override
void initState() {
_scrollWatcher = ScrollWatcher(
onReachBottom: () => _postListKey.currentState.reachedPostsBottom());
super.initState();
}
@override @override
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
@ -92,13 +80,7 @@ class _UserPageScreenState extends State<UserPageScreen> {
return Scaffold( return Scaffold(
body: RefreshIndicator( body: RefreshIndicator(
key: _refreshIndicatorKey, key: _refreshIndicatorKey,
child: ListView( child: _buildBody(),
children: <Widget>[]
..add(_buildHeader())
..addAll(_buildBody()),
physics: AlwaysScrollableScrollPhysics(),
controller: _scrollWatcher,
),
onRefresh: _getUserInfo, onRefresh: _getUserInfo,
), ),
); );
@ -161,27 +143,22 @@ class _UserPageScreenState extends State<UserPageScreen> {
); );
} }
List<Widget> _buildBody() { Widget _buildBody() {
return <Widget>[ return PostsListWidget(
// Posts create form topWidgets: [
_buildHeader(),
_userInfo.canPostTexts _userInfo.canPostTexts
? PostCreateFormWidget( ? PostCreateFormWidget(
postTarget: PostTarget.USER_PAGE, postTarget: PostTarget.USER_PAGE,
targetID: _userInfo.id, targetID: _userInfo.id,
onCreated: _postCreated, onCreated: _postCreated,
) )
: Container(), : Container()
],
// Posts list
PostsListWidget(
key: _postListKey,
getPostsList: () => _postsHelper.getUserPosts(widget.userID), getPostsList: () => _postsHelper.getUserPosts(widget.userID),
getOlder: (from) => getOlder: (from) => _postsHelper.getUserPosts(widget.userID, from: from),
_postsHelper.getUserPosts(widget.userID, from: from),
showPostsTarget: false, showPostsTarget: false,
buildListView: false, );
),
];
} }
/// Method called each time a menu option is selected /// Method called each time a menu option is selected

View File

@ -22,6 +22,7 @@ import 'package:flutter/material.dart';
/// @author Pierre HUBERT /// @author Pierre HUBERT
class PostsListWidget extends StatefulWidget { class PostsListWidget extends StatefulWidget {
final List<Widget> topWidgets;
final Future<PostsList> Function() getPostsList; final Future<PostsList> Function() getPostsList;
final Future<PostsList> Function(int from) getOlder; final Future<PostsList> Function(int from) getOlder;
final bool showPostsTarget; final bool showPostsTarget;
@ -35,6 +36,7 @@ class PostsListWidget extends StatefulWidget {
this.userNamesClickable = true, this.userNamesClickable = true,
this.buildListView = true, this.buildListView = true,
this.getOlder, this.getOlder,
this.topWidgets,
}) : assert(getPostsList != null), }) : assert(getPostsList != null),
assert(showPostsTarget != null), assert(showPostsTarget != null),
assert(buildListView != null), assert(buildListView != null),
@ -62,6 +64,9 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
set error(ErrorLevel err) => setState(() => _error = err); set error(ErrorLevel err) => setState(() => _error = err);
int get _numberTopWidgets =>
widget.topWidgets == null ? 0 : widget.topWidgets.length;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -187,7 +192,7 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
Widget _buildListView() { Widget _buildListView() {
return RefreshIndicator( return RefreshIndicator(
child: ListView.builder( child: ListView.builder(
itemCount: _list.length, itemCount: _list.length + _numberTopWidgets,
itemBuilder: _buildItem, itemBuilder: _buildItem,
controller: _scrollController, controller: _scrollController,
), ),
@ -205,8 +210,10 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
} }
Widget _buildItem(BuildContext context, int index) { Widget _buildItem(BuildContext context, int index) {
if (index < _numberTopWidgets) return widget.topWidgets[index];
return PostTile( return PostTile(
post: _list[index], post: _list[index - _numberTopWidgets],
usersInfo: _users, usersInfo: _users,
groupsInfo: _groups, groupsInfo: _groups,
onDeletedPost: _removePost, onDeletedPost: _removePost,