1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 12:58:51 +00:00

Can cancel membership request

This commit is contained in:
Pierre HUBERT 2020-04-15 13:56:59 +02:00
parent d0bafd5bd9
commit eaeb07db69
4 changed files with 96 additions and 5 deletions

View File

@ -100,13 +100,20 @@ class GroupsHelper {
.map((f) => cast<int>(f))
.toSet();
/// Remove group membership
Future<bool> removeMembership(int groupID) async =>
(await (APIRequest(uri: "groups/remove_membership", needLogin: true)
..addInt("id", groupID))
/// Perform a simple membership request
Future<bool> _simpleMembershipRequest(int groupID, String uri) async =>
(await (APIRequest(uri: uri, needLogin: true)..addInt("id", groupID))
.exec())
.isOK;
/// Remove group membership
Future<bool> removeMembership(int groupID) async =>
_simpleMembershipRequest(groupID, "groups/remove_membership");
/// Cancel membership request
Future<bool> cancelRequest(int groupID) async =>
_simpleMembershipRequest(groupID, "groups/cancel_request");
/// Turn an API entry into a group object
Group _getGroupFromAPI(Map<String, dynamic> map) {
return Group(

View File

@ -24,7 +24,7 @@ class Group {
final String name;
final String iconURL;
final int numberMembers;
final GroupMembershipLevel membershipLevel;
GroupMembershipLevel membershipLevel;
final GroupVisibilityLevel visibilityLevel;
final GroupRegistrationLevel registrationLevel;
final GroupPostCreationLevel postCreationLevel;

View File

@ -2,6 +2,7 @@ import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/models/group.dart';
import 'package:comunic/ui/widgets/group_icon_widget.dart';
import 'package:comunic/ui/widgets/group_membership_widget.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
@ -57,6 +58,7 @@ class _GroupsListScreenState extends SafeState<GroupsListScreen> {
.map((g) => ListTile(
leading: GroupIcon(group: g),
title: Text(g.displayName),
subtitle: GroupMembershipWidget(group: g),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () => _deleteGroup(g)),

View File

@ -0,0 +1,82 @@
import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/models/group.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/gestures.dart';
import 'package:flutter/material.dart';
/// Group membership information widget
///
/// @author Pierre Hubert
class GroupMembershipWidget extends StatefulWidget {
final Group group;
final Function() onUpdated;
const GroupMembershipWidget({
@required this.group,
this.onUpdated,
}) : assert(group != null);
@override
_GroupMembershipWidgetState createState() => _GroupMembershipWidgetState();
}
class _GroupMembershipWidgetState extends SafeState<GroupMembershipWidget> {
Group get group => widget.group;
int get _id => group.id;
GroupMembershipLevel get _level => group.membershipLevel;
@override
Widget build(BuildContext context) {
switch (_level) {
case GroupMembershipLevel.ADMINISTRATOR:
return Text(tr("Administrator"));
case GroupMembershipLevel.MODERATOR:
return Text(tr("Moderator"));
case GroupMembershipLevel.MEMBER:
return Text(tr("Member"));
case GroupMembershipLevel.INVITED:
// TODO: Handle this case.
break;
case GroupMembershipLevel.PENDING:
return _buildPendingState();
case GroupMembershipLevel.VISITOR:
// TODO: Handle this case.
break;
}
}
/// Build pending state
Widget _buildPendingState() {
return RichText(
text: TextSpan(children: [
TextSpan(text: tr("Requested") + " "),
TextSpan(
text: tr("Cancel"),
style: TextStyle(color: Colors.blue),
recognizer: TapGestureRecognizer()..onTap = () => _cancelRequest()),
]),
);
}
/// Cancel group membership request
void _cancelRequest() async {
if (!await GroupsHelper().cancelRequest(_id))
showSimpleSnack(context, tr("Could not cancel your membership request!"));
else {
// Refresh state
group.membershipLevel = GroupMembershipLevel.VISITOR;
this.setState(() {});
if (this.widget.onUpdated != null) this.widget.onUpdated();
}
}
}