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

163 lines
4.9 KiB
Dart
Raw Permalink 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;
final Function()? onError;
2020-04-15 11:56:59 +00:00
2020-04-15 16:06:20 +00:00
const GroupMembershipWidget(
2022-03-11 15:36:42 +00:00
{required this.group, this.onUpdated, this.onError});
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")!);
2020-04-15 11:56:59 +00:00
case GroupMembershipLevel.MODERATOR:
return Text(tr("Moderator")!);
2020-04-15 11:56:59 +00:00
case GroupMembershipLevel.MEMBER:
return Text(tr("Member")!);
2020-04-15 11:56:59 +00:00
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),
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!")!);
if (this.widget.onError != null) this.widget.onError!();
2020-04-15 16:06:20 +00:00
} 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)) {
2022-03-11 15:36:42 +00:00
showSimpleSnack(
context, tr("Could not cancel your membership request!")!);
if (this.widget.onError != null) this.widget.onError!();
2020-04-15 16:06:20 +00:00
} 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 11:56:59 +00:00
}
}
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)) {
showSimpleSnack(context, tr("Could not send your membership request!")!);
if (this.widget.onError != null) this.widget.onError!();
2020-04-15 16:06:20 +00:00
} 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 12:04:47 +00:00
}
}
2020-04-15 11:56:59 +00:00
}