diff --git a/lib/pages/chat_details/chat_details.dart b/lib/pages/chat_details/chat_details.dart index db515dbd..e2c883e1 100644 --- a/lib/pages/chat_details/chat_details.dart +++ b/lib/pages/chat_details/chat_details.dart @@ -10,6 +10,7 @@ import 'package:matrix/matrix.dart'; import 'package:vrouter/vrouter.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/platform_infos.dart'; import 'package:fluffychat/widgets/matrix.dart'; @@ -255,10 +256,50 @@ class ChatDetailsController extends State { } 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( + 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; if (PlatformInfos.isMobile) { final result = await ImagePicker().pickImage( - source: ImageSource.gallery, + source: action == AvatarAction.camera + ? ImageSource.camera + : ImageSource.gallery, imageQuality: 50, maxWidth: 1600, maxHeight: 1600); @@ -268,25 +309,18 @@ class ChatDetailsController extends State { name: result.path, ); } else { - final result = await FilePickerCross.importFromStorage( - type: FileTypeCross.image, - ); + final result = + await FilePickerCross.importFromStorage(type: FileTypeCross.image); if (result == null) return; file = MatrixFile( bytes: result.toUint8List(), name: result.fileName, ); } - final room = Matrix.of(context).client.getRoomById(roomId); - - final success = await showFutureLoadingDialog( + await showFutureLoadingDialog( 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 { diff --git a/lib/pages/settings/settings.dart b/lib/pages/settings/settings.dart index 891a4842..ef6afdc4 100644 --- a/lib/pages/settings/settings.dart +++ b/lib/pages/settings/settings.dart @@ -37,30 +37,34 @@ class SettingsController extends State { }); void setAvatarAction() async { - final action = await showModalActionSheet( - context: context, - title: L10n.of(context).changeYourAvatar, - actions: [ + 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 (profile?.avatarUrl != null) SheetAction( - key: AvatarAction.file, - label: L10n.of(context).openGallery, - icon: Icons.photo_outlined, + key: AvatarAction.remove, + label: L10n.of(context).removeYourAvatar, + isDestructiveAction: true, + icon: Icons.delete_outlined, ), - if (profile?.avatarUrl != null) - SheetAction( - key: AvatarAction.remove, - label: L10n.of(context).removeYourAvatar, - isDestructiveAction: true, - icon: Icons.delete_outlined, - ), - ], - ); + ]; + final action = actions.length == 1 + ? actions.single + : await showModalActionSheet( + context: context, + title: L10n.of(context).changeYourAvatar, + actions: actions, + ); if (action == null) return; final matrix = Matrix.of(context); if (action == AvatarAction.remove) {