wakelock_web.dart
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import 'dart:async';
import 'dart:js';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:import_js_library/import_js_library.dart';
import 'wakelock_js.dart' as wakelock;
/// A web implementation of the Wakelock plugin.
class WakelockWebPlugin {
final _isNativeWakelockSupported =
context['navigator'].hasProperty('wakeLock');
var _enabled = false;
static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'wakelock',
const StandardMethodCodec(),
registrar.messenger,
);
/// Import JS library
importJsLibrary(url: './assets/NoSleep.js', flutterPluginName: 'wakelock');
final pluginInstance = WakelockWebPlugin();
channel.setMethodCallHandler(pluginInstance.handleMethodCall);
}
/// Handles method calls over the MethodChannel of this plugin.
/// Note: Check the "federated" architecture for a new way of doing this:
/// https://flutter.dev/go/federated-plugins
Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'toggle':
final bool enable = call.arguments['enable'];
return Future.value(_toggle(enable));
break;
case 'isEnabled':
return Future.value(_isEnabled());
break;
default:
throw PlatformException(
code: 'Unimplemented',
details: 'wakelock_web for web doesn\'t implement \'${call.method}\'',
);
}
}
_toggle(bool enable) {
if (enable) {
wakelock.enable();
} else {
wakelock.disable();
}
_enabled = enable;
}
bool _isEnabled() {
/// If the native WebLock API is supported
/// since these APIs are async in nature
/// Give immediate boolean value based on `_enabled`
if (_isNativeWakelockSupported) {
return _enabled;
}
return wakelock.isEnabled();
}
}