1
- import 'dart:html' as html;
2
1
import 'dart:async' ;
2
+ import 'dart:js_interop' ;
3
+
4
+ import 'package:web/web.dart' as web;
3
5
4
6
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart' ;
5
7
@@ -8,21 +10,21 @@ import 'package:permission_handler_platform_interface/permission_handler_platfor
8
10
class WebDelegate {
9
11
/// Constructs a WebDelegate.
10
12
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,
14
16
) : _devices = devices,
15
17
_geolocation = geolocation,
16
18
_htmlPermissions = permissions;
17
19
18
20
/// The html media devices object used to request camera and microphone permissions.
19
- final html .MediaDevices ? _devices;
21
+ final web .MediaDevices ? _devices;
20
22
21
23
/// The html geolocation object used to request location permission.
22
- final html .Geolocation ? _geolocation;
24
+ final web .Geolocation ? _geolocation;
23
25
24
26
/// The html permissions object used to check permission status.
25
- final html .Permissions ? _htmlPermissions;
27
+ final web .Permissions ? _htmlPermissions;
26
28
27
29
/// The permission name to request access to the camera.
28
30
static const _microphonePermissionName = 'microphone' ;
@@ -58,8 +60,10 @@ class WebDelegate {
58
60
}
59
61
}
60
62
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;
63
67
return _toPermissionStatus (webPermissionStatus? .state);
64
68
}
65
69
@@ -69,7 +73,9 @@ class WebDelegate {
69
73
}
70
74
71
75
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;
73
79
74
80
// In browsers, calling [getUserMedia] will start the recording
75
81
// automatically right after. This is undesired behavior as
@@ -78,13 +84,13 @@ class WebDelegate {
78
84
// The manual stop action is then needed here for to stop the automatic
79
85
// recording.
80
86
81
- if (mediaStream.active ?? false ) {
82
- final audioTracks = mediaStream.getAudioTracks ();
87
+ if (mediaStream? .active ?? false ) {
88
+ final audioTracks = mediaStream? .getAudioTracks ().toDart ?? [] ;
83
89
if (audioTracks.isNotEmpty) {
84
90
audioTracks[0 ].stop ();
85
91
}
86
92
}
87
- } on html. DomException {
93
+ } on web. DOMException {
88
94
return false ;
89
95
}
90
96
@@ -97,7 +103,9 @@ class WebDelegate {
97
103
}
98
104
99
105
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;
101
109
102
110
// In browsers, calling [getUserMedia] will start the recording
103
111
// automatically right after. This is undesired behavior as
@@ -106,38 +114,51 @@ class WebDelegate {
106
114
// The manual stop action is then needed here for to stop the automatic
107
115
// recording.
108
116
109
- if (mediaStream.active ?? false ) {
110
- final videoTracks = mediaStream.getVideoTracks ();
117
+ if (mediaStream? .active ?? false ) {
118
+ final videoTracks = mediaStream? .getVideoTracks ().toDart ?? [] ;
111
119
if (videoTracks.isNotEmpty) {
112
120
videoTracks[0 ].stop ();
113
121
}
114
122
}
115
- } on html. DomException {
123
+ } on web. DOMException {
116
124
return false ;
117
125
}
118
126
119
127
return true ;
120
128
}
121
129
122
130
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));
124
134
}
125
135
126
136
Future <bool > _requestLocationPermission () async {
137
+ Completer <bool > completer = Completer <bool >();
127
138
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 );
131
149
}
150
+ return completer.future;
132
151
}
133
152
134
- Future <PermissionStatus > _requestSingularPermission (Permission permission) async {
153
+ Future <PermissionStatus > _requestSingularPermission (
154
+ Permission permission) async {
135
155
bool permissionGranted = switch (permission) {
136
156
Permission .microphone => await _requestMicrophonePermission (),
137
157
Permission .camera => await _requestCameraPermission (),
138
158
Permission .notification => await _requestNotificationPermission (),
139
159
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.' )
141
162
};
142
163
143
164
if (! permissionGranted) {
@@ -150,12 +171,14 @@ class WebDelegate {
150
171
/// they have not already been granted before.
151
172
///
152
173
/// 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 {
154
176
final Map <Permission , PermissionStatus > permissionStatusMap = {};
155
177
156
178
for (final permission in permissions) {
157
179
try {
158
- permissionStatusMap[permission] = await _requestSingularPermission (permission);
180
+ permissionStatusMap[permission] =
181
+ await _requestSingularPermission (permission);
159
182
} on UnimplementedError {
160
183
rethrow ;
161
184
}
0 commit comments