Skip to content

Commit

Permalink
feat(share_plus): remove direct dependence of url_launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
SirusCodes committed Oct 26, 2022
1 parent 1f1786c commit 364d35c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
11 changes: 9 additions & 2 deletions packages/share_plus/share_plus/lib/src/share_plus_linux.dart
Expand Up @@ -4,10 +4,13 @@ library share_plus_linux;
import 'dart:ui';

import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_linux/url_launcher_linux.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

/// The Linux implementation of SharePlatform.
class SharePlusLinuxPlugin extends SharePlatform {
final urlLauncher = UrlLauncherLinux();

/// Register this dart class as the platform implementation for linux
static void registerWith() {
SharePlatform.instance = SharePlusLinuxPlugin();
Expand Down Expand Up @@ -35,7 +38,11 @@ class SharePlusLinuxPlugin extends SharePlatform {
.join('&'),
);

await launchUrl(uri);
if (await urlLauncher.canLaunch(uri.toString())) {
await urlLauncher.launchUrl(uri.toString(), const LaunchOptions());
} else {
throw Exception('Unable to share on web');
}
}

/// Share files.
Expand Down
9 changes: 6 additions & 3 deletions packages/share_plus/share_plus/lib/src/share_plus_web.dart
Expand Up @@ -5,10 +5,13 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:mime/mime.dart' show lookupMimeType;
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:url_launcher_web/url_launcher_web.dart';

/// The web implementation of [SharePlatform].
class SharePlusWebPlugin extends SharePlatform {
final urlLauncher = UrlLauncherPlugin();

/// Registers this class as the default instance of [SharePlatform].
static void registerWith(Registrar registrar) {
SharePlatform.instance = SharePlusWebPlugin();
Expand Down Expand Up @@ -45,8 +48,8 @@ class SharePlusWebPlugin extends SharePlatform {
.join('&'),
);

if (await canLaunchUrl(uri)) {
await launchUrl(uri);
if (await urlLauncher.canLaunch(uri.toString())) {
await urlLauncher.launchUrl(uri.toString(), const LaunchOptions());
} else {
throw Exception('Unable to share on web');
}
Expand Down
Expand Up @@ -5,11 +5,14 @@ import 'dart:ui';

import 'package:share_plus/src/windows_version_helper.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:url_launcher_windows/url_launcher_windows.dart';

/// The fallback Windows implementation of [SharePlatform], for older Windows versions.
///
class SharePlusWindowsPlugin extends SharePlatform {
final urlLauncher = UrlLauncherWindows();

/// If the modern Share UI i.e. `DataTransferManager` is not available, then use this Dart class instead of platform specific implementation.
///
static void registerWith() {
Expand Down Expand Up @@ -39,8 +42,8 @@ class SharePlusWindowsPlugin extends SharePlatform {
.join('&'),
);

if (await canLaunchUrl(uri)) {
await launchUrl(uri);
if (await urlLauncher.canLaunch(uri.toString())) {
await urlLauncher.launchUrl(uri.toString(), const LaunchOptions());
} else {
throw Exception('Unable to share on windows');
}
Expand Down
6 changes: 4 additions & 2 deletions packages/share_plus/share_plus/pubspec.yaml
Expand Up @@ -34,15 +34,17 @@ dependencies:
sdk: flutter
share_plus_platform_interface: ^3.1.2
file: ^6.0.0
url_launcher: ^6.1.2
url_launcher_web: ^2.0.13
url_launcher_windows: ^3.0.1
url_launcher_linux: ^3.0.1
url_launcher_platform_interface: ^2.1.1
ffi: ^2.0.1
win32: ^3.0.0

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.1
url_launcher_platform_interface: ^2.0.2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 364d35c

Please sign in to comment.