mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Add RefreshIndicator on friends screen
This commit is contained in:
parent
b06c28dc87
commit
c8b68e71aa
@ -33,28 +33,31 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
|
||||
_ErrorsLevel _error = _ErrorsLevel.NONE;
|
||||
FriendsList _friendsList;
|
||||
UsersList _usersInfo;
|
||||
bool _loading = true;
|
||||
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
|
||||
GlobalKey<RefreshIndicatorState>();
|
||||
|
||||
/// 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<void> _refreshList() async {
|
||||
await _refreshIndicatorKey.currentState.show();
|
||||
}
|
||||
|
||||
/// Load the list of friends
|
||||
Future<void> _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<FriendsListScreen> {
|
||||
_friendsList = list;
|
||||
_usersInfo = users;
|
||||
});
|
||||
loading = false;
|
||||
error = _ErrorsLevel.NONE;
|
||||
}
|
||||
|
||||
@ -82,7 +84,7 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
|
||||
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<FriendsListScreen> {
|
||||
if (_error == _ErrorsLevel.MAJOR) return _buildError();
|
||||
if (_friendsList == null) return buildCenteredProgressBar();
|
||||
|
||||
return Stack(
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
Column(
|
||||
children: <Widget>[
|
||||
// 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<FriendsListScreen> {
|
||||
|
||||
/// Respond to friendship request
|
||||
Future<void> _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<void> _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<FriendsListScreen> {
|
||||
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<void> _openPrivateConversation(Friend friend) async {
|
||||
loading = true;
|
||||
await openPrivateConversation(context, friend.id);
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user