From 2d86780f0b891169620346d7f23d87dd81885b68 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 1 May 2020 20:52:24 +0200 Subject: [PATCH] Can change group visibility --- lib/models/group.dart | 2 +- lib/ui/screens/group_settings_screen.dart | 41 ++++++++++++++++- .../settings/multi_choices_settings_tile.dart | 46 +++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 lib/ui/widgets/settings/multi_choices_settings_tile.dart diff --git a/lib/models/group.dart b/lib/models/group.dart index 3f56bfa..047b0df 100644 --- a/lib/models/group.dart +++ b/lib/models/group.dart @@ -25,7 +25,7 @@ class Group { final String iconURL; final int numberMembers; GroupMembershipLevel membershipLevel; - final GroupVisibilityLevel visibilityLevel; + GroupVisibilityLevel visibilityLevel; final GroupRegistrationLevel registrationLevel; final GroupPostCreationLevel postCreationLevel; String virtualDirectory; diff --git a/lib/ui/screens/group_settings_screen.dart b/lib/ui/screens/group_settings_screen.dart index 7831ba2..a44f347 100644 --- a/lib/ui/screens/group_settings_screen.dart +++ b/lib/ui/screens/group_settings_screen.dart @@ -1,9 +1,12 @@ import 'package:comunic/helpers/groups_helper.dart'; import 'package:comunic/models/advanced_group_info.dart'; +import 'package:comunic/models/group.dart'; +import 'package:comunic/ui/dialogs/multi_choices_dialog.dart'; import 'package:comunic/ui/dialogs/virtual_directory_dialog.dart'; import 'package:comunic/ui/widgets/async_screen_widget.dart'; import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/ui/widgets/safe_state.dart'; +import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart'; import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart'; import 'package:comunic/utils/input_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; @@ -69,7 +72,10 @@ class _GroupSettingsScreenState extends SafeState { Widget _buildContent() { return SettingsList( - sections: [_buildGeneralSection()], + sections: [ + _buildGeneralSection(), + _buildAccessRestrictions(), + ], ); } @@ -137,4 +143,37 @@ class _GroupSettingsScreenState extends SafeState { ], ); } + + List> get _visibilityLevels => [ + MultiChoiceEntry( + id: GroupVisibilityLevel.OPEN, + title: tr("Open group"), + subtitle: + tr("Group information & public posts are available to everyone."), + ), + MultiChoiceEntry( + id: GroupVisibilityLevel.PRIVATE, + title: tr("Private groupe"), + subtitle: tr("The group is accessible to accepted members only."), + ), + MultiChoiceEntry( + id: GroupVisibilityLevel.SECRETE, + title: tr("Secrete groupe"), + subtitle: tr("The group is visible only to invited members."), + ), + ]; + + Widget _buildAccessRestrictions() => SettingsSection( + title: tr("Access restrictions"), + tiles: [ + MultiChoicesSettingsTile( + title: tr("Group visibility"), + choices: _visibilityLevels, + currentValue: _groupSettings.visibilityLevel, + onChanged: (v) { + _groupSettings.visibilityLevel = v; + _updateSettings(); + }) + ], + ); } diff --git a/lib/ui/widgets/settings/multi_choices_settings_tile.dart b/lib/ui/widgets/settings/multi_choices_settings_tile.dart new file mode 100644 index 0000000..57e1d8e --- /dev/null +++ b/lib/ui/widgets/settings/multi_choices_settings_tile.dart @@ -0,0 +1,46 @@ +import 'package:comunic/ui/dialogs/multi_choices_dialog.dart'; +import 'package:flutter/material.dart'; +import 'package:settings_ui/settings_ui.dart'; + +/// Multiple choices settings tile +/// +/// @author Pierre Hubert + +class MultiChoicesSettingsTile extends SettingsTile { + final String title; + final List> choices; + final T currentValue; + final Function(T) onChanged; + + const MultiChoicesSettingsTile({ + Key key, + @required this.title, + @required this.choices, + @required this.currentValue, + @required this.onChanged, + }) : assert(title != null), + assert(choices != null), + assert(currentValue != null), + assert(onChanged != null); + + @override + Widget build(BuildContext context) { + return SettingsTile( + title: title, + subtitle: choices.firstWhere((f) => f.id == currentValue).title, + onTap: () => _changeValue(context), + ); + } + + /// When the user request to change the current value + void _changeValue(BuildContext context) async { + final newChoice = await showMultiChoicesDialog( + context: context, + choices: choices, + defaultChoice: currentValue, + title: title, + ); + if (newChoice == null) return; + onChanged(newChoice); + } +}