2021-07-10 17:06:04 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
2021-07-25 07:00:38 +02:00
|
|
|
import 'package:nc_photos/app_localizations.dart';
|
2021-07-10 17:06:04 +02:00
|
|
|
import 'package:nc_photos/entity/file.dart';
|
|
|
|
import 'package:nc_photos/exception.dart';
|
|
|
|
import 'package:nc_photos/exception_util.dart' as exception_util;
|
|
|
|
import 'package:nc_photos/k.dart' as k;
|
|
|
|
import 'package:nc_photos/mobile/share.dart';
|
|
|
|
import 'package:nc_photos/platform/k.dart' as platform_k;
|
|
|
|
import 'package:nc_photos/snack_bar_manager.dart';
|
2021-09-09 22:42:47 +02:00
|
|
|
import 'package:nc_photos/use_case/download_file.dart';
|
2021-07-10 17:06:04 +02:00
|
|
|
import 'package:nc_photos/widget/processing_dialog.dart';
|
|
|
|
import 'package:tuple/tuple.dart';
|
|
|
|
|
|
|
|
/// Handle sharing to other apps
|
|
|
|
class ShareHandler {
|
|
|
|
Future<void> shareFiles(
|
|
|
|
BuildContext context, Account account, List<File> files) async {
|
|
|
|
assert(platform_k.isAndroid);
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
2021-08-29 13:51:43 +02:00
|
|
|
builder: (context) =>
|
|
|
|
ProcessingDialog(text: L10n.global().shareDownloadingDialogContent),
|
2021-07-10 17:06:04 +02:00
|
|
|
);
|
|
|
|
final results = <Tuple2<File, dynamic>>[];
|
|
|
|
for (final f in files) {
|
|
|
|
try {
|
2021-09-19 12:59:45 +02:00
|
|
|
results.add(Tuple2(
|
|
|
|
f,
|
|
|
|
await DownloadFile()(
|
|
|
|
account,
|
|
|
|
f,
|
|
|
|
shouldNotify: false,
|
|
|
|
)));
|
2021-07-10 17:06:04 +02:00
|
|
|
} on PermissionException catch (_) {
|
|
|
|
_log.warning("[shareFiles] Permission not granted");
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(L10n.global().downloadFailureNoPermissionNotification),
|
2021-07-10 17:06:04 +02:00
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
// dismiss the dialog
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
rethrow;
|
|
|
|
} catch (e, stacktrace) {
|
|
|
|
_log.shout("[shareFiles] Failed while downloadFile", e, stacktrace);
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(exception_util.toUserString(e)),
|
2021-07-10 17:06:04 +02:00
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// dismiss the dialog
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
|
|
|
final share = AndroidShare(results.map((e) => e.item2 as String).toList(),
|
|
|
|
results.map((e) => e.item1.contentType).toList());
|
|
|
|
share.share();
|
|
|
|
}
|
|
|
|
|
|
|
|
static final _log = Logger("share_handler.ShareHandler");
|
|
|
|
}
|