mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-09-18 21:38:48 +00:00
Display the list of friends of the user
This commit is contained in:
122
lib/ui/screens/friends_list_screen.dart
Normal file
122
lib/ui/screens/friends_list_screen.dart
Normal file
@@ -0,0 +1,122 @@
|
||||
import 'package:comunic/helpers/friends_helper.dart';
|
||||
import 'package:comunic/helpers/users_helper.dart';
|
||||
import 'package:comunic/lists/friends_list.dart';
|
||||
import 'package:comunic/lists/users_list.dart';
|
||||
import 'package:comunic/ui/tiles/accepted_friend_tile.dart';
|
||||
import 'package:comunic/ui/tiles/pending_friend_tile.dart';
|
||||
import 'package:comunic/ui/widgets/safe_state.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Friends list screen
|
||||
///
|
||||
/// Display the list of friends of the current user
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
enum _ErrorsLevel { NONE, MINOR, MAJOR }
|
||||
|
||||
class FriendsListScreen extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => _FriendsListScreenState();
|
||||
}
|
||||
|
||||
class _FriendsListScreenState extends SafeState<FriendsListScreen> {
|
||||
/// Helpers
|
||||
final _friendsHelper = FriendsHelper();
|
||||
final _usersHelper = UsersHelper();
|
||||
|
||||
/// Widget members
|
||||
_ErrorsLevel _error = _ErrorsLevel.NONE;
|
||||
FriendsList _friendsList;
|
||||
UsersList _usersInfo;
|
||||
bool _loading = true;
|
||||
|
||||
/// Useful setters
|
||||
set error(_ErrorsLevel err) => setState(() => _error = err);
|
||||
|
||||
set loading(bool loading) => setState(() => _loading = loading);
|
||||
|
||||
void _gotError() =>
|
||||
error = _friendsList == null ? _ErrorsLevel.MAJOR : _ErrorsLevel.MINOR;
|
||||
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_loadList();
|
||||
}
|
||||
|
||||
/// Load the list of friends
|
||||
Future<void> _loadList() async {
|
||||
error = _ErrorsLevel.NONE;
|
||||
loading = true;
|
||||
|
||||
// Get the list of friends
|
||||
final list = await _friendsHelper.downloadList();
|
||||
|
||||
// Check for errors
|
||||
if (list == null) return _gotError();
|
||||
|
||||
// Get information about related users
|
||||
final users = await _usersHelper.getUsersInfo(list.usersId);
|
||||
|
||||
// Check for errors
|
||||
if (users == null) return _gotError();
|
||||
|
||||
// Apply new information
|
||||
setState(() {
|
||||
_friendsList = list;
|
||||
_usersInfo = users;
|
||||
});
|
||||
loading = false;
|
||||
error = _ErrorsLevel.NONE;
|
||||
}
|
||||
|
||||
/// Build and return loading error
|
||||
Widget _buildError() =>
|
||||
buildErrorCard(
|
||||
tr("Could not load your list of friends!"),
|
||||
actions: [
|
||||
FlatButton(
|
||||
onPressed: _loadList,
|
||||
child: Text(
|
||||
tr("Retry").toUpperCase(),
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_error == _ErrorsLevel.MAJOR) return _buildError();
|
||||
if (_friendsList == null) return buildCenteredProgressBar();
|
||||
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
|
||||
// Check for errors
|
||||
Container(child: _error != _ErrorsLevel.NONE ? _buildError() : null),
|
||||
|
||||
// Check if loading
|
||||
Container(child: _loading ? CircularProgressIndicator() : 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),
|
||||
) : PendingFriendTile(
|
||||
friend: _friendsList[i],
|
||||
user: _usersInfo.getUser(_friendsList[i].id),
|
||||
onRespond: (friend, accept){},
|
||||
)),
|
||||
),
|
||||
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user