Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(share_plus): remove canLaunch check #1315

Merged
merged 7 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed for consistency with the other platforms

@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 mailto: URI');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As commented, the launchUrl can either return false or fail with an exception, in the case of returning false we throw an Exception.

I did not use PlatformException here because I don't think we should be creating them outside platform code, so I think just a generic Exception is fine here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it makes sense.

}
}

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 mailto: URI');
}
}
}
Expand Down
11 changes: 7 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 @@ -3,6 +3,7 @@ library share_plus_windows;

import 'dart:ui';

import 'package:flutter/services.dart';
miquelbeltran marked this conversation as resolved.
Show resolved Hide resolved
import 'package:share_plus/src/windows_version_helper.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
Expand Down Expand Up @@ -44,10 +45,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 mailto: 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;
miquelbeltran marked this conversation as resolved.
Show resolved Hide resolved
}
}