Skip to content

Commit 142eea5

Browse files
stuartmorgan-gyutaaraki-toydium
authored andcommitted
[url_launcher] Switch to new launchUrl interface (flutter#5985)
1 parent ee1892a commit 142eea5

File tree

9 files changed

+98
-83
lines changed

9 files changed

+98
-83
lines changed

packages/url_launcher/url_launcher/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 6.1.4
22

3+
* Adopts new platform interface method for launching URLs.
34
* Ignores unnecessary import warnings in preparation for [upcoming Flutter changes](https://github.com/flutter/flutter/pull/105648).
45

56
## 6.1.3
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
6+
7+
import 'types.dart';
8+
9+
/// Converts an (app-facing) [WebViewConfiguration] to a (platform interface)
10+
/// [InAppWebViewConfiguration].
11+
InAppWebViewConfiguration convertConfiguration(WebViewConfiguration config) {
12+
return InAppWebViewConfiguration(
13+
enableJavaScript: config.enableJavaScript,
14+
enableDomStorage: config.enableDomStorage,
15+
headers: config.headers,
16+
);
17+
}
18+
19+
/// Converts an (app-facing) [LaunchMode] to a (platform interface)
20+
/// [PreferredLaunchMode].
21+
PreferredLaunchMode convertLaunchMode(LaunchMode mode) {
22+
switch (mode) {
23+
case LaunchMode.platformDefault:
24+
return PreferredLaunchMode.platformDefault;
25+
case LaunchMode.inAppWebView:
26+
return PreferredLaunchMode.inAppWebView;
27+
case LaunchMode.externalApplication:
28+
return PreferredLaunchMode.externalApplication;
29+
case LaunchMode.externalNonBrowserApplication:
30+
return PreferredLaunchMode.externalNonBrowserApplication;
31+
}
32+
}

packages/url_launcher/url_launcher/lib/src/url_launcher_string.dart

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
66

7+
import 'type_conversion.dart';
78
import 'types.dart';
89

910
/// String version of [launchUrl].
@@ -24,27 +25,18 @@ Future<bool> launchUrlString(
2425
WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
2526
String? webOnlyWindowName,
2627
}) async {
27-
final bool isWebURL =
28-
urlString.startsWith('http:') || urlString.startsWith('https:');
29-
if (mode == LaunchMode.inAppWebView && !isWebURL) {
28+
if (mode == LaunchMode.inAppWebView &&
29+
!(urlString.startsWith('https:') || urlString.startsWith('http:'))) {
3030
throw ArgumentError.value(urlString, 'urlString',
3131
'To use an in-app web view, you must provide an http(s) URL.');
3232
}
33-
final bool useWebView = mode == LaunchMode.inAppWebView ||
34-
(isWebURL && mode == LaunchMode.platformDefault);
35-
36-
// TODO(stuartmorgan): Create a replacement platform interface method that
37-
// uses something more like the new argument structure, and switch to using
38-
// that, to support launch mode on more platforms.
39-
return await UrlLauncherPlatform.instance.launch(
33+
return await UrlLauncherPlatform.instance.launchUrl(
4034
urlString,
41-
useSafariVC: useWebView,
42-
useWebView: useWebView,
43-
enableJavaScript: webViewConfiguration.enableJavaScript,
44-
enableDomStorage: webViewConfiguration.enableDomStorage,
45-
universalLinksOnly: mode == LaunchMode.externalNonBrowserApplication,
46-
headers: webViewConfiguration.headers,
47-
webOnlyWindowName: webOnlyWindowName,
35+
LaunchOptions(
36+
mode: convertLaunchMode(mode),
37+
webViewConfiguration: convertConfiguration(webViewConfiguration),
38+
webOnlyWindowName: webOnlyWindowName,
39+
),
4840
);
4941
}
5042

packages/url_launcher/url_launcher/lib/src/url_launcher_uri.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'dart:async';
77
import 'package:url_launcher/url_launcher_string.dart';
88
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
99

10+
import 'type_conversion.dart';
11+
1012
/// Passes [url] to the underlying platform for handling.
1113
///
1214
/// [mode] support varies significantly by platform:
@@ -43,20 +45,18 @@ Future<bool> launchUrl(
4345
WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
4446
String? webOnlyWindowName,
4547
}) async {
46-
final bool isWebURL = url.scheme == 'http' || url.scheme == 'https';
47-
if (mode == LaunchMode.inAppWebView && !isWebURL) {
48+
if (mode == LaunchMode.inAppWebView &&
49+
!(url.scheme == 'https' || url.scheme == 'http')) {
4850
throw ArgumentError.value(url, 'url',
4951
'To use an in-app web view, you must provide an http(s) URL.');
5052
}
51-
// TODO(stuartmorgan): Use UrlLauncherPlatform directly once a new API
52-
// that better matches these parameters has been added. For now, delegate to
53-
// launchUrlString so that there's only one copy of the parameter translation
54-
// logic.
55-
return await launchUrlString(
53+
return await UrlLauncherPlatform.instance.launchUrl(
5654
url.toString(),
57-
mode: mode,
58-
webViewConfiguration: webViewConfiguration,
59-
webOnlyWindowName: webOnlyWindowName,
55+
LaunchOptions(
56+
mode: convertLaunchMode(mode),
57+
webViewConfiguration: convertConfiguration(webViewConfiguration),
58+
webOnlyWindowName: webOnlyWindowName,
59+
),
6060
);
6161
}
6262

packages/url_launcher/url_launcher/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL. Supports
33
web, phone, SMS, and email schemes.
44
repository: https://github.com/flutter/plugins/tree/main/packages/url_launcher/url_launcher
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
6-
version: 6.1.3
6+
version: 6.1.4
77

88
environment:
99
sdk: ">=2.14.0 <3.0.0"
@@ -34,7 +34,7 @@ dependencies:
3434
# implementations, as both are compatible.
3535
url_launcher_linux: ">=2.0.0 <4.0.0"
3636
url_launcher_macos: ">=2.0.0 <4.0.0"
37-
url_launcher_platform_interface: ^2.0.3
37+
url_launcher_platform_interface: ^2.1.0
3838
url_launcher_web: ^2.0.0
3939
url_launcher_windows: ">=2.0.0 <4.0.0"
4040

packages/url_launcher/url_launcher/test/link_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void main() {
1919
UrlLauncherPlatform.instance = mock;
2020
});
2121

22-
group('$Link', () {
22+
group('Link', () {
2323
testWidgets('handles null uri correctly', (WidgetTester tester) async {
2424
bool isBuilt = false;
2525
FollowLink? followLink;
@@ -55,8 +55,7 @@ void main() {
5555
mock
5656
..setLaunchExpectations(
5757
url: 'http://example.com/foobar',
58-
useSafariVC: false,
59-
useWebView: false,
58+
launchMode: PreferredLaunchMode.externalApplication,
6059
universalLinksOnly: false,
6160
enableJavaScript: true,
6261
enableDomStorage: true,
@@ -85,8 +84,7 @@ void main() {
8584
mock
8685
..setLaunchExpectations(
8786
url: 'http://example.com/foobar',
88-
useSafariVC: true,
89-
useWebView: true,
87+
launchMode: PreferredLaunchMode.inAppWebView,
9088
universalLinksOnly: false,
9189
enableJavaScript: true,
9290
enableDomStorage: true,

packages/url_launcher/url_launcher/test/mocks/mock_url_launcher_platform.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class MockUrlLauncher extends Fake
1111
with MockPlatformInterfaceMixin
1212
implements UrlLauncherPlatform {
1313
String? url;
14+
PreferredLaunchMode? launchMode;
1415
bool? useSafariVC;
1516
bool? useWebView;
1617
bool? enableJavaScript;
@@ -32,15 +33,17 @@ class MockUrlLauncher extends Fake
3233

3334
void setLaunchExpectations({
3435
required String url,
35-
required bool? useSafariVC,
36-
required bool useWebView,
36+
PreferredLaunchMode? launchMode,
37+
bool? useSafariVC,
38+
bool? useWebView,
3739
required bool enableJavaScript,
3840
required bool enableDomStorage,
3941
required bool universalLinksOnly,
4042
required Map<String, String> headers,
4143
required String? webOnlyWindowName,
4244
}) {
4345
this.url = url;
46+
this.launchMode = launchMode;
4447
this.useSafariVC = useSafariVC;
4548
this.useWebView = useWebView;
4649
this.enableJavaScript = enableJavaScript;
@@ -88,6 +91,18 @@ class MockUrlLauncher extends Fake
8891
return response!;
8992
}
9093

94+
@override
95+
Future<bool> launchUrl(String url, LaunchOptions options) async {
96+
expect(url, this.url);
97+
expect(options.mode, launchMode);
98+
expect(options.webViewConfiguration.enableJavaScript, enableJavaScript);
99+
expect(options.webViewConfiguration.enableDomStorage, enableDomStorage);
100+
expect(options.webViewConfiguration.headers, headers);
101+
expect(options.webOnlyWindowName, webOnlyWindowName);
102+
launchCalled = true;
103+
return response!;
104+
}
105+
91106
@override
92107
Future<void> closeWebView() async {
93108
closeWebViewCalled = true;

packages/url_launcher/url_launcher/test/src/url_launcher_string_test.dart

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ void main() {
4343
mock
4444
..setLaunchExpectations(
4545
url: urlString,
46-
useSafariVC: true,
47-
useWebView: true,
46+
launchMode: PreferredLaunchMode.platformDefault,
4847
enableJavaScript: true,
4948
enableDomStorage: true,
5049
universalLinksOnly: false,
@@ -60,8 +59,7 @@ void main() {
6059
mock
6160
..setLaunchExpectations(
6261
url: urlString,
63-
useSafariVC: false,
64-
useWebView: false,
62+
launchMode: PreferredLaunchMode.platformDefault,
6563
enableJavaScript: true,
6664
enableDomStorage: true,
6765
universalLinksOnly: false,
@@ -77,8 +75,7 @@ void main() {
7775
mock
7876
..setLaunchExpectations(
7977
url: urlString,
80-
useSafariVC: true,
81-
useWebView: true,
78+
launchMode: PreferredLaunchMode.platformDefault,
8279
enableJavaScript: true,
8380
enableDomStorage: true,
8481
universalLinksOnly: false,
@@ -95,8 +92,7 @@ void main() {
9592
mock
9693
..setLaunchExpectations(
9794
url: urlString,
98-
useSafariVC: false,
99-
useWebView: false,
95+
launchMode: PreferredLaunchMode.platformDefault,
10096
enableJavaScript: true,
10197
enableDomStorage: true,
10298
universalLinksOnly: false,
@@ -113,8 +109,7 @@ void main() {
113109
mock
114110
..setLaunchExpectations(
115111
url: urlString,
116-
useSafariVC: true,
117-
useWebView: true,
112+
launchMode: PreferredLaunchMode.inAppWebView,
118113
enableJavaScript: true,
119114
enableDomStorage: true,
120115
universalLinksOnly: false,
@@ -131,8 +126,7 @@ void main() {
131126
mock
132127
..setLaunchExpectations(
133128
url: urlString,
134-
useSafariVC: false,
135-
useWebView: false,
129+
launchMode: PreferredLaunchMode.externalApplication,
136130
enableJavaScript: true,
137131
enableDomStorage: true,
138132
universalLinksOnly: false,
@@ -151,8 +145,7 @@ void main() {
151145
mock
152146
..setLaunchExpectations(
153147
url: urlString,
154-
useSafariVC: false,
155-
useWebView: false,
148+
launchMode: PreferredLaunchMode.externalNonBrowserApplication,
156149
enableJavaScript: true,
157150
enableDomStorage: true,
158151
universalLinksOnly: true,
@@ -171,8 +164,7 @@ void main() {
171164
mock
172165
..setLaunchExpectations(
173166
url: urlString,
174-
useSafariVC: true,
175-
useWebView: true,
167+
launchMode: PreferredLaunchMode.inAppWebView,
176168
enableJavaScript: false,
177169
enableDomStorage: true,
178170
universalLinksOnly: false,
@@ -193,8 +185,7 @@ void main() {
193185
mock
194186
..setLaunchExpectations(
195187
url: urlString,
196-
useSafariVC: true,
197-
useWebView: true,
188+
launchMode: PreferredLaunchMode.inAppWebView,
198189
enableJavaScript: true,
199190
enableDomStorage: false,
200191
universalLinksOnly: false,
@@ -215,8 +206,7 @@ void main() {
215206
mock
216207
..setLaunchExpectations(
217208
url: urlString,
218-
useSafariVC: true,
219-
useWebView: true,
209+
launchMode: PreferredLaunchMode.inAppWebView,
220210
enableJavaScript: true,
221211
enableDomStorage: true,
222212
universalLinksOnly: false,
@@ -245,8 +235,7 @@ void main() {
245235
mock
246236
..setLaunchExpectations(
247237
url: emailLaunchUrlString,
248-
useSafariVC: false,
249-
useWebView: false,
238+
launchMode: PreferredLaunchMode.platformDefault,
250239
enableJavaScript: true,
251240
enableDomStorage: true,
252241
universalLinksOnly: false,
@@ -264,8 +253,7 @@ void main() {
264253
mock
265254
..setLaunchExpectations(
266255
url: urlString,
267-
useSafariVC: false,
268-
useWebView: false,
256+
launchMode: PreferredLaunchMode.platformDefault,
269257
enableJavaScript: true,
270258
enableDomStorage: true,
271259
universalLinksOnly: false,

0 commit comments

Comments
 (0)