Skip to content

Commit

Permalink
fix(share_plus): remove canLaunch check (#1315)
Browse files Browse the repository at this point in the history
Co-authored-by: Miguel Beltran <m@beltran.work>
  • Loading branch information
SirusCodes and miquelbeltran authored Nov 7, 2022
1 parent e1d3553 commit f8f1a0b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 72 deletions.
11 changes: 6 additions & 5 deletions packages/share_plus/share_plus/lib/src/share_plus_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class SharePlusLinuxPlugin extends SharePlatform {
}

/// Share text.
/// Throws a [PlatformException] if `mailto:` scheme cannot be handled.
@override
Future<void> share(
String text, {
Expand All @@ -40,10 +39,12 @@ class SharePlusLinuxPlugin extends SharePlatform {
.join('&'),
);

if (await urlLauncher.canLaunch(uri.toString())) {
await urlLauncher.launchUrl(uri.toString(), const LaunchOptions());
} else {
throw Exception('Unable to share on web');
final launchResult = await urlLauncher.launchUrl(
uri.toString(),
const LaunchOptions(),
);
if (!launchResult) {
throw Exception('Failed to launch $uri');
}
}

Expand Down
10 changes: 6 additions & 4 deletions packages/share_plus/share_plus/lib/src/share_plus_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ class SharePlusWebPlugin extends SharePlatform {
.join('&'),
);

if (await urlLauncher.canLaunch(uri.toString())) {
await urlLauncher.launchUrl(uri.toString(), const LaunchOptions());
} else {
throw Exception('Unable to share on web');
final launchResult = await urlLauncher.launchUrl(
uri.toString(),
const LaunchOptions(),
);
if (!launchResult) {
throw Exception('Failed to launch $uri');
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/share_plus/share_plus/lib/src/share_plus_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class SharePlusWindowsPlugin extends SharePlatform {
.join('&'),
);

if (await urlLauncher.canLaunch(uri.toString())) {
await urlLauncher.launchUrl(uri.toString(), const LaunchOptions());
} else {
throw Exception('Unable to share on windows');
final launchResult = await urlLauncher.launchUrl(
uri.toString(),
const LaunchOptions(),
);
if (!launchResult) {
throw Exception('Failed to launch $uri');
}
}

Expand Down
33 changes: 2 additions & 31 deletions packages/share_plus/share_plus/test/share_plus_linux_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:share_plus_platform_interface/share_plus_platform_interface.dart
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

import 'url_launcher_mock.dart';

void main() {
test('registered instance', () {
SharePlusLinuxPlugin.registerWith();
Expand Down Expand Up @@ -34,34 +36,3 @@ void main() {
throwsException);
});
}

class MockUrlLauncherPlatform extends UrlLauncherPlatform {
String? url;
bool canLaunchMockValue = true;

@override
LinkDelegate? get linkDelegate => throw UnimplementedError();

@override
Future<bool> canLaunch(String url) async {
return canLaunchMockValue;
}

@override
Future<bool> launch(
String url, {
required bool useSafariVC,
required bool useWebView,
required bool enableJavaScript,
required bool enableDomStorage,
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) async {
this.url = url;
if (!canLaunchMockValue) {
throw Exception();
}
return true;
}
}
30 changes: 2 additions & 28 deletions packages/share_plus/share_plus/test/share_plus_windows_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:share_plus_platform_interface/share_plus_platform_interface.dart
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

import 'url_launcher_mock.dart';

void main() {
test(
'registered instance',
Expand Down Expand Up @@ -64,31 +66,3 @@ void main() {
skip: VersionHelper.instance.isWindows10RS5OrGreater,
);
}

class MockUrlLauncherPlatform extends UrlLauncherPlatform {
String? url;
bool canLaunchMockValue = true;

@override
LinkDelegate? get linkDelegate => throw UnimplementedError();

@override
Future<bool> canLaunch(String url) async {
return canLaunchMockValue;
}

@override
Future<bool> launch(
String url, {
required bool useSafariVC,
required bool useWebView,
required bool enableJavaScript,
required bool enableDomStorage,
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) async {
this.url = url;
return true;
}
}
30 changes: 30 additions & 0 deletions packages/share_plus/share_plus/test/url_launcher_mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

class MockUrlLauncherPlatform extends UrlLauncherPlatform {
String? url;
bool canLaunchMockValue = true;

@override
LinkDelegate? get linkDelegate => throw UnimplementedError();

@override
Future<bool> canLaunch(String url) async {
return canLaunchMockValue;
}

@override
Future<bool> launch(
String url, {
required bool useSafariVC,
required bool useWebView,
required bool enableJavaScript,
required bool enableDomStorage,
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) async {
this.url = url;
return canLaunchMockValue;
}
}

0 comments on commit f8f1a0b

Please sign in to comment.