1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Can change conversation visibility

This commit is contained in:
Pierre HUBERT 2021-04-06 17:40:13 +02:00
parent 054b2a1d32
commit 5773981750
5 changed files with 71 additions and 6 deletions

View File

@ -1,9 +1,11 @@
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/lists/group_members_list.dart'; import 'package:comunic/lists/group_members_list.dart';
import 'package:comunic/lists/groups_list.dart'; import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/models/advanced_group_info.dart'; import 'package:comunic/models/advanced_group_info.dart';
import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/conversation.dart';
import 'package:comunic/models/group.dart'; import 'package:comunic/models/group.dart';
import 'package:comunic/models/group_membership.dart'; import 'package:comunic/models/group_membership.dart';
import 'package:comunic/models/new_group_conversation.dart'; import 'package:comunic/models/new_group_conversation.dart';
@ -345,6 +347,20 @@ class GroupsHelper {
.addString("name", conv.name) .addString("name", conv.name)
.execWithThrow(); .execWithThrow();
/// Set new conversation visibility level
///
/// Throws in case of failure
static Future<void> setConversationVisibility(
int convID, GroupMembershipLevel newLevel) async =>
await APIRequest.withLogin("groups/set_conversation_visibility")
.addInt("conv_id", convID)
.addString(
"min_membership_level",
APIGroupsMembershipLevelsMap.entries
.firstWhere((e) => e.value == newLevel)
.key)
.execWithThrow();
/// Turn an API entry into a group object /// Turn an API entry into a group object
Group _getGroupFromAPI(Map<String, dynamic> map) { Group _getGroupFromAPI(Map<String, dynamic> map) {
return Group( return Group(
@ -381,6 +397,10 @@ class GroupsHelper {
url: nullToEmpty(map["url"]), url: nullToEmpty(map["url"]),
likes: map["number_likes"], likes: map["number_likes"],
userLike: map["is_liking"], userLike: map["is_liking"],
conversations: map["conversations"]
.map((s) => ConversationsHelper.apiToConversation(s))
.cast<Conversation>()
.toList(),
); );
/// Create [GroupMembership] object from API entry /// Create [GroupMembership] object from API entry

View File

@ -35,6 +35,7 @@ class AdvancedGroupInfo extends Group implements LikeElement {
@required this.url, @required this.url,
@required this.likes, @required this.likes,
@required this.userLike, @required this.userLike,
@required this.conversations,
}) : super( }) : super(
id: id, id: id,
name: name, name: name,

View File

@ -77,6 +77,8 @@ class Conversation extends SerializableElement<Conversation> {
/// Check out whether a conversation is managed or not /// Check out whether a conversation is managed or not
bool get isManaged => groupID != null; bool get isManaged => groupID != null;
bool get hasLogo => logoURL != null;
Conversation.fromJson(Map<String, dynamic> map) Conversation.fromJson(Map<String, dynamic> map)
: id = map["id"], : id = map["id"],
name = map["name"], name = map["name"],

View File

@ -1,8 +1,10 @@
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:comunic/helpers/groups_helper.dart'; import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart'; import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/models/advanced_group_info.dart'; import 'package:comunic/models/advanced_group_info.dart';
import 'package:comunic/models/conversation.dart';
import 'package:comunic/models/group.dart'; import 'package:comunic/models/group.dart';
import 'package:comunic/models/new_group_conversation.dart'; import 'package:comunic/models/new_group_conversation.dart';
import 'package:comunic/ui/dialogs/input_user_password_dialog.dart'; import 'package:comunic/ui/dialogs/input_user_password_dialog.dart';
@ -284,12 +286,34 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
SettingsSection _buildConversationsArea() => SettingsSection( SettingsSection _buildConversationsArea() => SettingsSection(
title: tr("Group conversations"), title: tr("Group conversations"),
tiles: [ tiles: _groupSettings.conversations
.map(
(e) {
SettingsTile tile =
MultiChoicesSettingsTile<GroupMembershipLevel>(
title: e.name,
choices: _conversationMinMembershipLevel,
currentValue: e.groupMinMembershipLevel,
leading: e.hasLogo
? CachedNetworkImage(
imageUrl: e.logoURL,
width: 30,
)
: Icon(Icons.group, size: 30),
onChanged: (c) => _changeConversationVisibility(e, c),
);
return tile;
},
)
.toList()
.cast<SettingsTile>()
..add(
SettingsTile( SettingsTile(
title: tr("Create a new conversation"), title: tr("Create a new conversation"),
onPressed: _createNewGroupConversation, onPressed: _createNewGroupConversation,
), ),
], ),
); );
void _createNewGroupConversation(BuildContext context) async { void _createNewGroupConversation(BuildContext context) async {
@ -329,6 +353,18 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
} }
} }
void _changeConversationVisibility(
Conversation conv, GroupMembershipLevel newLevel) async {
try {
await GroupsHelper.setConversationVisibility(conv.id, newLevel);
_key.currentState.refresh();
} catch (e, s) {
logError(e, s);
snack(context, tr("Failed to change conversation visibility level!"));
}
}
Widget _buildGroupLogoArea() { Widget _buildGroupLogoArea() {
return SettingsSection( return SettingsSection(
title: tr("Group logo"), title: tr("Group logo"),

View File

@ -11,6 +11,8 @@ class MultiChoicesSettingsTile<T> extends SettingsTile {
final List<MultiChoiceEntry<T>> choices; final List<MultiChoiceEntry<T>> choices;
final T currentValue; final T currentValue;
final Function(T) onChanged; final Function(T) onChanged;
final Widget leading;
final Widget trailing;
const MultiChoicesSettingsTile({ const MultiChoicesSettingsTile({
Key key, Key key,
@ -18,6 +20,8 @@ class MultiChoicesSettingsTile<T> extends SettingsTile {
@required this.choices, @required this.choices,
@required this.currentValue, @required this.currentValue,
@required this.onChanged, @required this.onChanged,
this.leading,
this.trailing,
}) : assert(title != null), }) : assert(title != null),
assert(choices != null), assert(choices != null),
assert(currentValue != null), assert(currentValue != null),
@ -26,6 +30,8 @@ class MultiChoicesSettingsTile<T> extends SettingsTile {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SettingsTile( return SettingsTile(
leading: leading,
trailing: trailing,
title: title, title: title,
subtitle: choices.firstWhere((f) => f.id == currentValue).title, subtitle: choices.firstWhere((f) => f.id == currentValue).title,
onPressed: (_) => _changeValue(context), onPressed: (_) => _changeValue(context),