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

163 lines
4.9 KiB
Dart
Raw Normal View History

2020-04-15 11:56:59 +00:00
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;
2020-04-15 16:06:20 +00:00
final Function() onError;
2020-04-15 11:56:59 +00:00
2020-04-15 16:06:20 +00:00
const GroupMembershipWidget(
{@required this.group, this.onUpdated, this.onError})
: assert(group != null);
2020-04-15 11:56:59 +00:00
@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:
return _buildInvitedState();
2020-04-15 11:56:59 +00:00
case GroupMembershipLevel.PENDING:
return _buildPendingState();
case GroupMembershipLevel.VISITOR:
2020-04-15 12:04:47 +00:00
return _buildVisitorState();
2020-04-15 16:06:20 +00:00
default:
throw Exception("Unknown group membership level state: $_level");
2020-04-15 11:56:59 +00:00
}
}
/// Build invited state
Widget _buildInvitedState() {
return RichText(
text: TextSpan(children: [
WidgetSpan(
child: Icon(Icons.info_outline, size: 12),
alignment: PlaceholderAlignment.middle),
2021-03-16 16:41:14 +00:00
TextSpan(text: " " + tr("Invited") + " ", style: blackForWhiteTheme()),
TextSpan(
text: tr("Accept"),
style: TextStyle(color: Colors.green),
recognizer: TapGestureRecognizer()
..onTap = () => _respondInvitation(true)),
TextSpan(text: " "),
TextSpan(
text: tr("Reject"),
style: TextStyle(color: Colors.red),
recognizer: TapGestureRecognizer()
..onTap = () => _respondInvitation(false)),
]),
);
}
/// Respond to an invitation
void _respondInvitation(bool accept) async {
if (!accept &&
!await showConfirmDialog(
context: context,
message: tr("Do you really want to reject this invitation?")))
return;
2021-04-23 17:32:34 +00:00
if (!await GroupsHelper.respondInvitation(_id, accept)) {
showSimpleSnack(context, tr("Could not respond to your invitation!"));
2020-04-15 16:06:20 +00:00
if (this.widget.onError != null) this.widget.onError();
} else {
// Refresh state
group.membershipLevel =
accept ? GroupMembershipLevel.MEMBER : GroupMembershipLevel.VISITOR;
this.setState(() {});
if (this.widget.onUpdated != null) this.widget.onUpdated();
}
}
2020-04-15 11:56:59 +00:00
/// Build pending state
Widget _buildPendingState() {
return RichText(
text: TextSpan(children: [
2020-04-15 12:07:50 +00:00
WidgetSpan(
child: Icon(Icons.access_time, size: 12),
alignment: PlaceholderAlignment.middle),
2021-03-16 16:41:14 +00:00
TextSpan(
text: " " + tr("Requested") + " ", style: blackForWhiteTheme()),
2020-04-15 11:56:59 +00:00
TextSpan(
text: tr("Cancel"),
style: TextStyle(color: Colors.blue),
recognizer: TapGestureRecognizer()..onTap = () => _cancelRequest()),
]),
);
}
/// Cancel group membership request
void _cancelRequest() async {
2020-04-15 16:06:20 +00:00
if (!await GroupsHelper().cancelRequest(_id)) {
2020-04-15 11:56:59 +00:00
showSimpleSnack(context, tr("Could not cancel your membership request!"));
2020-04-15 16:06:20 +00:00
if (this.widget.onError != null) this.widget.onError();
} else {
2020-04-15 11:56:59 +00:00
// Refresh state
group.membershipLevel = GroupMembershipLevel.VISITOR;
this.setState(() {});
if (this.widget.onUpdated != null) this.widget.onUpdated();
}
}
2020-04-15 12:04:47 +00:00
/// Build visitor state
Widget _buildVisitorState() {
// Check if the user can request membership
if (group.registrationLevel == GroupRegistrationLevel.CLOSED)
return Text(tr("Closed registration"));
2020-04-15 12:04:47 +00:00
return RichText(
text: TextSpan(
text: tr("Request membership"),
style: blackForWhiteTheme(),
2020-04-15 12:04:47 +00:00
recognizer: TapGestureRecognizer()
..onTap = () => _requestMembership()),
);
}
/// Create new membership request
void _requestMembership() async {
2021-04-23 17:32:34 +00:00
if (!await GroupsHelper.sendRequest(_id)) {
2020-04-15 12:04:47 +00:00
showSimpleSnack(context, tr("Could not send your membership request!"));
2020-04-15 16:06:20 +00:00
if (this.widget.onError != null) this.widget.onError();
} else {
2020-04-15 12:04:47 +00:00
// Refresh state
group.membershipLevel = GroupMembershipLevel.PENDING;
this.setState(() {});
if (this.widget.onUpdated != null) this.widget.onUpdated();
}
}
2020-04-15 11:56:59 +00:00
}