From b7cec7a3e90d3149def04bf2bc7942327fae9cc4 Mon Sep 17 00:00:00 2001 From: Terence ZAFINDRATAFA Date: Fri, 3 May 2024 17:54:55 +0200 Subject: [PATCH] TW-1699: change avatar updated --- .../model/extensions/xfile_extension.dart | 15 +++++-- .../settings_profile/settings_profile.dart | 45 ++++++++++++++++++- .../settings_profile_view_mobile.dart | 4 +- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/lib/domain/model/extensions/xfile_extension.dart b/lib/domain/model/extensions/xfile_extension.dart index d36f7c5f92..684045c614 100644 --- a/lib/domain/model/extensions/xfile_extension.dart +++ b/lib/domain/model/extensions/xfile_extension.dart @@ -1,9 +1,9 @@ +import 'package:file_picker/file_picker.dart'; import 'package:file_selector/file_selector.dart'; import 'package:matrix/matrix.dart'; extension XFileExtension on XFile { - - Future toMatrixFile() async{ + Future toMatrixFile() async { return MatrixFile.fromMimeType( bytes: await readAsBytes(), mimeType: mimeType, @@ -12,4 +12,13 @@ extension XFileExtension on XFile { sizeInBytes: await length(), ); } -} \ No newline at end of file + + Future toPlatformFile() async { + return PlatformFile.fromMap({ + 'name': name, + 'path': path, + 'bytes': await readAsBytes(), + 'size': await length(), + }); + } +} diff --git a/lib/pages/settings_dashboard/settings_profile/settings_profile.dart b/lib/pages/settings_dashboard/settings_profile/settings_profile.dart index ada07b9f99..e1b20ba300 100644 --- a/lib/pages/settings_dashboard/settings_profile/settings_profile.dart +++ b/lib/pages/settings_dashboard/settings_profile/settings_profile.dart @@ -7,6 +7,7 @@ import 'package:fluffychat/di/global/get_it_initializer.dart'; import 'package:fluffychat/domain/app_state/room/upload_content_state.dart'; import 'package:fluffychat/domain/app_state/settings/update_profile_failure.dart'; import 'package:fluffychat/domain/app_state/settings/update_profile_success.dart'; +import 'package:fluffychat/domain/model/extensions/xfile_extension.dart'; import 'package:fluffychat/domain/usecase/room/upload_content_for_web_interactor.dart'; import 'package:fluffychat/domain/usecase/room/upload_content_interactor.dart'; import 'package:fluffychat/domain/usecase/settings/update_profile_interactor.dart'; @@ -33,6 +34,8 @@ import 'package:linagora_design_flutter/images_picker/asset_counter.dart'; import 'package:linagora_design_flutter/linagora_design_flutter.dart'; import 'package:matrix/matrix.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; +import 'package:file_selector/file_selector.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:wechat_camera_picker/wechat_camera_picker.dart'; class SettingsProfile extends StatefulWidget { @@ -180,7 +183,43 @@ class SettingsProfileController extends State ), ); Logs().d( - 'SettingsProfile::_getImageOnWeb(): AvatarWebNotifier - $result', + 'SettingsProfile::_getImageOnWeb(): AvatarNotifier - $result', + ); + } + } + + void _getImageOnDesktop( + BuildContext context, + ) async { + const XTypeGroup typeGroup = XTypeGroup( + label: 'images', + extensions: ['jpg', 'png'], + ); + final String initialDirectory = + (await getApplicationDocumentsDirectory()).path; + + final XFile? result = await openFile( + initialDirectory: initialDirectory, + acceptedTypeGroups: [typeGroup], + ); + + Logs().d( + 'SettingsProfile::_getImageOnDesktop(): FilePickerResult - ${result?.path}', + ); + + if (result == null) { + return; + } else { + if (!isEditedProfileNotifier.value) { + isEditedProfileNotifier.toggle(); + } + settingsProfileUIState.value = Right( + GetAvatarInBytesUIStateSuccess( + filePickerResult: FilePickerResult([await result.toPlatformFile()]), + ), + ); + Logs().d( + 'SettingsProfile::_getImageOnDesktop(): AvatarNotifier - $result', ); } } @@ -190,6 +229,10 @@ class SettingsProfileController extends State _getImageOnWeb(context); return; } + if (PlatformInfos.isDesktop) { + _getImageOnDesktop(context); + return; + } final currentPermissionPhotos = await getCurrentMediaPermission(); if (currentPermissionPhotos != null) { final imagePickerController = createImagePickerController(); diff --git a/lib/pages/settings_dashboard/settings_profile/settings_profile_view_mobile.dart b/lib/pages/settings_dashboard/settings_profile/settings_profile_view_mobile.dart index c3d36bcc7b..184f03d67a 100644 --- a/lib/pages/settings_dashboard/settings_profile/settings_profile_view_mobile.dart +++ b/lib/pages/settings_dashboard/settings_profile/settings_profile_view_mobile.dart @@ -93,7 +93,7 @@ class SettingsProfileViewMobile extends StatelessWidget { ); } if (success is GetAvatarInBytesUIStateSuccess && - PlatformInfos.isWeb) { + PlatformInfos.isWebOrDesktop) { if (success.filePickerResult == null || success.filePickerResult?.files.single.bytes == null) { @@ -160,7 +160,7 @@ class SettingsProfileViewMobile extends StatelessWidget { ) { return GestureDetector( onTap: () { - if (PlatformInfos.isWeb) { + if (PlatformInfos.isWebOrDesktop) { menuController.isOpen ? menuController.close() : menuController.open();