refactor: Download on android and iOS with file_picker

This commit is contained in:
krille-chan 2024-04-21 10:34:09 +02:00
parent 86df13d6d8
commit a705a5b399
No known key found for this signature in database
3 changed files with 16 additions and 46 deletions

View file

@ -3,7 +3,6 @@ import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter_file_dialog/flutter_file_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart'; import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
@ -15,10 +14,6 @@ import 'package:fluffychat/utils/size_string.dart';
extension MatrixFileExtension on MatrixFile { extension MatrixFileExtension on MatrixFile {
void save(BuildContext context) async { void save(BuildContext context) async {
if (PlatformInfos.isIOS || PlatformInfos.isAndroid) {
_mobileDownload(context);
}
if (PlatformInfos.isWeb) { if (PlatformInfos.isWeb) {
_webDownload(); _webDownload();
return; return;
@ -28,14 +23,17 @@ extension MatrixFileExtension on MatrixFile {
dialogTitle: L10n.of(context)!.saveFile, dialogTitle: L10n.of(context)!.saveFile,
fileName: name, fileName: name,
type: filePickerFileType, type: filePickerFileType,
bytes: bytes,
); );
if (downloadPath == null) return; if (downloadPath == null) return;
final result = await showFutureLoadingDialog( if (PlatformInfos.isDesktop) {
context: context, final result = await showFutureLoadingDialog(
future: () => File(downloadPath).writeAsBytes(bytes), context: context,
); future: () => File(downloadPath).writeAsBytes(bytes),
if (result.error != null) return; );
if (result.error != null) return;
}
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
@ -53,25 +51,6 @@ extension MatrixFileExtension on MatrixFile {
return FileType.any; return FileType.any;
} }
void _mobileDownload(BuildContext context) async {
final downloadPath = await FlutterFileDialog.saveFile(
params: SaveFileDialogParams(
fileName: name,
data: bytes,
),
);
if (downloadPath != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
L10n.of(context)!.fileHasBeenSavedAt(downloadPath),
),
),
);
}
return;
}
void _webDownload() { void _webDownload() {
html.AnchorElement( html.AnchorElement(
href: html.Url.createObjectUrlFromBlob( href: html.Url.createObjectUrlFromBlob(

View file

@ -397,10 +397,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: file_picker name: file_picker
sha256: "4e42aacde3b993c5947467ab640882c56947d9d27342a5b6f2895b23956954a6" sha256: b6283d7387310ad83bc4f3bc245b75d223a032ae6eba275afcd585de2b9a1476
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.1.1" version: "8.0.1"
file_selector_linux: file_selector_linux:
dependency: transitive dependency: transitive
description: description:
@ -459,14 +459,6 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_file_dialog:
dependency: "direct main"
description:
name: flutter_file_dialog
sha256: "9344b8f07be6a1b6f9854b723fb0cf84a8094ba94761af1d213589d3cb087488"
url: "https://pub.dev"
source: hosted
version: "3.0.2"
flutter_foreground_task: flutter_foreground_task:
dependency: "direct main" dependency: "direct main"
description: description:
@ -932,10 +924,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: image_picker name: image_picker
sha256: "26222b01a0c9a2c8fe02fc90b8208bd3325da5ed1f4a2acabf75939031ac0bdd" sha256: fe9ee64ccb8d599a5dfb0e21cc6652232c610bcf667af4e79b9eb175cc30a7a5
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.7" version: "1.1.0"
image_picker_android: image_picker_android:
dependency: transitive dependency: transitive
description: description:
@ -980,10 +972,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: image_picker_platform_interface name: image_picker_platform_interface
sha256: ed9b00e63977c93b0d2d2b343685bed9c324534ba5abafbb3dfbd6a780b1b514 sha256: "9ec26d410ff46f483c5519c29c02ef0e02e13a543f882b152d4bfd2f06802f80"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.9.1" version: "2.10.0"
image_picker_windows: image_picker_windows:
dependency: transitive dependency: transitive
description: description:

View file

@ -26,12 +26,11 @@ dependencies:
emoji_proposal: ^0.0.1 emoji_proposal: ^0.0.1
emojis: ^0.9.9 emojis: ^0.9.9
#fcm_shared_isolate: ^0.1.0 #fcm_shared_isolate: ^0.1.0
file_picker: ^6.1.1 file_picker: ^8.0.1
flutter: flutter:
sdk: flutter sdk: flutter
flutter_app_badger: ^1.5.0 flutter_app_badger: ^1.5.0
flutter_cache_manager: ^3.3.1 flutter_cache_manager: ^3.3.1
flutter_file_dialog: ^3.0.2
flutter_foreground_task: ^6.1.3 flutter_foreground_task: ^6.1.3
flutter_highlighter: ^0.1.1 flutter_highlighter: ^0.1.1
flutter_html: ^3.0.0-beta.2 flutter_html: ^3.0.0-beta.2
@ -59,7 +58,7 @@ dependencies:
html: ^0.15.4 html: ^0.15.4
http: ^1.2.0 http: ^1.2.0
image: ^4.1.7 image: ^4.1.7
image_picker: ^1.0.7 image_picker: ^1.1.0
intl: any intl: any
just_audio: ^0.9.37 just_audio: ^0.9.37
keyboard_shortcuts: ^0.1.4 keyboard_shortcuts: ^0.1.4