Skip to content

Commit

Permalink
code adjustments and README upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelbeltran committed Dec 12, 2024
1 parent 027ba16 commit 1ae9aa2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
6 changes: 6 additions & 0 deletions packages/share_plus/share_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ package.
Share.shareXFiles([XFile('assets/hello.txt')], text: 'Great picture');
```

File downloading fallback mechanism for web can be disabled by setting:

```dart
Share.downloadFallbackEnabled = false;
```

#### Share Data

You can also share files that you dynamically generate from its data using [`XFile.fromData`](https://pub.dev/documentation/share_plus/latest/share_plus/XFile/XFile.fromData.html).
Expand Down
1 change: 1 addition & 0 deletions packages/share_plus/share_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:share_plus/share_plus.dart';
import 'image_previews.dart';

void main() {
SharePlusWebPlugin.downloadFallbackEnabled = false;
runApp(const DemoApp());
}

Expand Down
3 changes: 3 additions & 0 deletions packages/share_plus/share_plus/lib/share_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export 'src/share_plus_windows.dart'
class Share {
static SharePlatform get _platform => SharePlatform.instance;

/// Whether to fall back to downloading files if [shareXFiles] fails on web.
static bool downloadFallbackEnabled = true;

/// Summons the platform's share sheet to share uri.
///
/// Wraps the platform's native share dialog. Can share a URL.
Expand Down
43 changes: 24 additions & 19 deletions packages/share_plus/share_plus/lib/src/share_plus_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:ui';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:meta/meta.dart';
import 'package:mime/mime.dart' show lookupMimeType;
import 'package:share_plus/share_plus.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:url_launcher_web/url_launcher_web.dart';
Expand All @@ -15,9 +16,6 @@ import 'package:web/web.dart';
class SharePlusWebPlugin extends SharePlatform {
final UrlLauncherPlatform urlLauncher;

/// Whether to fall back to download files if Navigator.share() fails.
static bool downloadFallbackEnabled = true;

/// Registers this class as the default instance of [SharePlatform].
static void registerWith(Registrar registrar) {
SharePlatform.instance = SharePlusWebPlugin(UrlLauncherPlugin());
Expand Down Expand Up @@ -208,12 +206,18 @@ class SharePlusWebPlugin extends SharePlatform {
);

return _downloadIfFallbackEnabled(
files, fileNameOverrides, 'Navigator.canShare() is unavailable');
files,
fileNameOverrides,
'Navigator.canShare() is unavailable',
);
}

if (!canShare) {
return _downloadIfFallbackEnabled(
files, fileNameOverrides, 'Navigator.canShare() is false');
files,
fileNameOverrides,
'Navigator.canShare() is false',
);
}

try {
Expand All @@ -229,30 +233,31 @@ class SharePlusWebPlugin extends SharePlatform {
return _resultDismissed;
}

developer.log(
'Failed to share files',
error: '$name: $message',
return _downloadIfFallbackEnabled(
files,
fileNameOverrides,
'Navigator.share() failed: $message',
);
if (name case 'NotAllowedError') {
return _downloadIfFallbackEnabled(
files, fileNameOverrides, 'Navigator.share() failed: $message');
}

throw Exception('Navigator.share() failed: $message');
}
}

Future<ShareResult> _downloadIfFallbackEnabled(
List<XFile> files, List<String>? fileNameOverrides, String message) {
if (downloadFallbackEnabled) {
List<XFile> files,
List<String>? fileNameOverrides,
String message,
) {
developer.log(message);
if (Share.downloadFallbackEnabled) {
return _download(files, fileNameOverrides);
} else {
throw Exception(message);
}
}

Future<ShareResult> _download(
List<XFile> files, List<String>? fileNameOverrides) async {
List<XFile> files,
List<String>? fileNameOverrides,
) async {
try {
for (final (index, file) in files.indexed) {
final bytes = await file.readAsBytes();
Expand All @@ -268,8 +273,8 @@ class SharePlusWebPlugin extends SharePlatform {

return ShareResult.unavailable;
} catch (error) {
developer.log('Failed to share files', error: error);
throw Exception('Failed to to share files: $error');
developer.log('Failed to download files', error: error);
throw Exception('Failed to to download files: $error');
}
}

Expand Down

0 comments on commit 1ae9aa2

Please sign in to comment.