fix: Set room avatar

This commit is contained in:
Krille Fear 2021-11-15 07:43:19 +01:00
parent d49d44dc80
commit b587827589
2 changed files with 66 additions and 28 deletions

View File

@ -10,6 +10,7 @@ import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart'; import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/pages/chat_details/chat_details_view.dart'; import 'package:fluffychat/pages/chat_details/chat_details_view.dart';
import 'package:fluffychat/pages/settings/settings.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/matrix_locals.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions.dart/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/matrix.dart'; import 'package:fluffychat/widgets/matrix.dart';
@ -255,10 +256,50 @@ class ChatDetailsController extends State<ChatDetails> {
} }
void setAvatarAction() async { void setAvatarAction() async {
final room = Matrix.of(context).client.getRoomById(roomId);
final actions = [
if (PlatformInfos.isMobile)
SheetAction(
key: AvatarAction.camera,
label: L10n.of(context).openCamera,
isDefaultAction: true,
icon: Icons.camera_alt_outlined,
),
SheetAction(
key: AvatarAction.file,
label: L10n.of(context).openGallery,
icon: Icons.photo_outlined,
),
if (room?.avatar != null)
SheetAction(
key: AvatarAction.remove,
label: L10n.of(context).delete,
isDestructiveAction: true,
icon: Icons.delete_outlined,
),
];
final action = actions.length == 1
? actions.single
: await showModalActionSheet<AvatarAction>(
context: context,
title: L10n.of(context).editRoomAvatar,
actions: actions,
);
if (action == null) return;
final matrix = Matrix.of(context);
if (action == AvatarAction.remove) {
await showFutureLoadingDialog(
context: context,
future: () => matrix.client.setAvatarUrl(matrix.client.userID, null),
);
return;
}
MatrixFile file; MatrixFile file;
if (PlatformInfos.isMobile) { if (PlatformInfos.isMobile) {
final result = await ImagePicker().pickImage( final result = await ImagePicker().pickImage(
source: ImageSource.gallery, source: action == AvatarAction.camera
? ImageSource.camera
: ImageSource.gallery,
imageQuality: 50, imageQuality: 50,
maxWidth: 1600, maxWidth: 1600,
maxHeight: 1600); maxHeight: 1600);
@ -268,25 +309,18 @@ class ChatDetailsController extends State<ChatDetails> {
name: result.path, name: result.path,
); );
} else { } else {
final result = await FilePickerCross.importFromStorage( final result =
type: FileTypeCross.image, await FilePickerCross.importFromStorage(type: FileTypeCross.image);
);
if (result == null) return; if (result == null) return;
file = MatrixFile( file = MatrixFile(
bytes: result.toUint8List(), bytes: result.toUint8List(),
name: result.fileName, name: result.fileName,
); );
} }
final room = Matrix.of(context).client.getRoomById(roomId); await showFutureLoadingDialog(
final success = await showFutureLoadingDialog(
context: context, context: context,
future: () => room.setAvatar(file), future: () => matrix.client.setAvatar(file),
); );
if (success.error == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(L10n.of(context).avatarHasBeenChanged)));
}
} }
void requestMoreMembersAction() async { void requestMoreMembersAction() async {

View File

@ -37,30 +37,34 @@ class SettingsController extends State<Settings> {
}); });
void setAvatarAction() async { void setAvatarAction() async {
final action = await showModalActionSheet<AvatarAction>( final actions = [
context: context, if (PlatformInfos.isMobile)
title: L10n.of(context).changeYourAvatar,
actions: [
SheetAction( SheetAction(
key: AvatarAction.camera, key: AvatarAction.camera,
label: L10n.of(context).openCamera, label: L10n.of(context).openCamera,
isDefaultAction: true, isDefaultAction: true,
icon: Icons.camera_alt_outlined, icon: Icons.camera_alt_outlined,
), ),
SheetAction(
key: AvatarAction.file,
label: L10n.of(context).openGallery,
icon: Icons.photo_outlined,
),
if (profile?.avatarUrl != null)
SheetAction( SheetAction(
key: AvatarAction.file, key: AvatarAction.remove,
label: L10n.of(context).openGallery, label: L10n.of(context).removeYourAvatar,
icon: Icons.photo_outlined, isDestructiveAction: true,
icon: Icons.delete_outlined,
), ),
if (profile?.avatarUrl != null) ];
SheetAction( final action = actions.length == 1
key: AvatarAction.remove, ? actions.single
label: L10n.of(context).removeYourAvatar, : await showModalActionSheet<AvatarAction>(
isDestructiveAction: true, context: context,
icon: Icons.delete_outlined, title: L10n.of(context).changeYourAvatar,
), actions: actions,
], );
);
if (action == null) return; if (action == null) return;
final matrix = Matrix.of(context); final matrix = Matrix.of(context);
if (action == AvatarAction.remove) { if (action == AvatarAction.remove) {