1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-24 22:09:21 +00:00
comunicmobile/lib/ui/screens/group_settings_screen.dart

496 lines
15 KiB
Dart
Raw Normal View History

2020-05-02 06:51:34 +00:00
import 'dart:typed_data';
2021-04-06 15:40:13 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2020-05-01 13:12:55 +00:00
import 'package:comunic/helpers/groups_helper.dart';
2021-04-06 15:04:55 +00:00
import 'package:comunic/helpers/server_config_helper.dart';
2020-05-01 13:12:55 +00:00
import 'package:comunic/models/advanced_group_info.dart';
2021-04-06 15:40:13 +00:00
import 'package:comunic/models/conversation.dart';
2020-05-01 18:52:24 +00:00
import 'package:comunic/models/group.dart';
2021-04-06 15:04:55 +00:00
import 'package:comunic/models/new_group_conversation.dart';
2020-05-02 07:45:03 +00:00
import 'package:comunic/ui/dialogs/input_user_password_dialog.dart';
2020-05-01 18:52:24 +00:00
import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
2020-05-01 18:10:25 +00:00
import 'package:comunic/ui/dialogs/virtual_directory_dialog.dart';
2020-05-05 11:21:37 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
2020-05-01 13:12:55 +00:00
import 'package:comunic/ui/widgets/async_screen_widget.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
2020-05-02 06:51:34 +00:00
import 'package:comunic/ui/widgets/group_icon_widget.dart';
2020-05-01 13:12:55 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
2021-02-12 21:45:17 +00:00
import 'package:comunic/ui/widgets/settings/header_spacer_section.dart';
2020-05-01 18:52:24 +00:00
import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart';
2020-05-01 13:39:54 +00:00
import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart';
2020-05-02 06:51:34 +00:00
import 'package:comunic/utils/files_utils.dart';
2021-12-28 15:34:14 +00:00
import 'package:comunic/utils/identicon_utils.dart';
2020-05-01 18:19:22 +00:00
import 'package:comunic/utils/input_utils.dart';
2020-05-01 13:12:55 +00:00
import 'package:comunic/utils/intl_utils.dart';
2021-04-06 15:04:55 +00:00
import 'package:comunic/utils/log_utils.dart';
2020-05-01 13:39:54 +00:00
import 'package:comunic/utils/ui_utils.dart';
2020-05-01 13:12:55 +00:00
import 'package:flutter/material.dart';
2021-12-28 14:23:08 +00:00
import 'package:flutter_settings_ui/flutter_settings_ui.dart';
2020-05-01 13:12:55 +00:00
/// Groups settings screen
///
/// @author Pierre Hubert
class GroupSettingsScreen extends StatefulWidget {
final int groupID;
const GroupSettingsScreen({Key? key, required this.groupID})
2022-03-11 16:09:37 +00:00
: super(key: key);
2020-05-01 13:12:55 +00:00
@override
_GroupSettingsScreenState createState() => _GroupSettingsScreenState();
}
class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
AdvancedGroupInfo? _groupSettings;
2020-05-01 13:12:55 +00:00
2020-05-01 13:39:54 +00:00
final _key = GlobalKey<AsyncScreenWidgetState>();
2020-05-01 13:12:55 +00:00
Future<void> _refresh() async {
_groupSettings = await GroupsHelper().getSettings(widget.groupID);
}
2020-05-01 13:39:54 +00:00
Future<void> _updateSettings() async {
try {
await GroupsHelper.setSettings(_groupSettings!);
2020-05-01 13:39:54 +00:00
} catch (e, stack) {
print("Could not update group settings! $e\n$stack");
showSimpleSnack(context, tr("Could not update group settings!")!);
2020-05-01 13:39:54 +00:00
}
_key.currentState!.refresh();
2020-05-01 13:39:54 +00:00
}
2020-05-01 13:12:55 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: ComunicBackButton(),
title: Text(tr("Group settings")!),
2020-05-01 13:12:55 +00:00
),
body: _buildBody(),
);
}
Widget _buildBody() {
return AsyncScreenWidget(
2020-05-01 13:39:54 +00:00
key: _key,
2020-05-01 13:12:55 +00:00
onReload: _refresh,
onBuild: _buildContent,
errorMessage: tr("Could not get group settings!")!,
2020-05-01 13:39:54 +00:00
showOldDataWhileUpdating: true,
2020-05-01 13:12:55 +00:00
);
}
Widget _buildContent() {
return SettingsList(
2020-05-01 18:52:24 +00:00
sections: [
2021-02-12 21:45:17 +00:00
HeadSpacerSection(),
2020-05-01 18:52:24 +00:00
_buildGeneralSection(),
_buildAccessRestrictions(),
2021-04-06 15:04:55 +00:00
_buildConversationsArea(),
2020-05-02 07:45:03 +00:00
_buildGroupLogoArea(),
_buildDangerZone(),
2020-05-01 18:52:24 +00:00
],
2020-05-01 13:12:55 +00:00
);
}
SettingsSection _buildGeneralSection() {
2020-05-01 13:12:55 +00:00
return SettingsSection(
title: tr("General information"),
tiles: [
2020-05-01 13:39:54 +00:00
// Group ID
2020-05-01 13:12:55 +00:00
SettingsTile(
title: tr("Group ID"),
subtitle: _groupSettings!.id.toString(),
2020-05-01 13:12:55 +00:00
),
2020-05-01 13:39:54 +00:00
// Group name
TextEditSettingsTile(
title: tr("Group name")!,
currValue: _groupSettings!.name,
2020-05-01 13:39:54 +00:00
onChanged: (s) {
_groupSettings!.name = s;
2020-05-01 13:39:54 +00:00
_updateSettings();
}),
2020-05-01 18:10:25 +00:00
// Group virtual directory
SettingsTile(
title: tr("Virtual directory (optional)"),
subtitle: _groupSettings!.virtualDirectory,
2021-02-07 16:09:08 +00:00
onPressed: (_) async {
2020-05-01 18:10:25 +00:00
final newDir = await showVirtualDirectoryDialog(
context: context,
initialDirectory: _groupSettings!.virtualDirectory,
id: _groupSettings!.id,
2020-05-01 18:10:25 +00:00
type: VirtualDirectoryTargetType.GROUP,
);
if (newDir == null) return;
_groupSettings!.virtualDirectory = newDir;
2020-05-01 18:10:25 +00:00
_updateSettings();
},
),
2020-05-01 18:19:22 +00:00
// Group URL
TextEditSettingsTile(
title: tr("Group URL (optional)")!,
currValue: _groupSettings!.url,
2020-05-01 18:19:22 +00:00
checkInput: validateUrl,
allowEmptyValues: true,
onChanged: (s) {
_groupSettings!.url = s;
2020-05-01 18:19:22 +00:00
_updateSettings();
},
2020-05-01 18:30:26 +00:00
),
// Group description
TextEditSettingsTile(
title: tr("Group description (optional)")!,
currValue: _groupSettings!.description,
2020-05-01 18:30:26 +00:00
maxLines: 3,
maxLength: 255,
allowEmptyValues: true,
onChanged: (s) {
_groupSettings!.description = s;
2020-05-01 18:30:26 +00:00
_updateSettings();
}),
2020-05-01 13:12:55 +00:00
],
);
}
2020-05-01 18:52:24 +00:00
List<MultiChoiceEntry<GroupVisibilityLevel>> get _visibilityLevels => [
MultiChoiceEntry(
id: GroupVisibilityLevel.OPEN,
title: tr("Open group")!,
2020-05-01 18:52:24 +00:00
subtitle:
tr("Group information & public posts are available to everyone."),
),
MultiChoiceEntry(
id: GroupVisibilityLevel.PRIVATE,
title: tr("Private group")!,
2020-05-01 18:52:24 +00:00
subtitle: tr("The group is accessible to accepted members only."),
),
MultiChoiceEntry(
id: GroupVisibilityLevel.SECRETE,
title: tr("Secrete group")!,
2020-05-01 18:52:24 +00:00
subtitle: tr("The group is visible only to invited members."),
),
];
2020-05-01 19:04:50 +00:00
List<MultiChoiceEntry<GroupRegistrationLevel>> get _registrationLevels => [
MultiChoiceEntry(
id: GroupRegistrationLevel.OPEN,
title: tr("Open registration")!,
2020-05-01 19:04:50 +00:00
subtitle: tr(
"Everyone can choose to join the group without moderator approval"),
),
MultiChoiceEntry(
id: GroupRegistrationLevel.MODERATED,
title: tr("Moderated registration")!,
2020-05-01 19:04:50 +00:00
subtitle: tr(
"Everyone can request a membership, but a moderator review the request"),
),
MultiChoiceEntry(
id: GroupRegistrationLevel.CLOSED,
title: tr("Closed registration")!,
2020-05-01 19:04:50 +00:00
subtitle: tr(
"The only way to join the group is to be invited by a moderator"),
),
];
2020-05-01 19:13:31 +00:00
List<MultiChoiceEntry<GroupPostCreationLevel>> get _postsCreationLevels => [
MultiChoiceEntry(
id: GroupPostCreationLevel.MEMBERS,
title: tr("All members")!,
2020-05-01 19:13:31 +00:00
subtitle:
tr("All the members of the group can create posts on the group"),
),
MultiChoiceEntry(
id: GroupPostCreationLevel.MODERATORS,
title: tr("Moderators only")!,
2020-05-01 19:13:31 +00:00
subtitle: tr(
"Only moderators and administrators of the group can create posts on it"),
),
];
SettingsSection _buildAccessRestrictions() => SettingsSection(
2020-05-01 18:52:24 +00:00
title: tr("Access restrictions"),
tiles: [
2020-05-01 19:13:31 +00:00
// Group visibility
2020-05-01 18:52:24 +00:00
MultiChoicesSettingsTile(
title: tr("Group visibility")!,
2020-05-01 18:52:24 +00:00
choices: _visibilityLevels,
currentValue: _groupSettings!.visibilityLevel,
onChanged: (dynamic v) {
_groupSettings!.visibilityLevel = v;
2020-05-01 18:52:24 +00:00
_updateSettings();
2020-05-01 19:04:50 +00:00
}),
2020-05-01 19:13:31 +00:00
// Group registration level
2020-05-01 19:04:50 +00:00
MultiChoicesSettingsTile(
title: tr("Group registration level")!,
2020-05-01 19:04:50 +00:00
choices: _registrationLevels,
currentValue: _groupSettings!.registrationLevel,
onChanged: (dynamic v) {
_groupSettings!.registrationLevel = v;
2020-05-01 19:04:50 +00:00
_updateSettings();
}),
2020-05-01 19:13:31 +00:00
// Group posts creation levels
MultiChoicesSettingsTile(
title: tr("Posts creation level")!,
2020-05-01 19:13:31 +00:00
choices: _postsCreationLevels,
currentValue: _groupSettings!.postCreationLevel,
onChanged: (dynamic s) {
_groupSettings!.postCreationLevel = s;
2020-05-01 19:13:31 +00:00
_updateSettings();
}),
// Groups members list visibility
SettingsTile.switchTile(
title: tr("Make members list public"),
onToggle: (s) {
_groupSettings!.isMembersListPublic = s;
_updateSettings();
},
switchValue: _groupSettings!.isMembersListPublic,
2021-04-17 06:40:33 +00:00
titleMaxLines: 2,
)
2020-05-01 18:52:24 +00:00
],
);
2020-05-02 06:51:34 +00:00
2021-04-06 15:04:55 +00:00
List<MultiChoiceEntry<GroupMembershipLevel>>
get _conversationMinMembershipLevel => [
MultiChoiceEntry(
id: GroupMembershipLevel.ADMINISTRATOR,
title: tr("Administrators only")!,
2021-04-06 15:04:55 +00:00
subtitle: tr(
"Only the administrators of the group can access the conversation"),
),
MultiChoiceEntry(
id: GroupMembershipLevel.MODERATOR,
title: tr("Moderators and administrators")!,
2021-04-06 15:04:55 +00:00
subtitle: tr(
"Only moderators and administrators of the group can access the conversation"),
),
MultiChoiceEntry(
id: GroupMembershipLevel.MEMBER,
title: tr("All members")!,
2021-04-06 15:04:55 +00:00
subtitle: tr(
"All the members of the group can access the conversation"),
),
];
SettingsSection _buildConversationsArea() => SettingsSection(
title: tr("Group conversations"),
tiles: _groupSettings!.conversations!
2021-04-06 15:40:13 +00:00
.map(
(e) {
SettingsTile tile =
MultiChoicesSettingsTile<GroupMembershipLevel?>(
title: e.name!,
2021-04-06 15:40:13 +00:00
choices: _conversationMinMembershipLevel,
currentValue: e.groupMinMembershipLevel,
leading: e.hasLogo
? CachedNetworkImage(
imageUrl: e.logoURL!,
2021-04-06 15:40:13 +00:00
width: 30,
)
: Icon(Icons.group, size: 30),
onChanged: (c) => _changeConversationVisibility(e, c),
2021-04-06 15:46:05 +00:00
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () => _deleteConversation(e),
),
2021-04-06 15:40:13 +00:00
);
return tile;
},
)
.toList()
.cast<SettingsTile>()
2021-12-28 15:34:14 +00:00
..add(
SettingsTile(
title: tr("Create a new conversation"),
onPressed: _createNewGroupConversation,
),
),
2021-04-06 15:04:55 +00:00
);
void _createNewGroupConversation(BuildContext context) async {
try {
final name = await askUserString(
context: context,
title: tr("New conversation name")!,
message: tr("Please give a name to the new conversation")!,
2021-04-06 15:04:55 +00:00
defaultValue: "",
hint: tr("Name")!,
2021-04-06 15:04:55 +00:00
minLength: 1,
maxLength: ServerConfigurationHelper
.config!.conversationsPolicy.maxConversationNameLen,
2021-04-06 15:04:55 +00:00
);
if (name == null) return;
final visibility = await showMultiChoicesDialog(
context: context,
choices: _conversationMinMembershipLevel,
defaultChoice: GroupMembershipLevel.MEMBER,
title: tr("Conversation visibility"),
);
if (visibility == null) return;
await GroupsHelper.createGroupConversation(NewGroupConversation(
groupID: _groupSettings!.id,
2021-04-06 15:04:55 +00:00
name: name,
minMembershipLevel: visibility,
));
_key.currentState!.refresh();
2021-04-06 15:04:55 +00:00
} catch (e, s) {
logError(e, s);
snack(context, tr("Failed to create a conversation!")!);
2021-04-06 15:04:55 +00:00
}
}
2021-04-06 15:40:13 +00:00
void _changeConversationVisibility(
Conversation conv, GroupMembershipLevel? newLevel) async {
2021-04-06 15:40:13 +00:00
try {
await GroupsHelper.setConversationVisibility(conv.id, newLevel);
_key.currentState!.refresh();
2021-04-06 15:40:13 +00:00
} catch (e, s) {
logError(e, s);
snack(context, tr("Failed to change conversation visibility level!")!);
2021-04-06 15:40:13 +00:00
}
}
2021-04-06 15:46:05 +00:00
void _deleteConversation(Conversation conv) async {
try {
if (!await showConfirmDialog(
context: context,
message: tr("Do you really want to delete this conversation?")))
return;
await GroupsHelper.deleteConversation(conv.id);
_key.currentState!.refresh();
2021-04-06 15:46:05 +00:00
} catch (e, s) {
logError(e, s);
snack(context, tr("Failed to delete conversation!")!);
2021-04-06 15:46:05 +00:00
}
}
SettingsSection _buildGroupLogoArea() {
2020-05-02 06:51:34 +00:00
return SettingsSection(
title: tr("Group logo"),
tiles: [
// Current logo
SettingsTile(
title: tr("Current logo"),
leading: GroupIcon(group: _groupSettings!),
2020-05-02 06:51:34 +00:00
),
// Upload a new logo
SettingsTile(
title: tr("Upload a new logo"),
2021-03-13 17:03:20 +00:00
onPressed: (_) => _uploadNewLogo(),
2020-05-02 06:51:34 +00:00
),
// Generate a new random logo
SettingsTile(
title: tr("Generate a new random logo"),
2021-03-13 17:03:20 +00:00
onPressed: (_) => _generateRandomLogo(),
2020-05-02 07:15:02 +00:00
),
// Delete current logo
SettingsTile(
title: tr("Delete logo"),
2021-03-13 17:03:20 +00:00
onPressed: (_) => _deleteLogo(),
2020-05-02 07:15:02 +00:00
),
2020-05-02 06:51:34 +00:00
],
);
}
/// Upload a new logo for the group
2020-05-02 06:51:34 +00:00
void _uploadNewLogo() async {
try {
final logo = await pickImage(context);
2021-03-13 17:03:20 +00:00
if (logo == null) return;
await _doUploadLogo(logo.bytes as Uint8List?);
2020-05-02 06:51:34 +00:00
} catch (e, stack) {
print("Could not upload new logo! $e\n$stack");
showSimpleSnack(context, tr("Could not upload new logo!")!);
2020-05-02 06:51:34 +00:00
}
}
/// Generate a new random logo for the group
void _generateRandomLogo() async {
try {
2021-12-28 15:34:14 +00:00
final newLogo = await genIdenticon(context);
await _doUploadLogo(newLogo);
} catch (e, stack) {
print("Could not generate new logo! $e\n$stack");
showSimpleSnack(context, tr("Could not generate new random logo!")!);
}
}
Future<void> _doUploadLogo(Uint8List? bytes) async {
await GroupsHelper.uploadNewLogo(_groupSettings!.id, bytes);
_key.currentState!.refresh();
2020-05-02 06:51:34 +00:00
}
2020-05-02 07:15:02 +00:00
/// Delete previous group logo
void _deleteLogo() async {
try {
if (!await showConfirmDialog(
context: context,
message: tr("Do you really want to delete the logo of this group ?")))
return;
await GroupsHelper.deleteLogo(_groupSettings!.id);
_key.currentState!.refresh();
2020-05-02 07:15:02 +00:00
} catch (e, s) {
print("Could not delete group logo! $e\n$s");
showSimpleSnack(context, tr("Could not delete group logo!")!);
2020-05-02 07:15:02 +00:00
}
}
2020-05-02 07:45:03 +00:00
SettingsSection _buildDangerZone() {
2020-05-02 07:45:03 +00:00
return SettingsSection(
title: tr("Danger zone"),
tiles: [
SettingsTile(
title: tr("Delete group"),
2021-03-13 17:03:20 +00:00
onPressed: (_) => _deleteGroup(),
2020-05-02 07:45:03 +00:00
),
],
);
}
/// Delete the group
void _deleteGroup() async {
try {
final password = await showUserPasswordDialog(context);
if (password == null) return;
if (!await showConfirmDialog(
context: context,
message: tr(
"Do you really want to delete this group ? All the posts related to it will be permanently deleted!")))
return;
await GroupsHelper.deleteGroup(_groupSettings!.id, password);
2020-05-02 07:45:03 +00:00
MainController.of(context)!.popPage();
2020-05-02 07:45:03 +00:00
} catch (e, s) {
print("Could not delete the group! $e\n$s");
showSimpleSnack(context, tr("Could not delete the group")!);
2020-05-02 07:45:03 +00:00
}
}
2020-05-01 13:12:55 +00:00
}