1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 14:59:22 +00:00

Can upload a new logo for the group

This commit is contained in:
Pierre HUBERT 2020-05-02 08:51:34 +02:00
parent f450a46e99
commit 1a53a26f39
3 changed files with 56 additions and 0 deletions

View File

@ -1,3 +1,5 @@
import 'dart:typed_data';
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';
@ -226,6 +228,15 @@ class GroupsHelper {
.execWithThrow(); .execWithThrow();
} }
/// Upload a new logo
///
/// Throws in case of failure
static Future<void> uploadNewLogo(int groupID, Uint8List bytes) async =>
await APIRequest(uri: "groups/upload_logo", needLogin: true)
.addInt("id", groupID)
.addBytesFile("logo", BytesFile("logo.png", bytes))
.execWithFilesAndThrow();
/// 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(

View File

@ -71,4 +71,8 @@ class APIRequest {
/// Execute the request with files /// Execute the request with files
Future<APIResponse> execWithFiles() async => APIHelper().execWithFiles(this); Future<APIResponse> execWithFiles() async => APIHelper().execWithFiles(this);
/// Execute the request with files to send & throws in case of failure
Future<APIResponse> execWithFilesAndThrow() async =>
(await execWithFiles()).assertOk();
} }

View File

@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:comunic/helpers/groups_helper.dart'; import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/models/advanced_group_info.dart'; import 'package:comunic/models/advanced_group_info.dart';
import 'package:comunic/models/group.dart'; import 'package:comunic/models/group.dart';
@ -5,9 +7,11 @@ import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
import 'package:comunic/ui/dialogs/virtual_directory_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/async_screen_widget.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart'; import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
import 'package:comunic/ui/widgets/group_icon_widget.dart';
import 'package:comunic/ui/widgets/safe_state.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/multi_choices_settings_tile.dart';
import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart'; import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart';
import 'package:comunic/utils/files_utils.dart';
import 'package:comunic/utils/input_utils.dart'; import 'package:comunic/utils/input_utils.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart'; import 'package:comunic/utils/ui_utils.dart';
@ -75,6 +79,7 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
sections: [ sections: [
_buildGeneralSection(), _buildGeneralSection(),
_buildAccessRestrictions(), _buildAccessRestrictions(),
_buildGroupLogoArea()
], ],
); );
} }
@ -233,4 +238,40 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
}), }),
], ],
); );
Widget _buildGroupLogoArea() {
return SettingsSection(
title: tr("Group logo"),
tiles: [
// Current logo
SettingsTile(
title: tr("Current logo"),
leading: GroupIcon(group: _groupSettings),
),
// Upload a new logo
SettingsTile(
title: tr("Upload a new logo"),
onTap: _uploadNewLogo,
),
],
);
}
// Upload a new logo for the group
void _uploadNewLogo() async {
try {
final logo = await pickImage(context);
final bytes = logo.readAsBytesSync();
await _doUploadLogo(bytes);
} catch (e, stack) {
print("Could not upload new logo! $e\n$stack");
showSimpleSnack(context, tr("Could not upload new logo!"));
}
}
Future<void> _doUploadLogo(Uint8List bytes) async {
await GroupsHelper.uploadNewLogo(_groupSettings.id, bytes);
_key.currentState.refresh();
}
} }