From 1e5f7de794fe2eb362c1f55740b9c0bf03b3af72 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sun, 4 Oct 2020 17:01:54 +0200 Subject: [PATCH] fix: Desktop file picker --- lib/views/chat.dart | 83 +++++++++++++------ lib/views/chat_details.dart | 40 ++++++--- lib/views/settings.dart | 38 ++++++--- lib/views/settings_emotes.dart | 33 ++++++-- lib/views/sign_up.dart | 23 ++--- lib/views/sign_up_password.dart | 11 +-- linux/flutter/generated_plugin_registrant.cc | 4 + linux/flutter/generated_plugins.cmake | 1 + pubspec.lock | 37 +++++++-- pubspec.yaml | 3 +- .../flutter/generated_plugin_registrant.cc | 3 + windows/flutter/generated_plugins.cmake | 1 + 12 files changed, 188 insertions(+), 89 deletions(-) diff --git a/lib/views/chat.dart b/lib/views/chat.dart index a2f525b..0dd0283 100644 --- a/lib/views/chat.dart +++ b/lib/views/chat.dart @@ -3,7 +3,8 @@ import 'dart:io'; import 'dart:math'; import 'package:famedlysdk/famedlysdk.dart'; -import 'package:file_picker_platform_interface/file_picker_platform_interface.dart'; + +import 'package:file_picker_cross/file_picker_cross.dart'; import 'package:fluffychat/components/adaptive_page_layout.dart'; import 'package:fluffychat/components/avatar.dart'; import 'package:fluffychat/components/chat_settings_popup_menu.dart'; @@ -17,6 +18,7 @@ import 'package:fluffychat/components/reply_content.dart'; import 'package:fluffychat/config/app_emojis.dart'; import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/utils/matrix_locals.dart'; +import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/room_status_extension.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -24,7 +26,6 @@ import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:image_picker/image_picker.dart'; -import 'package:memoryfilepicker/memoryfilepicker.dart'; import 'package:pedantic/pedantic.dart'; import 'package:scroll_to_index/scroll_to_index.dart'; @@ -211,38 +212,66 @@ class _ChatState extends State<_Chat> { } void sendFileAction(BuildContext context) async { - var file = await MemoryFilePicker.getFile(); - if (file == null) return; + final result = + await FilePickerCross.importFromStorage(type: FileTypeCross.any); + if (result == null) return; await showDialog( - context: context, - builder: (context) => SendFileDialog( - file: - MatrixFile(bytes: file.bytes, name: file.path).detectFileType, - room: room, - )); + context: context, + builder: (context) => SendFileDialog( + file: MatrixFile( + bytes: result.toUint8List(), + name: result.fileName, + ).detectFileType, + room: room, + ), + ); } void sendImageAction(BuildContext context) async { - var file = await MemoryFilePicker.getFile(type: FileType.image); - if (file == null) return; - final bytes = await file.bytes; + MatrixFile file; + if (PlatformInfos.isMobile) { + final result = await ImagePicker().getImage( + source: ImageSource.gallery, + imageQuality: 50, + maxWidth: 1600, + maxHeight: 1600); + if (result == null) return; + file = MatrixFile( + bytes: await result.readAsBytes(), + name: result.path, + ); + } else { + final result = + await FilePickerCross.importFromStorage(type: FileTypeCross.image); + if (result == null) return; + file = MatrixFile( + bytes: result.toUint8List(), + name: result.fileName, + ); + } await showDialog( - context: context, - builder: (context) => SendFileDialog( - file: MatrixImageFile(bytes: bytes, name: file.path), - room: room, - )); + context: context, + builder: (context) => SendFileDialog( + file: file, + room: room, + ), + ); } void openCameraAction(BuildContext context) async { - var file = await MemoryFilePicker.getImage(source: ImageSource.camera); + var file = await ImagePicker().getImage(source: ImageSource.camera); if (file == null) return; + final bytes = await file.readAsBytes(); await showDialog( - context: context, - builder: (context) => SendFileDialog( - file: MatrixImageFile(bytes: file.bytes, name: file.path), - room: room, - )); + context: context, + builder: (context) => SendFileDialog( + file: MatrixImageFile( + bytes: bytes, + name: file.path, + ), + room: room, + ), + ); } void voiceMessageAction(BuildContext context) async { @@ -888,7 +917,7 @@ class _ChatState extends State<_Chat> { contentPadding: EdgeInsets.all(0), ), ), - if (!kIsWeb) + if (PlatformInfos.isMobile) PopupMenuItem( value: 'camera', child: ListTile( @@ -902,7 +931,7 @@ class _ChatState extends State<_Chat> { contentPadding: EdgeInsets.all(0), ), ), - if (!kIsWeb) + if (PlatformInfos.isMobile) PopupMenuItem( value: 'voice', child: ListTile( @@ -972,7 +1001,7 @@ class _ChatState extends State<_Chat> { ), ), ), - if (!kIsWeb && inputText.isEmpty) + if (PlatformInfos.isMobile && inputText.isEmpty) Container( height: 56, alignment: Alignment.center, diff --git a/lib/views/chat_details.dart b/lib/views/chat_details.dart index fd4e423..d702db3 100644 --- a/lib/views/chat_details.dart +++ b/lib/views/chat_details.dart @@ -1,6 +1,8 @@ import 'package:bot_toast/bot_toast.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/matrix_api.dart'; + +import 'package:file_picker_cross/file_picker_cross.dart'; import 'package:fluffychat/components/adaptive_page_layout.dart'; import 'package:fluffychat/components/chat_settings_popup_menu.dart'; import 'package:fluffychat/components/content_banner.dart'; @@ -8,6 +10,7 @@ import 'package:fluffychat/components/dialogs/simple_dialogs.dart'; import 'package:fluffychat/components/list_items/participant_list_item.dart'; import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/utils/matrix_locals.dart'; +import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/views/chat_list.dart'; import 'package:fluffychat/views/invitation_selection.dart'; import 'package:flutter/foundation.dart'; @@ -16,7 +19,6 @@ import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:image_picker/image_picker.dart'; import 'package:matrix_link_text/link_text.dart'; -import 'package:memoryfilepicker/memoryfilepicker.dart'; import './settings_emotes.dart'; import './settings_multiple_emotes.dart'; @@ -104,19 +106,31 @@ class _ChatDetailsState extends State { } void setAvatarAction(BuildContext context) async { - final tempFile = await MemoryFilePicker.getImage( - source: ImageSource.gallery, - imageQuality: 50, - maxWidth: 1600, - maxHeight: 1600); - if (tempFile == null) return; + MatrixFile file; + if (PlatformInfos.isMobile) { + final result = await ImagePicker().getImage( + source: ImageSource.gallery, + imageQuality: 50, + maxWidth: 1600, + maxHeight: 1600); + if (result == null) return; + file = MatrixFile( + bytes: await result.readAsBytes(), + name: result.path, + ); + } else { + final result = await FilePickerCross.importFromStorage( + type: FileTypeCross.image, + ); + if (result == null) return; + file = MatrixFile( + bytes: result.toUint8List(), + name: result.fileName, + ); + } + final success = await SimpleDialogs(context).tryRequestWithLoadingDialog( - widget.room.setAvatar( - MatrixFile( - bytes: tempFile.bytes, - name: tempFile.path, - ), - ), + widget.room.setAvatar(file), ); if (success != false) { BotToast.showText(text: L10n.of(context).avatarHasBeenChanged); diff --git a/lib/views/settings.dart b/lib/views/settings.dart index eee3a62..2fec875 100644 --- a/lib/views/settings.dart +++ b/lib/views/settings.dart @@ -2,15 +2,17 @@ import 'dart:io'; import 'package:bot_toast/bot_toast.dart'; import 'package:famedlysdk/famedlysdk.dart'; +import 'package:file_picker_cross/file_picker_cross.dart'; + import 'package:fluffychat/components/settings_themes.dart'; import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/views/settings_devices.dart'; import 'package:fluffychat/views/settings_ignore_list.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:image_picker/image_picker.dart'; -import 'package:memoryfilepicker/memoryfilepicker.dart'; import 'package:url_launcher/url_launcher.dart'; import '../components/adaptive_page_layout.dart'; @@ -137,20 +139,30 @@ class _SettingsState extends State { } void setAvatarAction(BuildContext context) async { - final tempFile = await MemoryFilePicker.getImage( - source: ImageSource.gallery, - imageQuality: 50, - maxWidth: 1600, - maxHeight: 1600); - if (tempFile == null) return; + MatrixFile file; + if (PlatformInfos.isMobile) { + final result = await ImagePicker().getImage( + source: ImageSource.gallery, + imageQuality: 50, + maxWidth: 1600, + maxHeight: 1600); + if (result == null) return; + file = MatrixFile( + bytes: await result.readAsBytes(), + name: result.path, + ); + } else { + final result = + await FilePickerCross.importFromStorage(type: FileTypeCross.image); + if (result == null) return; + file = MatrixFile( + bytes: result.toUint8List(), + name: result.fileName, + ); + } final matrix = Matrix.of(context); final success = await SimpleDialogs(context).tryRequestWithLoadingDialog( - matrix.client.setAvatar( - MatrixFile( - bytes: tempFile.bytes, - name: tempFile.path, - ), - ), + matrix.client.setAvatar(file), ); if (success != false) { setState(() { diff --git a/lib/views/settings_emotes.dart b/lib/views/settings_emotes.dart index 9b5af89..5046031 100644 --- a/lib/views/settings_emotes.dart +++ b/lib/views/settings_emotes.dart @@ -1,12 +1,13 @@ import 'package:bot_toast/bot_toast.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:famedlysdk/famedlysdk.dart'; +import 'package:file_picker_cross/file_picker_cross.dart'; + import 'package:fluffychat/utils/platform_infos.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:image_picker/image_picker.dart'; -import 'package:memoryfilepicker/memoryfilepicker.dart'; import '../components/adaptive_page_layout.dart'; import '../components/dialogs/simple_dialogs.dart'; @@ -458,16 +459,30 @@ class _EmoteImagePickerState extends State<_EmoteImagePicker> { BotToast.showText(text: L10n.of(context).notSupportedInWeb); return; } - var file = await MemoryFilePicker.getImage( - source: ImageSource.gallery, - imageQuality: 50, - maxWidth: 128, - maxHeight: 128); - if (file == null) return; - final matrixFile = MatrixFile(bytes: file.bytes, name: file.path); + MatrixFile file; + if (PlatformInfos.isMobile) { + final result = await ImagePicker().getImage( + source: ImageSource.gallery, + imageQuality: 50, + maxWidth: 1600, + maxHeight: 1600); + if (result == null) return; + file = MatrixFile( + bytes: await result.readAsBytes(), + name: result.path, + ); + } else { + final result = await FilePickerCross.importFromStorage( + type: FileTypeCross.image); + if (result == null) return; + file = MatrixFile( + bytes: result.toUint8List(), + name: result.fileName, + ); + } final uploadResp = await SimpleDialogs(context).tryRequestWithLoadingDialog( - Matrix.of(context).client.upload(matrixFile.bytes, matrixFile.name), + Matrix.of(context).client.upload(file.bytes, file.name), ); setState(() { widget.controller.text = uploadResp; diff --git a/lib/views/sign_up.dart b/lib/views/sign_up.dart index cf8e199..2350ff8 100644 --- a/lib/views/sign_up.dart +++ b/lib/views/sign_up.dart @@ -1,6 +1,8 @@ import 'dart:math'; import 'package:famedlysdk/famedlysdk.dart'; +import 'package:file_picker_cross/file_picker_cross.dart'; + import 'package:fluffychat/components/matrix.dart'; import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/views/login.dart'; @@ -8,8 +10,6 @@ import 'package:fluffychat/views/sign_up_password.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:image_picker/image_picker.dart'; -import 'package:memoryfilepicker/memoryfilepicker.dart'; class SignUp extends StatefulWidget { SignUp({Key key, WellKnownInformations this.wellknown: null}) @@ -25,16 +25,19 @@ class _SignUpState extends State { final TextEditingController usernameController = TextEditingController(); String usernameError; bool loading = false; - MemoryFile avatar; + MatrixFile avatar; void setAvatarAction() async { - var file = await MemoryFilePicker.getImage( - source: ImageSource.gallery, - maxHeight: 512, - maxWidth: 512, - imageQuality: 50, - ); - if (file != null) setState(() => avatar = file); + var file = + await FilePickerCross.importFromStorage(type: FileTypeCross.image); + if (file != null) { + setState( + () => avatar = MatrixFile( + bytes: file.toUint8List(), + name: file.fileName, + ), + ); + } } void signUpAction(BuildContext context) async { diff --git a/lib/views/sign_up_password.dart b/lib/views/sign_up_password.dart index 84cebb6..59c8ed2 100644 --- a/lib/views/sign_up_password.dart +++ b/lib/views/sign_up_password.dart @@ -2,17 +2,17 @@ import 'dart:math'; import 'package:bot_toast/bot_toast.dart'; import 'package:famedlysdk/famedlysdk.dart'; + import 'package:fluffychat/components/matrix.dart'; import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/views/auth_web_view.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:memoryfilepicker/memoryfilepicker.dart'; import 'chat_list.dart'; class SignUpPassword extends StatefulWidget { - final MemoryFile avatar; + final MatrixFile avatar; final String username; final String displayname; final WellKnownInformations wellknown; @@ -101,12 +101,7 @@ class _SignUpPasswordState extends State { } if (widget.avatar != null) { try { - await matrix.client.setAvatar( - MatrixFile( - bytes: widget.avatar.bytes, - name: widget.avatar.path, - ), - ); + await matrix.client.setAvatar(widget.avatar); } catch (exception) { BotToast.showText(text: L10n.of(context).couldNotSetAvatar); } diff --git a/linux/flutter/generated_plugin_registrant.cc b/linux/flutter/generated_plugin_registrant.cc index 026851f..b044cbd 100644 --- a/linux/flutter/generated_plugin_registrant.cc +++ b/linux/flutter/generated_plugin_registrant.cc @@ -4,9 +4,13 @@ #include "generated_plugin_registrant.h" +#include #include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) file_chooser_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "FileChooserPlugin"); + file_chooser_plugin_register_with_registrar(file_chooser_registrar); g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin"); url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar); diff --git a/linux/flutter/generated_plugins.cmake b/linux/flutter/generated_plugins.cmake index 1fc8ed3..6a174de 100644 --- a/linux/flutter/generated_plugins.cmake +++ b/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + file_chooser url_launcher_linux ) diff --git a/pubspec.lock b/pubspec.lock index 489c31f..d249890 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -162,6 +162,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.6" + disk_space: + dependency: transitive + description: + name: disk_space + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.3" encrypt: dependency: transitive description: @@ -197,6 +204,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "5.2.1" + file_chooser: + dependency: transitive + description: + name: file_chooser + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.5" file_picker: dependency: transitive description: @@ -204,6 +218,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.13.3" + file_picker_cross: + dependency: "direct main" + description: + name: file_picker_cross + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.2" file_picker_platform_interface: dependency: transitive description: @@ -407,7 +428,7 @@ packages: source: hosted version: "2.1.18" image_picker: - dependency: transitive + dependency: "direct main" description: name: image_picker url: "https://pub.dartlang.org" @@ -497,13 +518,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.5" - memoryfilepicker: - dependency: "direct main" - description: - name: memoryfilepicker - url: "https://pub.dartlang.org" - source: hosted - version: "0.1.3" meta: dependency: transitive description: @@ -590,6 +604,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.9.3" + package_info: + dependency: transitive + description: + name: package_info + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.3" password_hash: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 432b891..f3b9153 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,7 +28,8 @@ dependencies: path: ../famedlysdk localstorage: ^3.0.1+4 - memoryfilepicker: ^0.1.3 + file_picker_cross: ^4.2.2 + image_picker: ^0.6.7+11 url_launcher: ^5.4.1 url_launcher_web: ^0.1.0 cached_network_image: ^2.3.2+1 diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index ddfcf7c..9579d43 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -4,9 +4,12 @@ #include "generated_plugin_registrant.h" +#include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + FileChooserPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("FileChooserPlugin")); UrlLauncherPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("UrlLauncherPlugin")); } diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 411af46..5c2bdaf 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + file_chooser url_launcher_windows )