Skip to content

Commit c4a2034

Browse files
authored
migrate to package:web (#1303)
1 parent 9290ecd commit c4a2034

File tree

9 files changed

+109
-2144
lines changed

9 files changed

+109
-2144
lines changed

permission_handler_html/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.2
2+
3+
* Migrate to package:web and adding wasm support.
4+
15
## 0.1.1
26

37
* Updates the dependency on `permission_handler_platform_interface` to version 4.1.0 (SiriKit support is only available for iOS and macOS).
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file configures the analyzer, which statically analyzes Dart code to
2+
# check for errors, warnings, and lints.
3+
#
4+
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
5+
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
6+
# invoked from the command line by running `flutter analyze`.
7+
8+
# The following line activates a set of recommended lints for Flutter apps,
9+
# packages, and plugins designed to encourage good coding practices.
10+
include: package:flutter_lints/flutter.yaml
11+
12+
linter:
13+
# The lint rules applied to this project can be customized in the
14+
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
15+
# included above or to enable additional rules. A list of all available lints
16+
# and their documentation is published at https://dart.dev/lints.
17+
#
18+
# Instead of disabling a lint rule for the entire project in the
19+
# section below, it can also be suppressed for a single line of code
20+
# or a specific dart file by using the `// ignore: name_of_lint` and
21+
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
22+
# producing the lint.
23+
rules:
24+
# avoid_print: false # Uncomment to disable the `avoid_print` rule
25+
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
26+
27+
# Additional information about this file can be found at
28+
# https://dart.dev/guides/language/analysis-options

permission_handler_html/example/web/index.html

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html>
3+
34
<head>
45
<!--
56
If you are serving your web app in a path other than the root, change the
@@ -27,33 +28,23 @@
2728
<link rel="apple-touch-icon" href="icons/Icon-192.png">
2829

2930
<!-- Favicon -->
30-
<link rel="icon" type="image/png" href="favicon.png"/>
31+
<link rel="icon" type="image/png" href="favicon.png" />
3132

3233
<title>example</title>
3334
<link rel="manifest" href="manifest.json">
3435

3536
<script>
36-
// The value below is injected by flutter build, do not touch.
37-
var serviceWorkerVersion = null;
37+
{{flutter_service_worker_version}}
3838
</script>
3939
<!-- This script adds the flutter initialization JS code -->
40-
<script src="flutter.js" defer></script>
40+
<script src="flutter.js"></script>
4141
</head>
42+
4243
<body>
4344
<script>
44-
window.addEventListener('load', function(ev) {
45-
// Download main.dart.js
46-
_flutter.loader.loadEntrypoint({
47-
serviceWorker: {
48-
serviceWorkerVersion: serviceWorkerVersion,
49-
},
50-
onEntrypointLoaded: function(engineInitializer) {
51-
engineInitializer.initializeEngine().then(function(appRunner) {
52-
appRunner.runApp();
53-
});
54-
}
55-
});
56-
});
45+
{{flutter_build_config}}
46+
_flutter.loader.load();
5747
</script>
5848
</body>
59-
</html>
49+
50+
</html>

permission_handler_html/lib/permission_handler_html.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import 'dart:html' as html;
21
import 'dart:async';
32

3+
import 'package:web/web.dart' as web;
4+
45
import 'package:flutter/foundation.dart';
56
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
67
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart';
@@ -9,11 +10,10 @@ import 'web_delegate.dart';
910

1011
/// Platform implementation of the permission_handler Flutter plugin.
1112
class WebPermissionHandler extends PermissionHandlerPlatform {
12-
static final html.MediaDevices? _devices = html.window.navigator.mediaDevices;
13-
static final html.Geolocation _geolocation =
14-
html.window.navigator.geolocation;
15-
static final html.Permissions? _htmlPermissions =
16-
html.window.navigator.permissions;
13+
static final web.MediaDevices? _devices = web.window.navigator.mediaDevices;
14+
static final web.Geolocation _geolocation = web.window.navigator.geolocation;
15+
static final web.Permissions? _htmlPermissions =
16+
web.window.navigator.permissions;
1717

1818
final WebDelegate _webDelegate;
1919

permission_handler_html/lib/web_delegate.dart

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import 'dart:html' as html;
21
import 'dart:async';
2+
import 'dart:js_interop';
3+
4+
import 'package:web/web.dart' as web;
35

46
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart';
57

@@ -8,21 +10,21 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor
810
class WebDelegate {
911
/// Constructs a WebDelegate.
1012
WebDelegate(
11-
html.MediaDevices? devices,
12-
html.Geolocation? geolocation,
13-
html.Permissions? permissions,
13+
web.MediaDevices? devices,
14+
web.Geolocation? geolocation,
15+
web.Permissions? permissions,
1416
) : _devices = devices,
1517
_geolocation = geolocation,
1618
_htmlPermissions = permissions;
1719

1820
/// The html media devices object used to request camera and microphone permissions.
19-
final html.MediaDevices? _devices;
21+
final web.MediaDevices? _devices;
2022

2123
/// The html geolocation object used to request location permission.
22-
final html.Geolocation? _geolocation;
24+
final web.Geolocation? _geolocation;
2325

2426
/// The html permissions object used to check permission status.
25-
final html.Permissions? _htmlPermissions;
27+
final web.Permissions? _htmlPermissions;
2628

2729
/// The permission name to request access to the camera.
2830
static const _microphonePermissionName = 'microphone';
@@ -58,8 +60,10 @@ class WebDelegate {
5860
}
5961
}
6062

61-
Future<PermissionStatus> _permissionStatusState(String webPermissionName, html.Permissions? permissions) async {
62-
final webPermissionStatus = await permissions?.query({'name': webPermissionName});
63+
Future<PermissionStatus> _permissionStatusState(
64+
String webPermissionName, web.Permissions? permissions) async {
65+
final webPermissionStatus =
66+
await permissions?.query({'name': webPermissionName}.toJSBox).toDart;
6367
return _toPermissionStatus(webPermissionStatus?.state);
6468
}
6569

@@ -69,7 +73,9 @@ class WebDelegate {
6973
}
7074

7175
try {
72-
html.MediaStream mediaStream = await _devices!.getUserMedia({'audio': true});
76+
web.MediaStream? mediaStream = await _devices
77+
?.getUserMedia(web.MediaStreamConstraints(audio: true.toJS))
78+
.toDart;
7379

7480
// In browsers, calling [getUserMedia] will start the recording
7581
// automatically right after. This is undesired behavior as
@@ -78,13 +84,13 @@ class WebDelegate {
7884
// The manual stop action is then needed here for to stop the automatic
7985
// recording.
8086

81-
if (mediaStream.active ?? false) {
82-
final audioTracks = mediaStream.getAudioTracks();
87+
if (mediaStream?.active ?? false) {
88+
final audioTracks = mediaStream?.getAudioTracks().toDart ?? [];
8389
if (audioTracks.isNotEmpty) {
8490
audioTracks[0].stop();
8591
}
8692
}
87-
} on html.DomException {
93+
} on web.DOMException {
8894
return false;
8995
}
9096

@@ -97,7 +103,9 @@ class WebDelegate {
97103
}
98104

99105
try {
100-
html.MediaStream mediaStream = await _devices!.getUserMedia({'video': true});
106+
web.MediaStream? mediaStream = await _devices
107+
?.getUserMedia(web.MediaStreamConstraints(video: true.toJS))
108+
.toDart;
101109

102110
// In browsers, calling [getUserMedia] will start the recording
103111
// automatically right after. This is undesired behavior as
@@ -106,38 +114,51 @@ class WebDelegate {
106114
// The manual stop action is then needed here for to stop the automatic
107115
// recording.
108116

109-
if (mediaStream.active ?? false) {
110-
final videoTracks = mediaStream.getVideoTracks();
117+
if (mediaStream?.active ?? false) {
118+
final videoTracks = mediaStream?.getVideoTracks().toDart ?? [];
111119
if (videoTracks.isNotEmpty) {
112120
videoTracks[0].stop();
113121
}
114122
}
115-
} on html.DomException {
123+
} on web.DOMException {
116124
return false;
117125
}
118126

119127
return true;
120128
}
121129

122130
Future<bool> _requestNotificationPermission() async {
123-
return html.Notification.requestPermission().then((permission) => permission == "granted");
131+
return web.Notification.requestPermission()
132+
.toDart
133+
.then((permission) => (permission == "granted".toJS));
124134
}
125135

126136
Future<bool> _requestLocationPermission() async {
137+
Completer<bool> completer = Completer<bool>();
127138
try {
128-
return await _geolocation?.getCurrentPosition().then((value) => true) ?? false;
129-
} on html.PositionError {
130-
return false;
139+
_geolocation?.getCurrentPosition(
140+
(JSAny _) {
141+
completer.complete(true);
142+
}.toJS,
143+
(JSAny _) {
144+
completer.complete(false);
145+
}.toJS,
146+
);
147+
} catch (_) {
148+
completer.complete(false);
131149
}
150+
return completer.future;
132151
}
133152

134-
Future<PermissionStatus> _requestSingularPermission(Permission permission) async {
153+
Future<PermissionStatus> _requestSingularPermission(
154+
Permission permission) async {
135155
bool permissionGranted = switch (permission) {
136156
Permission.microphone => await _requestMicrophonePermission(),
137157
Permission.camera => await _requestCameraPermission(),
138158
Permission.notification => await _requestNotificationPermission(),
139159
Permission.location => await _requestLocationPermission(),
140-
_ => throw UnsupportedError('The ${permission.toString()} permission is currently not supported on web.')
160+
_ => throw UnsupportedError(
161+
'The ${permission.toString()} permission is currently not supported on web.')
141162
};
142163

143164
if (!permissionGranted) {
@@ -150,12 +171,14 @@ class WebDelegate {
150171
/// they have not already been granted before.
151172
///
152173
/// Returns a [Map] containing the status per requested [Permission].
153-
Future<Map<Permission, PermissionStatus>> requestPermissions(List<Permission> permissions) async {
174+
Future<Map<Permission, PermissionStatus>> requestPermissions(
175+
List<Permission> permissions) async {
154176
final Map<Permission, PermissionStatus> permissionStatusMap = {};
155177

156178
for (final permission in permissions) {
157179
try {
158-
permissionStatusMap[permission] = await _requestSingularPermission(permission);
180+
permissionStatusMap[permission] =
181+
await _requestSingularPermission(permission);
159182
} on UnimplementedError {
160183
rethrow;
161184
}

permission_handler_html/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: permission_handler_html
22
description: Permission plugin for Flutter. This plugin provides the web API to request and check permissions.
3-
version: 0.1.1
3+
version: 0.1.2
44
homepage: https://github.com/baseflow/flutter-permission-handler
55

66
environment:
@@ -13,6 +13,7 @@ dependencies:
1313
flutter_web_plugins:
1414
sdk: flutter
1515
permission_handler_platform_interface: ^4.1.0
16+
web: ^0.5.1
1617

1718
dev_dependencies:
1819
flutter_test:

0 commit comments

Comments
 (0)