1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-23 05:19:22 +00:00
comunicmobile/lib/ui/screens/group_members_screen.dart

331 lines
8.9 KiB
Dart
Raw Normal View History

2020-05-02 09:18:03 +00:00
import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/group_members_list.dart';
import 'package:comunic/lists/users_list.dart';
2020-05-02 09:28:28 +00:00
import 'package:comunic/models/group.dart';
2020-05-02 09:18:03 +00:00
import 'package:comunic/models/group_membership.dart';
import 'package:comunic/models/user.dart';
2020-05-02 09:28:28 +00:00
import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
2020-05-02 13:30:19 +00:00
import 'package:comunic/ui/dialogs/pick_user_dialog.dart';
2020-05-02 09:18:03 +00:00
import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
2020-05-02 15:30:55 +00:00
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Group members screen
///
/// @author Pierre HUBERT
class GroupMembersScreen extends StatefulWidget {
final int groupID;
const GroupMembersScreen({Key key, this.groupID})
: assert(groupID != null),
super(key: key);
@override
_GroupMembersScreenState createState() => _GroupMembersScreenState();
}
class _GroupMembersScreenState extends State<GroupMembersScreen> {
final _key = GlobalKey<AsyncScreenWidgetState>();
2020-05-02 15:26:03 +00:00
Group _group;
2020-05-02 09:18:03 +00:00
GroupMembersList _members;
UsersList _users;
Future<void> _refresh() async {
2020-05-02 15:26:03 +00:00
final group = await GroupsHelper().getSingle(widget.groupID, force: true);
2020-05-02 09:18:03 +00:00
final members = await GroupsHelper.getMembersList(widget.groupID);
final users = await UsersHelper().getListWithThrow(members.usersID);
2020-05-02 15:26:03 +00:00
_group = group;
2020-05-02 09:18:03 +00:00
_members = members;
_users = users;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: ComunicBackButton(),
title: Text(tr("Group members")),
),
2020-05-02 09:18:03 +00:00
body: _buildBody(),
);
}
Widget _buildBody() => AsyncScreenWidget(
key: _key,
2020-05-02 09:18:03 +00:00
onReload: _refresh,
onBuild: _buildBodyContent,
errorMessage: tr("Could not load the list of members of this group!"),
2020-05-02 09:28:28 +00:00
showOldDataWhileUpdating: true,
2020-05-02 09:18:03 +00:00
);
Widget _buildBodyContent() {
2020-05-02 13:30:19 +00:00
return Stack(
children: [
ListView(children: _members.map(_buildGroupMemberTile).toList()),
_buildInvitationFAB()
],
2020-05-02 09:18:03 +00:00
);
}
Widget _buildGroupMemberTile(GroupMembership membership) {
2020-05-02 09:18:03 +00:00
final user = _users.getUser(membership.userID);
return _GroupMembershipTile(
2020-05-02 15:26:03 +00:00
group: _group,
membership: membership,
user: user,
onUpdated: () => _key.currentState.refresh(),
);
}
2020-05-02 13:30:19 +00:00
Widget _buildInvitationFAB() {
return Positioned(
right: 20,
bottom: 20,
child: FloatingActionButton(
onPressed: _inviteMember,
child: Icon(Icons.add),
),
);
}
void _inviteMember() async {
try {
final userID = await showPickUserDialog(context);
if (userID == null) return;
2020-05-02 13:38:02 +00:00
await GroupsHelper.sendInvitation(widget.groupID, userID);
2020-05-02 13:30:19 +00:00
} catch (e, s) {
print("Could not invite a user! $e\n$s");
showSimpleSnack(context, tr("Could not invite a user!"));
}
2020-05-02 13:38:02 +00:00
_key.currentState.refresh();
2020-05-02 13:30:19 +00:00
}
}
List<MultiChoiceEntry<GroupMembershipLevel>> get _membershipLevels => [
MultiChoiceEntry(
id: GroupMembershipLevel.ADMINISTRATOR,
title: tr("Administrator"),
2020-05-02 15:26:03 +00:00
subtitle: tr("Can change members privileges and group settings")),
MultiChoiceEntry(
id: GroupMembershipLevel.MODERATOR,
title: tr("Moderator"),
subtitle: tr(
"Can always create posts, invite users and respond to membership request")),
MultiChoiceEntry(
id: GroupMembershipLevel.MEMBER,
title: tr("Member"),
subtitle: tr("Can access to all group posts")),
MultiChoiceEntry(
2020-05-02 15:36:14 +00:00
id: GroupMembershipLevel.PENDING,
title: tr("Requested"),
hidden: true,
),
MultiChoiceEntry(
id: GroupMembershipLevel.INVITED,
title: tr("Invited"),
hidden: true,
),
MultiChoiceEntry(
id: GroupMembershipLevel.VISITOR,
title: tr("Visitor"),
hidden: true,
),
];
class _GroupMembershipTile extends StatefulWidget {
2020-05-02 15:26:03 +00:00
final Group group;
final GroupMembership membership;
final User user;
final void Function() onUpdated;
const _GroupMembershipTile({
Key key,
2020-05-02 15:26:03 +00:00
@required this.group,
@required this.membership,
@required this.user,
@required this.onUpdated,
2020-05-02 15:26:03 +00:00
}) : assert(group != null),
assert(membership != null),
assert(user != null),
assert(onUpdated != null),
super(key: key);
@override
__GroupMembershipTileState createState() => __GroupMembershipTileState();
}
2020-05-02 15:26:03 +00:00
enum _MemberMenuOptions { CHANGE_LEVEL, DELETE }
2020-05-02 15:05:18 +00:00
class __GroupMembershipTileState extends State<_GroupMembershipTile> {
int get groupID => widget.membership.groupID;
2020-05-02 15:30:55 +00:00
int get memberID => widget.membership.userID;
bool get _isCurrentUser => widget.membership.userID == userID();
@override
Widget build(BuildContext context) {
2020-05-02 09:18:03 +00:00
return ListTile(
leading: AccountImageWidget(
user: widget.user,
2020-05-02 09:18:03 +00:00
),
title: Text(widget.user.displayName),
subtitle: Text(_membershipLevels
.firstWhere((f) => f.id == widget.membership.level)
.title),
trailing: _buildTrailing(),
);
}
Widget _buildTrailing() {
switch (widget.membership.level) {
case GroupMembershipLevel.ADMINISTRATOR:
case GroupMembershipLevel.MODERATOR:
case GroupMembershipLevel.MEMBER:
2020-05-02 15:05:18 +00:00
return _buildMemberCase();
break;
case GroupMembershipLevel.INVITED:
return _buildInvitedCase();
break;
case GroupMembershipLevel.PENDING:
return _buildRequestedCase();
break;
case GroupMembershipLevel.VISITOR:
// This case should never happen
break;
}
return null;
}
2020-05-02 15:05:18 +00:00
Widget _buildMemberCase() {
return PopupMenuButton<_MemberMenuOptions>(
itemBuilder: (c) => [
2020-05-02 15:26:03 +00:00
// Change membership level
PopupMenuItem(
child: Text(tr("Change level")),
2020-05-02 15:30:55 +00:00
enabled: !_isCurrentUser && widget.group.isAdmin,
2020-05-02 15:26:03 +00:00
value: _MemberMenuOptions.CHANGE_LEVEL,
),
2020-05-02 15:05:18 +00:00
// Remove membership
PopupMenuItem(
child: Text(tr("Remove")),
value: _MemberMenuOptions.DELETE,
2020-05-02 15:30:55 +00:00
enabled: !_isCurrentUser,
2020-05-02 15:05:18 +00:00
),
],
onSelected: _handleMenu,
);
}
void _handleMenu(_MemberMenuOptions value) {
switch (value) {
2020-05-02 15:26:03 +00:00
case _MemberMenuOptions.CHANGE_LEVEL:
_changeLevel();
break;
2020-05-02 15:05:18 +00:00
case _MemberMenuOptions.DELETE:
_removeMembership();
break;
}
}
2020-05-02 15:26:03 +00:00
void _changeLevel() async {
try {
final newLevel = await showMultiChoicesDialog(
context: context,
choices: _membershipLevels,
defaultChoice: widget.membership.level,
title: tr("New membership level"),
);
if (newLevel == null) return;
2020-05-02 15:30:55 +00:00
await GroupsHelper.setNewLevel(groupID, memberID, newLevel);
2020-05-02 15:26:03 +00:00
} catch (e, s) {
print("Could not change group membership level! $e\n$s");
showSimpleSnack(context, tr("Could not change group membership level!"));
}
widget.onUpdated();
}
2020-05-02 15:05:18 +00:00
void _removeMembership() async {
try {
if (!await showConfirmDialog(
context: context,
message: tr("Do you really want to remove this membership ?")))
return;
2020-05-02 15:30:55 +00:00
await GroupsHelper.removeMemberFromGroup(groupID, memberID);
2020-05-02 15:05:18 +00:00
} catch (e, s) {
print("Could not remove membership! $e\n$s");
showSimpleSnack(context, tr("Could not remove this membership!"));
}
widget.onUpdated();
}
Widget _buildInvitedCase() {
return MaterialButton(
onPressed: _cancelMembership,
child: Text(tr("Cancel").toUpperCase()),
textColor: Colors.red,
);
}
void _cancelMembership() async {
try {
2020-05-02 15:30:55 +00:00
await GroupsHelper.cancelInvitation(groupID, memberID);
} catch (e, s) {
print("Could not cancel invitation! $e\n$s");
showSimpleSnack(context, tr("Could not cancel invitation!"));
}
widget.onUpdated();
}
Widget _buildRequestedCase() {
return IntrinsicWidth(
child: Row(
children: <Widget>[
MaterialButton(
onPressed: () => _respondRequest(false),
child: Text(tr("Reject").toUpperCase()),
textColor: Colors.red,
),
MaterialButton(
onPressed: () => _respondRequest(true),
child: Text(tr("Accept").toUpperCase()),
textColor: Colors.green,
)
],
));
}
void _respondRequest(bool accept) async {
try {
2020-05-02 15:30:55 +00:00
await GroupsHelper.respondRequest(groupID, memberID, accept);
} catch (e, s) {
print("Could not respond to membership request! $e\n$s");
showSimpleSnack(context, tr("Could not respond to membership request!"));
}
widget.onUpdated();
}
}