From 364d35c2d4da9a013ddb807447955ce9a1cc028a Mon Sep 17 00:00:00 2001 From: SirusCodes Date: Wed, 26 Oct 2022 16:00:58 +0530 Subject: [PATCH 1/2] feat(share_plus): remove direct dependence of url_launcher --- .../share_plus/lib/src/share_plus_linux.dart | 11 +++++++++-- .../share_plus/share_plus/lib/src/share_plus_web.dart | 9 ++++++--- .../share_plus/lib/src/share_plus_windows.dart | 9 ++++++--- packages/share_plus/share_plus/pubspec.yaml | 6 ++++-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/share_plus/share_plus/lib/src/share_plus_linux.dart b/packages/share_plus/share_plus/lib/src/share_plus_linux.dart index 8e92fd606f..88893363ea 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_linux.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_linux.dart @@ -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(); @@ -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. diff --git a/packages/share_plus/share_plus/lib/src/share_plus_web.dart b/packages/share_plus/share_plus/lib/src/share_plus_web.dart index 986fb37d5b..08128f6879 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_web.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_web.dart @@ -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(); @@ -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'); } diff --git a/packages/share_plus/share_plus/lib/src/share_plus_windows.dart b/packages/share_plus/share_plus/lib/src/share_plus_windows.dart index 205c3b57d8..3a8f084b4d 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_windows.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_windows.dart @@ -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() { @@ -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'); } diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index 1e59bb2f81..72df003dea 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -34,7 +34,10 @@ 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 @@ -42,7 +45,6 @@ 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" From 167c604ec030b7045558b8e67075dadfad2b0382 Mon Sep 17 00:00:00 2001 From: SirusCodes Date: Fri, 28 Oct 2022 15:39:26 +0530 Subject: [PATCH 2/2] fix(share_plus): breaking tests --- .../share_plus/lib/src/share_plus_linux.dart | 6 ++++-- .../share_plus/share_plus/lib/src/share_plus_web.dart | 10 ++++++---- .../share_plus/lib/src/share_plus_windows.dart | 6 ++++-- .../share_plus/test/share_plus_linux_test.dart | 9 +++------ .../share_plus/test/share_plus_windows_test.dart | 9 +++------ 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/share_plus/share_plus/lib/src/share_plus_linux.dart b/packages/share_plus/share_plus/lib/src/share_plus_linux.dart index 88893363ea..a0f8531942 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_linux.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_linux.dart @@ -9,11 +9,13 @@ import 'package:url_launcher_platform_interface/url_launcher_platform_interface. /// The Linux implementation of SharePlatform. class SharePlusLinuxPlugin extends SharePlatform { - final urlLauncher = UrlLauncherLinux(); + SharePlusLinuxPlugin(this.urlLauncher); + + final UrlLauncherPlatform urlLauncher; /// Register this dart class as the platform implementation for linux static void registerWith() { - SharePlatform.instance = SharePlusLinuxPlugin(); + SharePlatform.instance = SharePlusLinuxPlugin(UrlLauncherLinux()); } /// Share text. diff --git a/packages/share_plus/share_plus/lib/src/share_plus_web.dart b/packages/share_plus/share_plus/lib/src/share_plus_web.dart index 08128f6879..f1bad19f94 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_web.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_web.dart @@ -10,18 +10,20 @@ import 'package:url_launcher_web/url_launcher_web.dart'; /// The web implementation of [SharePlatform]. class SharePlusWebPlugin extends SharePlatform { - final urlLauncher = UrlLauncherPlugin(); + final UrlLauncherPlatform urlLauncher; /// Registers this class as the default instance of [SharePlatform]. static void registerWith(Registrar registrar) { - SharePlatform.instance = SharePlusWebPlugin(); + SharePlatform.instance = SharePlusWebPlugin(UrlLauncherPlugin()); } final html.Navigator _navigator; /// A constructor that allows tests to override the window object used by the plugin. - SharePlusWebPlugin({@visibleForTesting html.Navigator? debugNavigator}) - : _navigator = debugNavigator ?? html.window.navigator; + SharePlusWebPlugin( + this.urlLauncher, { + @visibleForTesting html.Navigator? debugNavigator, + }) : _navigator = debugNavigator ?? html.window.navigator; /// Share text @override diff --git a/packages/share_plus/share_plus/lib/src/share_plus_windows.dart b/packages/share_plus/share_plus/lib/src/share_plus_windows.dart index 3a8f084b4d..bc850b9252 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_windows.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_windows.dart @@ -11,13 +11,15 @@ 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(); + SharePlusWindowsPlugin(this.urlLauncher); + + final UrlLauncherPlatform urlLauncher; /// If the modern Share UI i.e. `DataTransferManager` is not available, then use this Dart class instead of platform specific implementation. /// static void registerWith() { if (!VersionHelper.instance.isWindows10RS5OrGreater) { - SharePlatform.instance = SharePlusWindowsPlugin(); + SharePlatform.instance = SharePlusWindowsPlugin(UrlLauncherWindows()); } } diff --git a/packages/share_plus/share_plus/test/share_plus_linux_test.dart b/packages/share_plus/share_plus/test/share_plus_linux_test.dart index b5b02ad073..e0800edccd 100644 --- a/packages/share_plus/share_plus/test/share_plus_linux_test.dart +++ b/packages/share_plus/share_plus/test/share_plus_linux_test.dart @@ -11,9 +11,8 @@ void main() { }); test('url encoding is correct for &', () async { final mock = MockUrlLauncherPlatform(); - UrlLauncherPlatform.instance = mock; - await SharePlusLinuxPlugin().share('foo&bar', subject: 'bar&foo'); + await SharePlusLinuxPlugin(mock).share('foo&bar', subject: 'bar&foo'); expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar'); }); @@ -21,9 +20,8 @@ void main() { // see https://github.com/dart-lang/sdk/issues/43838#issuecomment-823551891 test('url encoding is correct for spaces', () async { final mock = MockUrlLauncherPlatform(); - UrlLauncherPlatform.instance = mock; - await SharePlusLinuxPlugin().share('foo bar', subject: 'bar foo'); + await SharePlusLinuxPlugin(mock).share('foo bar', subject: 'bar foo'); expect(mock.url, 'mailto:?subject=bar%20foo&body=foo%20bar'); }); @@ -31,9 +29,8 @@ void main() { test('throws when url_launcher can\'t launch uri', () async { final mock = MockUrlLauncherPlatform(); mock.canLaunchMockValue = false; - UrlLauncherPlatform.instance = mock; - expect(() async => await SharePlusLinuxPlugin().share('foo bar'), + expect(() async => await SharePlusLinuxPlugin(mock).share('foo bar'), throwsException); }); } diff --git a/packages/share_plus/share_plus/test/share_plus_windows_test.dart b/packages/share_plus/share_plus/test/share_plus_windows_test.dart index b27469c31b..0f77ee6181 100644 --- a/packages/share_plus/share_plus/test/share_plus_windows_test.dart +++ b/packages/share_plus/share_plus/test/share_plus_windows_test.dart @@ -31,9 +31,8 @@ void main() { 'url encoding is correct for &', () async { final mock = MockUrlLauncherPlatform(); - UrlLauncherPlatform.instance = mock; - await SharePlusWindowsPlugin().share('foo&bar', subject: 'bar&foo'); + await SharePlusWindowsPlugin(mock).share('foo&bar', subject: 'bar&foo'); expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar'); }, @@ -45,9 +44,8 @@ void main() { 'url encoding is correct for spaces', () async { final mock = MockUrlLauncherPlatform(); - UrlLauncherPlatform.instance = mock; - await SharePlusWindowsPlugin().share('foo bar', subject: 'bar foo'); + await SharePlusWindowsPlugin(mock).share('foo bar', subject: 'bar foo'); expect(mock.url, 'mailto:?subject=bar%20foo&body=foo%20bar'); }, @@ -59,9 +57,8 @@ void main() { () async { final mock = MockUrlLauncherPlatform(); mock.canLaunchMockValue = false; - UrlLauncherPlatform.instance = mock; - expect(() async => await SharePlusWindowsPlugin().share('foo bar'), + expect(() async => await SharePlusWindowsPlugin(mock).share('foo bar'), throwsException); }, skip: VersionHelper.instance.isWindows10RS5OrGreater,