Skip to content

Commit 436d4fc

Browse files
Harry Terkelsenmormih
authored andcommitted
[url_launcher] Migrate url_launcher_web to the platform interface (flutter#2237)
* [url_launcher] Migrate url_launcher_web to the platform interface * Fix analyzer errors * Add doc comments
1 parent 4f25d11 commit 436d4fc

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.0.2
2+
3+
- Switch to using `url_launcher_platform_interface`.
4+
15
# 0.0.1
26

37
- Initial open-source release.
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
11
import 'dart:async';
22
import 'dart:html' as html;
33

4-
import 'package:flutter/services.dart';
54
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
65
import 'package:meta/meta.dart';
6+
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
77

8-
class UrlLauncherPlugin {
8+
/// The web implementation of [UrlLauncherPlatform].
9+
///
10+
/// This class implements the `package:url_launcher` functionality for the web.
11+
class UrlLauncherPlugin extends UrlLauncherPlatform {
12+
/// Registers this class as the default instance of [UrlLauncherPlatform].
913
static void registerWith(Registrar registrar) {
10-
final MethodChannel channel = MethodChannel(
11-
'plugins.flutter.io/url_launcher',
12-
const StandardMethodCodec(),
13-
registrar.messenger);
14-
final UrlLauncherPlugin instance = UrlLauncherPlugin();
15-
channel.setMethodCallHandler(instance.handleMethodCall);
14+
UrlLauncherPlatform.instance = UrlLauncherPlugin();
1615
}
1716

18-
Future<dynamic> handleMethodCall(MethodCall call) async {
19-
switch (call.method) {
20-
case 'canLaunch':
21-
final String url = call.arguments['url'];
22-
return _canLaunch(url);
23-
case 'launch':
24-
final String url = call.arguments['url'];
25-
return _launch(url);
26-
default:
27-
throw PlatformException(
28-
code: 'Unimplemented',
29-
details: "The url_launcher plugin for web doesn't implement "
30-
"the method '${call.method}'");
31-
}
17+
/// Opens the given [url] in a new window.
18+
///
19+
/// Returns the newly created window.
20+
@visibleForTesting
21+
html.WindowBase openNewWindow(String url) {
22+
return html.window.open(url, '');
3223
}
3324

34-
bool _canLaunch(String url) {
25+
@override
26+
Future<bool> canLaunch(String url) {
3527
final Uri parsedUrl = Uri.tryParse(url);
36-
if (parsedUrl == null) return false;
37-
38-
return parsedUrl.isScheme('http') || parsedUrl.isScheme('https');
39-
}
28+
if (parsedUrl == null) return Future<bool>.value(false);
4029

41-
bool _launch(String url) {
42-
return openNewWindow(url) != null;
30+
return Future<bool>.value(
31+
parsedUrl.isScheme('http') || parsedUrl.isScheme('https'));
4332
}
4433

45-
@visibleForTesting
46-
html.WindowBase openNewWindow(String url) {
47-
return html.window.open(url, '');
34+
@override
35+
Future<bool> launch(
36+
String url, {
37+
@required bool useSafariVC,
38+
@required bool useWebView,
39+
@required bool enableJavaScript,
40+
@required bool enableDomStorage,
41+
@required bool universalLinksOnly,
42+
@required Map<String, String> headers,
43+
}) {
44+
return Future<bool>.value(openNewWindow(url) != null);
4845
}
4946
}

packages/url_launcher/url_launcher_web/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: url_launcher_web
22
description: Web platform implementation of url_launcher
33
author: Flutter Team <[email protected]>
44
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_web
5-
version: 0.0.1
5+
version: 0.0.2
66

77
flutter:
88
plugin:
@@ -17,6 +17,7 @@ dependencies:
1717
flutter_web_plugins:
1818
sdk: flutter
1919
meta: ^1.1.7
20+
url_launcher_platform_interface: ^1.0.1
2021

2122
dev_dependencies:
2223
flutter_test:

packages/url_launcher/url_launcher_web/test/url_launcher_web_test.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
import 'dart:html' as html;
88

99
import 'package:flutter_test/flutter_test.dart';
10-
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
1110
import 'package:url_launcher/url_launcher.dart';
1211
import 'package:url_launcher_web/url_launcher_web.dart';
12+
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
1313

1414
void main() {
1515
group('URL Launcher for Web', () {
1616
setUp(() {
17-
TestWidgetsFlutterBinding.ensureInitialized();
18-
webPluginRegistry.registerMessageHandler();
19-
final Registrar registrar =
20-
webPluginRegistry.registrarFor(UrlLauncherPlugin);
21-
UrlLauncherPlugin.registerWith(registrar);
17+
UrlLauncherPlatform.instance = UrlLauncherPlugin();
18+
});
19+
20+
test('$UrlLauncherPlugin is the live instance', () {
21+
expect(UrlLauncherPlatform.instance, isA<UrlLauncherPlugin>());
2222
});
2323

2424
test('can launch "http" URLs', () {
@@ -45,5 +45,9 @@ void main() {
4545
expect(newWindow, isNot(equals(html.window)));
4646
expect(newWindow.opener, equals(html.window));
4747
});
48+
49+
test('does not implement closeWebView()', () {
50+
expect(closeWebView(), throwsUnimplementedError);
51+
});
4852
});
4953
}

0 commit comments

Comments
 (0)