From c8b68e71aa8af661864ec471dccde0ecf87ca15e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 4 May 2019 09:37:53 +0200 Subject: [PATCH] Add RefreshIndicator on friends screen --- lib/ui/screens/friends_list_screen.dart | 89 ++++++++++--------------- 1 file changed, 37 insertions(+), 52 deletions(-) diff --git a/lib/ui/screens/friends_list_screen.dart b/lib/ui/screens/friends_list_screen.dart index 6c1fff1..24e3563 100644 --- a/lib/ui/screens/friends_list_screen.dart +++ b/lib/ui/screens/friends_list_screen.dart @@ -33,28 +33,31 @@ class _FriendsListScreenState extends SafeState { _ErrorsLevel _error = _ErrorsLevel.NONE; FriendsList _friendsList; UsersList _usersInfo; - bool _loading = true; + GlobalKey _refreshIndicatorKey = + GlobalKey(); /// Useful setters set error(_ErrorsLevel err) => setState(() => _error = err); - set loading(bool loading) => setState(() => _loading = loading); - void _gotError() { - loading = false; error = _friendsList == null ? _ErrorsLevel.MAJOR : _ErrorsLevel.MINOR; } + @override void didChangeDependencies() { super.didChangeDependencies(); _loadList(); } + /// Refresh the list of friends + Future _refreshList() async { + await _refreshIndicatorKey.currentState.show(); + } + /// Load the list of friends Future _loadList() async { error = _ErrorsLevel.NONE; - loading = true; // Get the list of friends final list = await _friendsHelper.downloadList(); @@ -73,7 +76,6 @@ class _FriendsListScreenState extends SafeState { _friendsList = list; _usersInfo = users; }); - loading = false; error = _ErrorsLevel.NONE; } @@ -82,7 +84,7 @@ class _FriendsListScreenState extends SafeState { tr("Could not load your list of friends!"), actions: [ FlatButton( - onPressed: _loadList, + onPressed: _refreshList, child: Text( tr("Retry").toUpperCase(), style: TextStyle(color: Colors.white), @@ -96,43 +98,33 @@ class _FriendsListScreenState extends SafeState { if (_error == _ErrorsLevel.MAJOR) return _buildError(); if (_friendsList == null) return buildCenteredProgressBar(); - return Stack( + return Column( children: [ - Column( - children: [ - // Check for errors - Container( - child: _error != _ErrorsLevel.NONE ? _buildError() : null), + // Check for errors + Container(child: _error != _ErrorsLevel.NONE ? _buildError() : null), - // List of friends - Expanded( - child: ListView.builder( - itemCount: _friendsList.length, - itemBuilder: (c, i) => _friendsList[i].accepted - ? AcceptedFriendTile( - friend: _friendsList[i], - user: _usersInfo.getUser(_friendsList[i].id), - onOpenPrivateConversation: _openPrivateConversation, - onSetFollowing: _setFollowingFriend, - onRequestDelete: _deleteFriend, - ) - : PendingFriendTile( - friend: _friendsList[i], - user: _usersInfo.getUser(_friendsList[i].id), - onRespond: _respondRequest, - )), - ), - ], - ), - - // Check if loading - Positioned( - child: Container( - child: - _loading ? Center(child: CircularProgressIndicator()) : null), - top: 16.0, - left: 0.0, - right: 0.0, + // List of friends + Expanded( + child: RefreshIndicator( + key: _refreshIndicatorKey, + onRefresh: _loadList, + child: ListView.builder( + physics: AlwaysScrollableScrollPhysics(), + itemCount: _friendsList.length, + itemBuilder: (c, i) => _friendsList[i].accepted + ? AcceptedFriendTile( + friend: _friendsList[i], + user: _usersInfo.getUser(_friendsList[i].id), + onOpenPrivateConversation: _openPrivateConversation, + onSetFollowing: _setFollowingFriend, + onRequestDelete: _deleteFriend, + ) + : PendingFriendTile( + friend: _friendsList[i], + user: _usersInfo.getUser(_friendsList[i].id), + onRespond: _respondRequest, + )), + ), ), ], ); @@ -140,23 +132,19 @@ class _FriendsListScreenState extends SafeState { /// Respond to friendship request Future _respondRequest(Friend f, bool accept) async { - loading = true; - if (!await _friendsHelper.respondRequest(f.id, accept)) showSimpleSnack(context, tr("Could not respond to friendship request!")); // Load the list of friends again - _loadList(); + _refreshList(); } /// Update following status of a friend Future _setFollowingFriend(Friend friend, bool follow) async { - loading = true; - if (!await _friendsHelper.setFollowing(friend.id, follow)) showSimpleSnack(context, tr("Could not update following status!")); - _loadList(); + _refreshList(); } /// Handles deletion request of a friend @@ -186,19 +174,16 @@ class _FriendsListScreenState extends SafeState { if (choice == null || !choice) return; // Forward the request to the server - loading = true; if (!await _friendsHelper.removeFriend(f.id)) showSimpleSnack( context, tr("Could not delete this person from your friends list!")); // Refresh list - _loadList(); + _refreshList(); } /// Open a private conversation for a given [friend] Future _openPrivateConversation(Friend friend) async { - loading = true; await openPrivateConversation(context, friend.id); - loading = false; } }