2019-06-15 06:16:47 +00:00
|
|
|
import 'package:comunic/helpers/friends_helper.dart';
|
|
|
|
import 'package:comunic/helpers/users_helper.dart';
|
|
|
|
import 'package:comunic/lists/users_list.dart';
|
|
|
|
import 'package:comunic/ui/tiles/simple_user_tile.dart';
|
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:comunic/utils/navigation_utils.dart';
|
|
|
|
import 'package:comunic/utils/ui_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Other friends list route (not intended to display current user friends list)
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
2020-04-16 11:13:31 +00:00
|
|
|
class OtherUserFriendsListScreen extends StatefulWidget {
|
|
|
|
final int userID;
|
2021-03-17 16:14:53 +00:00
|
|
|
final bool enableAppBar;
|
2019-06-15 06:16:47 +00:00
|
|
|
|
2020-04-16 11:13:31 +00:00
|
|
|
const OtherUserFriendsListScreen({
|
2019-06-15 06:16:47 +00:00
|
|
|
Key key,
|
2020-04-16 11:13:31 +00:00
|
|
|
@required this.userID,
|
2021-03-17 16:14:53 +00:00
|
|
|
this.enableAppBar = true,
|
2020-04-16 11:13:31 +00:00
|
|
|
}) : assert(userID != null),
|
2021-03-17 16:14:53 +00:00
|
|
|
assert(enableAppBar != null),
|
2019-06-15 06:16:47 +00:00
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
2020-04-16 11:13:31 +00:00
|
|
|
_OtherUserFriendsListScreenState createState() =>
|
|
|
|
_OtherUserFriendsListScreenState();
|
2019-06-15 06:16:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:13:31 +00:00
|
|
|
class _OtherUserFriendsListScreenState
|
|
|
|
extends State<OtherUserFriendsListScreen> {
|
2019-06-15 06:16:47 +00:00
|
|
|
final FriendsHelper friendsHelper = FriendsHelper();
|
|
|
|
final UsersHelper usersHelper = UsersHelper();
|
|
|
|
|
2020-05-16 15:31:16 +00:00
|
|
|
Set<int> _friendsList;
|
|
|
|
UsersList _usersInfo;
|
2019-06-15 06:16:47 +00:00
|
|
|
bool _error = false;
|
|
|
|
|
2020-04-16 11:13:31 +00:00
|
|
|
String get _routeName => tr("Friends of %name%",
|
2020-05-16 15:31:16 +00:00
|
|
|
args: {"name": _usersInfo.getUser(widget.userID).displayName});
|
2019-06-15 06:16:47 +00:00
|
|
|
|
|
|
|
void setError(bool e) => setState(() => _error = e);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load the list of friends of the user
|
|
|
|
Future<void> load() async {
|
|
|
|
setError(false);
|
|
|
|
|
|
|
|
try {
|
2020-05-16 15:31:16 +00:00
|
|
|
final friendsList = await friendsHelper.getOtherUserList(widget.userID);
|
|
|
|
|
|
|
|
// We use [Set.toSet] here to avoid the current user to appear in the list
|
|
|
|
final users = await usersHelper
|
|
|
|
.getListWithThrow(friendsList.toSet()..add(widget.userID));
|
2019-06-15 06:16:47 +00:00
|
|
|
|
|
|
|
setState(() {
|
2020-05-16 15:31:16 +00:00
|
|
|
_friendsList = friendsList;
|
|
|
|
_usersInfo = users;
|
2019-06-15 06:16:47 +00:00
|
|
|
});
|
|
|
|
} catch (e, st) {
|
|
|
|
print(e);
|
|
|
|
print(st);
|
|
|
|
setError(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (_error) return _buildError();
|
|
|
|
|
2020-05-16 15:31:16 +00:00
|
|
|
if (_usersInfo == null) return buildCenteredProgressBar();
|
2019-06-15 06:16:47 +00:00
|
|
|
|
|
|
|
return Scaffold(
|
2021-03-17 16:14:53 +00:00
|
|
|
appBar: widget.enableAppBar
|
|
|
|
? AppBar(
|
|
|
|
title: Text(_routeName),
|
|
|
|
)
|
|
|
|
: null,
|
2019-06-15 06:16:47 +00:00
|
|
|
body: ListView.builder(
|
2020-05-16 15:31:16 +00:00
|
|
|
itemCount: _friendsList.length,
|
2019-06-15 06:16:47 +00:00
|
|
|
itemBuilder: (c, i) => SimpleUserTile(
|
2020-05-16 15:31:16 +00:00
|
|
|
user: _usersInfo.getUser(_friendsList.elementAt(i)),
|
2020-04-16 11:13:31 +00:00
|
|
|
onTap: (u) => openUserPage(
|
|
|
|
context: context,
|
|
|
|
userID: u.id,
|
|
|
|
),
|
|
|
|
),
|
2019-06-15 06:16:47 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildError() {
|
|
|
|
return Scaffold(
|
|
|
|
body: buildErrorCard(
|
|
|
|
tr(
|
2020-04-16 11:13:31 +00:00
|
|
|
"Could not get the list of friends of this user !",
|
2019-06-15 06:16:47 +00:00
|
|
|
),
|
|
|
|
actions: [
|
2021-03-13 14:28:34 +00:00
|
|
|
TextButton(
|
2019-06-15 06:16:47 +00:00
|
|
|
child: Text(
|
|
|
|
tr("Try again").toUpperCase(),
|
|
|
|
style: TextStyle(color: Colors.white),
|
|
|
|
),
|
|
|
|
onPressed: load,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|