1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-22 22:43:22 +00:00

Can change conversation image

This commit is contained in:
Pierre HUBERT 2021-03-13 11:33:25 +01:00
parent cbbda7237b
commit 642f5e11fc
5 changed files with 71 additions and 2 deletions

View File

@ -100,6 +100,15 @@ class ConversationsHelper {
.removeElement((t) => t.id == settings.convID);
}
/// Set a new conversation logo
///
/// Throws in case of failure
static Future<void> changeImage(int convID, BytesFile file) async =>
await APIRequest.withLogin("conversations/change_image")
.addInt("convID", convID)
.addBytesFile("file", file)
.execWithFilesAndThrow();
/// Delete a conversation specified by its [id]
Future<void> deleteConversation(int id) async =>
await APIRequest.withLogin("conversations/delete")

View File

@ -64,6 +64,8 @@ class ServerConfigurationHelper {
conversationsPolicy["max_message_image_height"],
maxThumbnailWidth: conversationsPolicy["max_thumbnail_width"],
maxThumbnailHeight: conversationsPolicy["max_thumbnail_height"],
maxLogoWidth: conversationsPolicy["max_logo_width"],
maxLogoHeight: conversationsPolicy["max_logo_height"],
));
}

View File

@ -68,6 +68,8 @@ class ConversationsPolicy {
final int maxMessageImageHeight;
final int maxThumbnailWidth;
final int maxThumbnailHeight;
final int maxLogoWidth;
final int maxLogoHeight;
const ConversationsPolicy({
@required this.minMessageLen,
@ -80,6 +82,8 @@ class ConversationsPolicy {
@required this.maxMessageImageHeight,
@required this.maxThumbnailWidth,
@required this.maxThumbnailHeight,
@required this.maxLogoWidth,
@required this.maxLogoHeight,
}) : assert(minMessageLen != null),
assert(maxMessageLen != null),
assert(allowedFilesType != null),
@ -89,7 +93,9 @@ class ConversationsPolicy {
assert(maxMessageImageWidth != null),
assert(maxMessageImageHeight != null),
assert(maxThumbnailWidth != null),
assert(maxThumbnailHeight != null);
assert(maxThumbnailHeight != null),
assert(maxLogoWidth != null),
assert(maxLogoHeight != null);
}
class ServerConfig {

View File

@ -173,7 +173,7 @@ Future<BytesFile> showPickFileDialog({
if (file == null) return null;
// Check file size
if (file.bytes.length > maxFileSize) {
if (maxFileSize != null && file.bytes.length > maxFileSize) {
showSimpleSnack(
context,
tr("This file could not be sent: it is too big! (Max allowed size: %1%)",

View File

@ -1,4 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation.dart';
@ -6,6 +8,7 @@ import 'package:comunic/models/new_conversation.dart';
import 'package:comunic/models/new_conversation_settings.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/dialogs/color_picker_dialog.dart';
import 'package:comunic/ui/dialogs/pick_file_dialog.dart';
import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/tiles/simple_user_tile.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart';
@ -46,6 +49,7 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
Set<int> _admins = Set();
bool _followConversation = true;
bool _canEveryoneAddMembers = true;
String _image;
String get _conversationColor => _colorController.text;
@ -87,6 +91,7 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
_admins = _conversation.adminsID;
_followConversation = _conversation.following;
_canEveryoneAddMembers = _conversation.canEveryoneAddMembers;
_image = _conversation.logoURL;
setState(() {});
}
@ -170,6 +175,9 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
],
),
// Conversation image
isUpdating ? _buildConversationImageWidget() : Container(),
// Add a member to the conversation
PickUserWidget(
resetOnChoose: true,
@ -314,4 +322,48 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
snack(context, tr("Failed to update conversation settings!"));
}
}
/// Conversation image management
Widget _buildConversationImageWidget() => Column(
children: [
SizedBox(height: 10),
Text(tr("Conversation logo"),
style: TextStyle(fontWeight: FontWeight.bold)),
SizedBox(height: 5),
_image == null
? Text("No logo defined yet.")
: CachedNetworkImage(imageUrl: _image),
SizedBox(height: 5),
isAdmin
? OutlineButton(
onPressed: _uploadNewLogo,
child: Text(tr("Change logo")),
)
: Container(),
SizedBox(height: 10),
],
);
/// Upload new conversation logo
Future<void> _uploadNewLogo() async {
try {
final newLogo = await showPickFileDialog(
context: context,
allowedMimeTypes: ["image/png", "image/jpeg", "image/gif"],
imageMaxWidth: srvConfig.conversationsPolicy.maxLogoWidth,
imageMaxHeight: srvConfig.conversationsPolicy.maxLogoHeight,
);
if (newLogo == null) return;
await ConversationsHelper.changeImage(_conversation.id, newLogo);
final newConvSettings =
await ConversationsHelper().getSingle(_conversation.id, force: true);
setState(() => _image = newConvSettings.logoURL);
} catch (e, s) {
logError(e, s);
snack(context, tr("Failed to change conversation logo !"));
}
}
}