refactor: Switch to file_picker package and get rid of some dependency overrides
This commit is contained in:
parent
5606cb8252
commit
d9aa2f8e44
17 changed files with 175 additions and 212 deletions
|
|
@ -1,9 +1,14 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:file_picker_cross/file_picker_cross.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:universal_html/html.dart' as html;
|
||||
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
import 'package:fluffychat/utils/size_string.dart';
|
||||
|
|
@ -13,10 +18,65 @@ extension MatrixFileExtension on MatrixFile {
|
|||
if (PlatformInfos.isIOS) {
|
||||
return share(context);
|
||||
}
|
||||
final fileName = name.split('/').last;
|
||||
|
||||
final file = FilePickerCross(bytes);
|
||||
await file.exportToStorage(fileName: fileName, share: false);
|
||||
if (PlatformInfos.isWeb) {
|
||||
_webDownload();
|
||||
return;
|
||||
}
|
||||
|
||||
final downloadPath = PlatformInfos.isAndroid
|
||||
? await getDownloadPathAndroid()
|
||||
: await FilePicker.platform.saveFile(
|
||||
dialogTitle: L10n.of(context)!.saveFile,
|
||||
fileName: name,
|
||||
type: filePickerFileType,
|
||||
);
|
||||
if (downloadPath == null) return;
|
||||
|
||||
final result = await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => File(downloadPath).writeAsBytes(bytes),
|
||||
);
|
||||
if (result.error != null) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
L10n.of(context)!.fileHasBeenSavedAt(downloadPath),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<String> getDownloadPathAndroid() async {
|
||||
final downloadDirectories = await getExternalStorageDirectories(
|
||||
type: StorageDirectory.downloads,
|
||||
);
|
||||
if (downloadDirectories != null && downloadDirectories.isNotEmpty) {
|
||||
return downloadDirectories.first.path;
|
||||
}
|
||||
final fallbackDirectory = await getApplicationDocumentsDirectory();
|
||||
return fallbackDirectory.path;
|
||||
}
|
||||
|
||||
FileType get filePickerFileType {
|
||||
if (this is MatrixImageFile) return FileType.image;
|
||||
if (this is MatrixAudioFile) return FileType.audio;
|
||||
if (this is MatrixVideoFile) return FileType.video;
|
||||
return FileType.any;
|
||||
}
|
||||
|
||||
void _webDownload() {
|
||||
html.AnchorElement(
|
||||
href: html.Url.createObjectUrlFromBlob(
|
||||
html.Blob(
|
||||
[bytes],
|
||||
'application/octet-stream',
|
||||
),
|
||||
),
|
||||
)
|
||||
..download = name
|
||||
..click();
|
||||
}
|
||||
|
||||
void share(BuildContext context) async {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue