1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 08:16:28 +00:00

Can cancel a group membership invitation

This commit is contained in:
Pierre HUBERT 2020-05-02 13:59:40 +02:00
parent f179e7e1d5
commit 1227ef283c
3 changed files with 131 additions and 31 deletions

View File

@ -268,6 +268,15 @@ class GroupsHelper {
.map((f) => _apiToGroupMembership(f)) .map((f) => _apiToGroupMembership(f))
.toList()); .toList());
/// Cancel a group membership invitation
///
/// Throws an exception in case of failure
static Future<void> cancelInvitation(int groupID, int userID) async =>
await APIRequest.withLogin("groups/cancel_invitation")
.addInt("groupID", groupID)
.addInt("userID", userID)
.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

@ -36,6 +36,12 @@ class APIRequest {
if (this.args == null) this.args = Map(); if (this.args == null) this.args = Map();
} }
APIRequest.withLogin(this.uri, {this.args})
: needLogin = true,
assert(uri != null) {
if (args == null) this.args = Map();
}
APIRequest addString(String name, String value) { APIRequest addString(String name, String value) {
args[name] = value; args[name] = value;
return this; return this;

View File

@ -4,11 +4,13 @@ import 'package:comunic/lists/group_members_list.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/group.dart'; import 'package:comunic/models/group.dart';
import 'package:comunic/models/group_membership.dart'; import 'package:comunic/models/group_membership.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/dialogs/multi_choices_dialog.dart'; import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart'; import 'package:comunic/ui/widgets/async_screen_widget.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// Group members screen /// Group members screen
@ -27,6 +29,7 @@ class GroupMembersScreen extends StatefulWidget {
} }
class _GroupMembersScreenState extends State<GroupMembersScreen> { class _GroupMembersScreenState extends State<GroupMembersScreen> {
final _key = GlobalKey<AsyncScreenWidgetState>();
GroupMembersList _members; GroupMembersList _members;
UsersList _users; UsersList _users;
@ -50,6 +53,7 @@ class _GroupMembersScreenState extends State<GroupMembersScreen> {
} }
Widget _buildBody() => AsyncScreenWidget( Widget _buildBody() => AsyncScreenWidget(
key: _key,
onReload: _refresh, onReload: _refresh,
onBuild: _buildBodyContent, onBuild: _buildBodyContent,
errorMessage: tr("Could not load the list of members of this group!"), errorMessage: tr("Could not load the list of members of this group!"),
@ -62,39 +66,120 @@ class _GroupMembersScreenState extends State<GroupMembersScreen> {
); );
} }
List<MultiChoiceEntry<GroupMembershipLevel>> get _membershipLevels => [ Widget _buildGroupMemberTile(GroupMembership membership) {
MultiChoiceEntry(
id: GroupMembershipLevel.ADMINISTRATOR,
title: tr("Administrator"),
subtitle:
tr("Can change members privileges and change 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(
id: GroupMembershipLevel.PENDING, title: tr("Requested")),
MultiChoiceEntry(
id: GroupMembershipLevel.INVITED, title: tr("Invited")),
MultiChoiceEntry(
id: GroupMembershipLevel.VISITOR, title: tr("Visitor")),
];
ListTile _buildGroupMemberTile(GroupMembership membership) {
final user = _users.getUser(membership.userID); final user = _users.getUser(membership.userID);
return ListTile( return _GroupMembershipTile(
leading: AccountImageWidget( membership: membership,
user: user, user: user,
), onUpdated: () => _key.currentState.refresh(),
title: Text(user.displayName),
subtitle: Text(
_membershipLevels.firstWhere((f) => f.id == membership.level).title),
); );
} }
} }
List<MultiChoiceEntry<GroupMembershipLevel>> get _membershipLevels => [
MultiChoiceEntry(
id: GroupMembershipLevel.ADMINISTRATOR,
title: tr("Administrator"),
subtitle:
tr("Can change members privileges and change 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(
id: GroupMembershipLevel.PENDING, title: tr("Requested")),
MultiChoiceEntry(id: GroupMembershipLevel.INVITED, title: tr("Invited")),
MultiChoiceEntry(id: GroupMembershipLevel.VISITOR, title: tr("Visitor")),
];
class _GroupMembershipTile extends StatefulWidget {
final GroupMembership membership;
final User user;
final void Function() onUpdated;
const _GroupMembershipTile({
Key key,
@required this.membership,
@required this.user,
@required this.onUpdated,
}) : assert(membership != null),
assert(user != null),
assert(onUpdated != null),
super(key: key);
@override
__GroupMembershipTileState createState() => __GroupMembershipTileState();
}
class __GroupMembershipTileState extends State<_GroupMembershipTile> {
int get groupID => widget.membership.groupID;
int get userID => widget.membership.userID;
@override
Widget build(BuildContext context) {
return ListTile(
leading: AccountImageWidget(
user: widget.user,
),
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:
// TODO: Handle this case.
break;
case GroupMembershipLevel.MODERATOR:
// TODO: Handle this case.
break;
case GroupMembershipLevel.MEMBER:
// TODO: Handle this case.
break;
case GroupMembershipLevel.INVITED:
return _buildInvitedCase();
break;
case GroupMembershipLevel.PENDING:
// TODO: Handle this case.
break;
case GroupMembershipLevel.VISITOR:
// This case should never happen
break;
}
return null;
}
Widget _buildInvitedCase() {
return MaterialButton(
onPressed: _cancelMembership,
child: Text(tr("Cancel").toUpperCase()),
textColor: Colors.red,
);
}
void _cancelMembership() async {
try {
await GroupsHelper.cancelInvitation(groupID, userID);
} catch (e, s) {
print("Could not cancel invitation! $e\n$s");
showSimpleSnack(context, tr("Could not cancel invitation!"));
}
widget.onUpdated();
}
}