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;
|
2022-03-10 18:39:57 +00:00
|
|
|
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:
|
2022-03-10 18:39:57 +00:00
|
|
|
return Text(tr("Administrator")!);
|
2020-04-15 11:56:59 +00:00
|
|
|
|
|
|
|
case GroupMembershipLevel.MODERATOR:
|
2022-03-10 18:39:57 +00:00
|
|
|
return Text(tr("Moderator")!);
|
2020-04-15 11:56:59 +00:00
|
|
|
|
|
|
|
case GroupMembershipLevel.MEMBER:
|
2022-03-10 18:39:57 +00:00
|
|
|
return Text(tr("Member")!);
|
2020-04-15 11:56:59 +00:00
|
|
|
|
|
|
|
case GroupMembershipLevel.INVITED:
|
2020-04-15 12:31:45 +00:00
|
|
|
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:
|
2021-03-17 17:56:09 +00:00
|
|
|
throw Exception("Unknown group membership level state: $_level");
|
2020-04-15 11:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 12:31:45 +00:00
|
|
|
/// Build invited state
|
|
|
|
Widget _buildInvitedState() {
|
|
|
|
return RichText(
|
|
|
|
text: TextSpan(children: [
|
|
|
|
WidgetSpan(
|
|
|
|
child: Icon(Icons.info_outline, size: 12),
|
|
|
|
alignment: PlaceholderAlignment.middle),
|
2022-03-10 18:39:57 +00:00
|
|
|
TextSpan(text: " " + tr("Invited")! + " ", style: blackForWhiteTheme()),
|
2020-04-15 12:31:45 +00:00
|
|
|
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)) {
|
2022-03-10 18:39:57 +00:00
|
|
|
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 {
|
2020-04-15 12:31:45 +00:00
|
|
|
// Refresh state
|
|
|
|
group.membershipLevel =
|
|
|
|
accept ? GroupMembershipLevel.MEMBER : GroupMembershipLevel.VISITOR;
|
|
|
|
this.setState(() {});
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
if (this.widget.onUpdated != null) this.widget.onUpdated!();
|
2020-04-15 12:31:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
2022-03-10 18:39:57 +00:00
|
|
|
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!")!);
|
2022-03-10 18:39:57 +00:00
|
|
|
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(() {});
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
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() {
|
2020-04-15 16:09:22 +00:00
|
|
|
// Check if the user can request membership
|
|
|
|
if (group.registrationLevel == GroupRegistrationLevel.CLOSED)
|
2022-03-10 18:39:57 +00:00
|
|
|
return Text(tr("Closed registration")!);
|
2020-04-15 16:09:22 +00:00
|
|
|
|
2020-04-15 12:04:47 +00:00
|
|
|
return RichText(
|
|
|
|
text: TextSpan(
|
|
|
|
text: tr("Request membership"),
|
2021-04-23 17:41:14 +00:00
|
|
|
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)) {
|
2022-03-10 18:39:57 +00:00
|
|
|
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(() {});
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
if (this.widget.onUpdated != null) this.widget.onUpdated!();
|
2020-04-15 12:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 11:56:59 +00:00
|
|
|
}
|