refactor: Better download UX with file picker for android and iOS
This commit is contained in:
parent
1776a65884
commit
826cce6573
3 changed files with 36 additions and 28 deletions
|
|
@ -3,10 +3,10 @@ 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';
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
import 'package:share_plus/share_plus.dart';
|
import 'package:share_plus/share_plus.dart';
|
||||||
import 'package:universal_html/html.dart' as html;
|
import 'package:universal_html/html.dart' as html;
|
||||||
|
|
||||||
|
|
@ -15,8 +15,8 @@ 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) {
|
if (PlatformInfos.isIOS || PlatformInfos.isAndroid) {
|
||||||
return share(context);
|
_mobileDownload(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PlatformInfos.isWeb) {
|
if (PlatformInfos.isWeb) {
|
||||||
|
|
@ -24,13 +24,11 @@ extension MatrixFileExtension on MatrixFile {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final downloadPath = PlatformInfos.isAndroid
|
final downloadPath = await FilePicker.platform.saveFile(
|
||||||
? await getDownloadPathAndroid()
|
dialogTitle: L10n.of(context)!.saveFile,
|
||||||
: await FilePicker.platform.saveFile(
|
fileName: name,
|
||||||
dialogTitle: L10n.of(context)!.saveFile,
|
type: filePickerFileType,
|
||||||
fileName: name,
|
);
|
||||||
type: filePickerFileType,
|
|
||||||
);
|
|
||||||
if (downloadPath == null) return;
|
if (downloadPath == null) return;
|
||||||
|
|
||||||
final result = await showFutureLoadingDialog(
|
final result = await showFutureLoadingDialog(
|
||||||
|
|
@ -48,24 +46,6 @@ extension MatrixFileExtension on MatrixFile {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> getDownloadPathAndroid() async {
|
|
||||||
final directory = await getDownloadDirectoryAndroid();
|
|
||||||
var counter = 1;
|
|
||||||
var path = '${directory.path}/$name';
|
|
||||||
while (await File(path).exists()) {
|
|
||||||
path = '${directory.path}/(${counter++})$name';
|
|
||||||
}
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<Directory> getDownloadDirectoryAndroid() async {
|
|
||||||
final defaultDownloadDirectory = Directory('/storage/emulated/0/Download');
|
|
||||||
if (await defaultDownloadDirectory.exists()) {
|
|
||||||
return defaultDownloadDirectory;
|
|
||||||
}
|
|
||||||
return await getApplicationDocumentsDirectory();
|
|
||||||
}
|
|
||||||
|
|
||||||
FileType get filePickerFileType {
|
FileType get filePickerFileType {
|
||||||
if (this is MatrixImageFile) return FileType.image;
|
if (this is MatrixImageFile) return FileType.image;
|
||||||
if (this is MatrixAudioFile) return FileType.audio;
|
if (this is MatrixAudioFile) return FileType.audio;
|
||||||
|
|
@ -73,6 +53,25 @@ 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(
|
||||||
|
|
|
||||||
|
|
@ -427,6 +427,14 @@ 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:
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ dependencies:
|
||||||
flutter_app_badger: ^1.5.0
|
flutter_app_badger: ^1.5.0
|
||||||
flutter_blurhash: ^0.8.2
|
flutter_blurhash: ^0.8.2
|
||||||
flutter_cache_manager: ^3.3.0
|
flutter_cache_manager: ^3.3.0
|
||||||
|
flutter_file_dialog: ^3.0.2
|
||||||
flutter_foreground_task: ^6.0.0+1
|
flutter_foreground_task: ^6.0.0+1
|
||||||
flutter_highlighter: ^0.1.1
|
flutter_highlighter: ^0.1.1
|
||||||
flutter_html: ^3.0.0-beta.2
|
flutter_html: ^3.0.0-beta.2
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue