1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Can change group membership levels

This commit is contained in:
Pierre HUBERT 2020-05-02 17:26:03 +02:00
parent eb66ea407b
commit 685565e031
3 changed files with 55 additions and 5 deletions

View File

@ -306,6 +306,17 @@ class GroupsHelper {
.addInt("userID", userID) .addInt("userID", userID)
.execWithThrow(); .execWithThrow();
/// Change the membership level of a member of a group
///
/// Throws an exception in case of failure
static Future<void> setNewLevel(
int groupID, int userID, GroupMembershipLevel level) async =>
await APIRequest.withLogin("groups/update_membership_level")
.addInt("groupID", groupID)
.addInt("userID", userID)
.addString("level", invertMap(_APIGroupsMembershipLevelsMap)[level])
.execWithThrow();
/// Turn an API entry into a group object /// Turn an API entry into a group object
Group _getGroupFromAPI(Map<String, dynamic> map) { Group _getGroupFromAPI(Map<String, dynamic> map) {
return Group( return Group(

View File

@ -19,6 +19,8 @@ class MultiChoiceEntry<T> {
this.subtitle, this.subtitle,
}) : assert(id != null), }) : assert(id != null),
assert(title != null); assert(title != null);
bool get hasSubtitle => subtitle != null;
} }
/// Show multiple choices dialog /// Show multiple choices dialog
@ -81,7 +83,7 @@ class __MultiChoicesEntryDialogState<T>
onChanged: (v) => setState(() => _currChoice = v), onChanged: (v) => setState(() => _currChoice = v),
), ),
title: Text(f.title), title: Text(f.title),
subtitle: Text(f.subtitle), subtitle: f.hasSubtitle ? Text(f.subtitle) : null,
)) ))
.toList(), .toList(),
), ),

View File

@ -31,13 +31,16 @@ class GroupMembersScreen extends StatefulWidget {
class _GroupMembersScreenState extends State<GroupMembersScreen> { class _GroupMembersScreenState extends State<GroupMembersScreen> {
final _key = GlobalKey<AsyncScreenWidgetState>(); final _key = GlobalKey<AsyncScreenWidgetState>();
Group _group;
GroupMembersList _members; GroupMembersList _members;
UsersList _users; UsersList _users;
Future<void> _refresh() async { Future<void> _refresh() async {
final group = await GroupsHelper().getSingle(widget.groupID, force: true);
final members = await GroupsHelper.getMembersList(widget.groupID); final members = await GroupsHelper.getMembersList(widget.groupID);
final users = await UsersHelper().getListWithThrow(members.usersID); final users = await UsersHelper().getListWithThrow(members.usersID);
_group = group;
_members = members; _members = members;
_users = users; _users = users;
} }
@ -74,6 +77,7 @@ class _GroupMembersScreenState extends State<GroupMembersScreen> {
final user = _users.getUser(membership.userID); final user = _users.getUser(membership.userID);
return _GroupMembershipTile( return _GroupMembershipTile(
group: _group,
membership: membership, membership: membership,
user: user, user: user,
onUpdated: () => _key.currentState.refresh(), onUpdated: () => _key.currentState.refresh(),
@ -110,8 +114,7 @@ List<MultiChoiceEntry<GroupMembershipLevel>> get _membershipLevels => [
MultiChoiceEntry( MultiChoiceEntry(
id: GroupMembershipLevel.ADMINISTRATOR, id: GroupMembershipLevel.ADMINISTRATOR,
title: tr("Administrator"), title: tr("Administrator"),
subtitle: subtitle: tr("Can change members privileges and group settings")),
tr("Can change members privileges and change group settings")),
MultiChoiceEntry( MultiChoiceEntry(
id: GroupMembershipLevel.MODERATOR, id: GroupMembershipLevel.MODERATOR,
title: tr("Moderator"), title: tr("Moderator"),
@ -128,16 +131,19 @@ List<MultiChoiceEntry<GroupMembershipLevel>> get _membershipLevels => [
]; ];
class _GroupMembershipTile extends StatefulWidget { class _GroupMembershipTile extends StatefulWidget {
final Group group;
final GroupMembership membership; final GroupMembership membership;
final User user; final User user;
final void Function() onUpdated; final void Function() onUpdated;
const _GroupMembershipTile({ const _GroupMembershipTile({
Key key, Key key,
@required this.group,
@required this.membership, @required this.membership,
@required this.user, @required this.user,
@required this.onUpdated, @required this.onUpdated,
}) : assert(membership != null), }) : assert(group != null),
assert(membership != null),
assert(user != null), assert(user != null),
assert(onUpdated != null), assert(onUpdated != null),
super(key: key); super(key: key);
@ -146,7 +152,7 @@ class _GroupMembershipTile extends StatefulWidget {
__GroupMembershipTileState createState() => __GroupMembershipTileState(); __GroupMembershipTileState createState() => __GroupMembershipTileState();
} }
enum _MemberMenuOptions { DELETE } enum _MemberMenuOptions { CHANGE_LEVEL, DELETE }
class __GroupMembershipTileState extends State<_GroupMembershipTile> { class __GroupMembershipTileState extends State<_GroupMembershipTile> {
int get groupID => widget.membership.groupID; int get groupID => widget.membership.groupID;
@ -194,6 +200,13 @@ class __GroupMembershipTileState extends State<_GroupMembershipTile> {
Widget _buildMemberCase() { Widget _buildMemberCase() {
return PopupMenuButton<_MemberMenuOptions>( return PopupMenuButton<_MemberMenuOptions>(
itemBuilder: (c) => [ itemBuilder: (c) => [
// Change membership level
PopupMenuItem(
child: Text(tr("Change level")),
enabled: widget.group.isAdmin,
value: _MemberMenuOptions.CHANGE_LEVEL,
),
// Remove membership // Remove membership
PopupMenuItem( PopupMenuItem(
child: Text(tr("Remove")), child: Text(tr("Remove")),
@ -206,12 +219,36 @@ class __GroupMembershipTileState extends State<_GroupMembershipTile> {
void _handleMenu(_MemberMenuOptions value) { void _handleMenu(_MemberMenuOptions value) {
switch (value) { switch (value) {
case _MemberMenuOptions.CHANGE_LEVEL:
_changeLevel();
break;
case _MemberMenuOptions.DELETE: case _MemberMenuOptions.DELETE:
_removeMembership(); _removeMembership();
break; break;
} }
} }
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;
await GroupsHelper.setNewLevel(groupID, userID, newLevel);
} catch (e, s) {
print("Could not change group membership level! $e\n$s");
showSimpleSnack(context, tr("Could not change group membership level!"));
}
widget.onUpdated();
}
void _removeMembership() async { void _removeMembership() async {
try { try {
if (!await showConfirmDialog( if (!await showConfirmDialog(