creativecreatorormaybenot
Committed by GitHub

Migrate to null safety (#62)

* Migrate wakelock_web

* Adjust constraint

* flutter constraint

* Finish

* Temporarily unsound
@@ -96,4 +96,4 @@ jobs: @@ -96,4 +96,4 @@ jobs:
96 if: matrix.package == 'wakelock' 96 if: matrix.package == 'wakelock'
97 run: | 97 run: |
98 cd wakelock/example 98 cd wakelock/example
99 - flutter drive --driver=test_driver/integration_test.dart --target=integration_test/wakelock_test.dart 99 + flutter drive --driver=test_driver/integration_test.dart --target=integration_test/wakelock_test.dart --no-sound-null-safety
  1 +## 0.3.0-nullsafety.0
  2 +
  3 +* Migrated to null safety.
  4 +* **Breaking**: removed deprecated `isEnabled` and `on` in `toggle`.
  5 +
1 ## 0.2.1+1 6 ## 0.2.1+1
2 7
3 * Fixed Android build warning (`import_js_library`) by bumping `wakelock_web`. 8 * Fixed Android build warning (`import_js_library`) by bumping `wakelock_web`.
1 import 'package:flutter_test/flutter_test.dart'; 1 import 'package:flutter_test/flutter_test.dart';
  2 +
  3 +// See https://github.com/flutter/flutter/issues/71379.
  4 +// ignore: import_of_legacy_library_into_null_safe
2 import 'package:integration_test/integration_test.dart'; 5 import 'package:integration_test/integration_test.dart';
3 import 'package:wakelock/wakelock.dart'; 6 import 'package:wakelock/wakelock.dart';
4 7
@@ -58,16 +58,17 @@ class _WakelockExampleAppState extends State<WakelockExampleApp> { @@ -58,16 +58,17 @@ class _WakelockExampleAppState extends State<WakelockExampleApp> {
58 FutureBuilder( 58 FutureBuilder(
59 future: Wakelock.enabled, 59 future: Wakelock.enabled,
60 builder: (context, AsyncSnapshot<bool> snapshot) { 60 builder: (context, AsyncSnapshot<bool> snapshot) {
  61 + final data = snapshot.data;
61 // The use of FutureBuilder is necessary here to await the 62 // The use of FutureBuilder is necessary here to await the
62 // bool value from the `enabled` getter. 63 // bool value from the `enabled` getter.
63 - if (!snapshot.hasData) { 64 + if (data == null) {
64 // The Future is retrieved so fast that you will not be able 65 // The Future is retrieved so fast that you will not be able
65 // to see any loading indicator. 66 // to see any loading indicator.
66 return Container(); 67 return Container();
67 } 68 }
68 69
69 return Text('The wakelock is currently ' 70 return Text('The wakelock is currently '
70 - '${snapshot.data ? 'enabled' : 'disabled'}.'); 71 + '${data ? 'enabled' : 'disabled'}.');
71 }, 72 },
72 ), 73 ),
73 const Spacer( 74 const Spacer(
@@ -5,7 +5,7 @@ description: >-2 @@ -5,7 +5,7 @@ description: >-2
5 publish_to: 'none' 5 publish_to: 'none'
6 6
7 environment: 7 environment:
8 - sdk: ">=2.7.0 <3.0.0" 8 + sdk: '>=2.12.0-0 <3.0.0'
9 9
10 dependencies: 10 dependencies:
11 flutter: 11 flutter:
@@ -24,7 +24,8 @@ dev_dependencies: @@ -24,7 +24,8 @@ dev_dependencies:
24 sdk: flutter 24 sdk: flutter
25 flutter_driver: 25 flutter_driver:
26 sdk: flutter 26 sdk: flutter
27 - integration_test: ^0.9.1 27 +
  28 + integration_test: ^0.9.2+2
28 test: any 29 test: any
29 30
30 - pedantic: ^1.9.2 31 + pedantic: ^1.10.0-nullsafety.3
  1 +// See https://github.com/flutter/flutter/issues/71379.
  2 +// ignore: import_of_legacy_library_into_null_safe
1 import 'package:integration_test/integration_test_driver.dart'; 3 import 'package:integration_test/integration_test_driver.dart';
2 4
3 Future<void> main() => integrationDriver(); 5 Future<void> main() => integrationDriver();
1 -import 'package:meta/meta.dart';  
2 import 'package:wakelock_platform_interface/wakelock_platform_interface.dart'; 1 import 'package:wakelock_platform_interface/wakelock_platform_interface.dart';
3 2
4 final _wakelockPlatformInstance = WakelockPlatformInterface.instance; 3 final _wakelockPlatformInstance = WakelockPlatformInterface.instance;
@@ -53,20 +52,9 @@ class Wakelock { @@ -53,20 +52,9 @@ class Wakelock {
53 /// 52 ///
54 /// You can await the [Future] to wait for the operation to complete. 53 /// You can await the [Future] to wait for the operation to complete.
55 static Future<void> toggle({ 54 static Future<void> toggle({
56 - @required bool enable,  
57 - @Deprecated('Use the `enable` parameter instead.') bool on, 55 + required bool enable,
58 }) { 56 }) {
59 - // The checks allow only `on` to be used in the case of old code and  
60 - // they encourage to use only `enable` instead (combined with the  
61 - // deprecation warning).  
62 - assert(enable != null || on != null,  
63 - 'The `enable` parameter must not be null when toggling the wakelock.');  
64 - assert(  
65 - on == null || enable == null,  
66 - 'The `on` parameter has been deprecated; '  
67 - 'specify only the `enable` parameter instead.');  
68 -  
69 - return _wakelockPlatformInstance.toggle(enable: enable ?? on); 57 + return _wakelockPlatformInstance.toggle(enable: enable);
70 } 58 }
71 59
72 /// Returns whether the wakelock is currently enabled or not. 60 /// Returns whether the wakelock is currently enabled or not.
@@ -78,8 +66,4 @@ class Wakelock { @@ -78,8 +66,4 @@ class Wakelock {
78 /// bool wakelockEnabled = await Wakelock.enabled; 66 /// bool wakelockEnabled = await Wakelock.enabled;
79 /// ``` 67 /// ```
80 static Future<bool> get enabled => _wakelockPlatformInstance.enabled; 68 static Future<bool> get enabled => _wakelockPlatformInstance.enabled;
81 -  
82 - /// Returns the current wakelock status.  
83 - @Deprecated('Use the `enabled` getter instead.')  
84 - static Future<bool> get isEnabled => enabled;  
85 } 69 }
  1 +// This is fine because it is a dev dependency.
  2 +// See https://github.com/flutter/flutter/issues/71360.
  3 +// @dart=2.9
1 import 'package:pigeon/pigeon.dart'; 4 import 'package:pigeon/pigeon.dart';
2 5
3 /// Message for toggling the wakelock on the platform side. 6 /// Message for toggling the wakelock on the platform side.
@@ -2,12 +2,12 @@ name: wakelock @@ -2,12 +2,12 @@ name: wakelock
2 description: >-2 2 description: >-2
3 Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on 3 Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
4 Android, iOS, and web. 4 Android, iOS, and web.
5 -version: 0.2.1+1 5 +version: 0.3.0-nullsafety.0
6 homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock 6 homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock
7 7
8 environment: 8 environment:
9 - sdk: ">=2.8.0 <3.0.0"  
10 - flutter: ">=1.20.0 <2.0.0" 9 + sdk: '>=2.12.0-0 <3.0.0'
  10 + flutter: '>=1.24.0-0 <2.0.0'
11 11
12 dependencies: 12 dependencies:
13 flutter: 13 flutter:
@@ -15,15 +15,15 @@ dependencies: @@ -15,15 +15,15 @@ dependencies:
15 15
16 meta: ^1.2.0 16 meta: ^1.2.0
17 17
18 - wakelock_platform_interface: ^0.1.0+1  
19 - wakelock_web: ^0.1.0+3 18 + wakelock_platform_interface: ^0.2.0-nullsafety.2
  19 + wakelock_web: ^0.2.0-nullsafety.0
20 20
21 dev_dependencies: 21 dev_dependencies:
22 flutter_test: 22 flutter_test:
23 sdk: flutter 23 sdk: flutter
24 24
25 - pedantic: ^1.9.2  
26 - pigeon: ^0.1.14 25 + pedantic: ^1.10.0-nullsafety.0
  26 + pigeon: ^0.1.15
27 27
28 flutter: 28 flutter:
29 plugin: 29 plugin:
@@ -6,16 +6,12 @@ void main() { @@ -6,16 +6,12 @@ void main() {
6 TestWidgetsFlutterBinding.ensureInitialized(); 6 TestWidgetsFlutterBinding.ensureInitialized();
7 7
8 group('$Wakelock', () { 8 group('$Wakelock', () {
9 - FakeWakelockApi fakeWakelock; 9 + late FakeWakelockApi fakeWakelock;
10 10
11 setUpAll(() { 11 setUpAll(() {
12 fakeWakelock = FakeWakelockApi(); 12 fakeWakelock = FakeWakelockApi();
13 }); 13 });
14 14
15 - test('toggle enable non-null assertion', () {  
16 - expect(() => Wakelock.toggle(enable: null), throwsAssertionError);  
17 - });  
18 -  
19 test('enable', () async { 15 test('enable', () async {
20 await Wakelock.enable(); 16 await Wakelock.enable();
21 17
@@ -55,7 +51,7 @@ class FakeWakelockApi extends TestWakelockApi { @@ -55,7 +51,7 @@ class FakeWakelockApi extends TestWakelockApi {
55 } 51 }
56 52
57 final calls = <String>[]; 53 final calls = <String>[];
58 - ToggleMessage toggleMessage; 54 + late ToggleMessage toggleMessage;
59 55
60 @override 56 @override
61 IsEnabledMessage isEnabled() { 57 IsEnabledMessage isEnabled() {
  1 +## 0.2.0-nullsafety.0
  2 +
  3 +* Migrated to null safety.
  4 +* Temporarily removed `import_js_library` dependency and included code internally.
  5 +
1 ## 0.1.0+3 6 ## 0.1.0+3
2 7
3 * Bumped minimum `import_js_library` version in order to resolve Android build warning. 8 * Bumped minimum `import_js_library` version in order to resolve Android build warning.
  1 +/// Function with a single typed argument.
1 typedef Func1<A, R> = R Function(A a); 2 typedef Func1<A, R> = R Function(A a);
  1 +import 'dart:html' as html;
  2 +
  3 +/// This is an implementation of the `import_js_library` plugin that is used
  4 +/// until that plugin is migrated to null safety.
  5 +/// See https://github.com/florent37/flutter_web_import_js_library/pull/6#issuecomment-735349208.
  6 +
  7 +/// Imports a JS script file from the given [url] given the relative
  8 +/// [flutterPluginName].
  9 +void importJsLibrary({required String url, String? flutterPluginName}) {
  10 + if (flutterPluginName == null) {
  11 + _importJSLibraries([url]);
  12 + } else {
  13 + _importJSLibraries([_libraryUrl(url, flutterPluginName)]);
  14 + }
  15 +}
  16 +
  17 +String _libraryUrl(String url, String pluginName) {
  18 + if (url.startsWith('./')) {
  19 + url = url.replaceFirst('./', '');
  20 + return './assets/packages/$pluginName/$url';
  21 + }
  22 + if (url.startsWith('assets/')) {
  23 + return './assets/packages/$pluginName/$url';
  24 + } else {
  25 + return url;
  26 + }
  27 +}
  28 +
  29 +html.ScriptElement _createScriptTag(String library) {
  30 + final script = html.ScriptElement()
  31 + ..type = 'text/javascript'
  32 + ..charset = 'utf-8'
  33 + ..async = true
  34 + ..src = library;
  35 + return script;
  36 +}
  37 +
  38 +/// Injects a bunch of libraries in the <head> and returns a
  39 +/// Future that resolves when all load.
  40 +Future<void> _importJSLibraries(List<String> libraries) {
  41 + final loading = <Future<void>>[];
  42 + final head = html.querySelector('head');
  43 +
  44 + libraries.forEach((String library) {
  45 + if (!_isImported(library)) {
  46 + final scriptTag = _createScriptTag(library);
  47 + head!.children.add(scriptTag);
  48 + loading.add(scriptTag.onLoad.first);
  49 + }
  50 + });
  51 +
  52 + return Future.wait(loading);
  53 +}
  54 +
  55 +bool _isImported(String url) {
  56 + final head = html.querySelector('head')!;
  57 + return _isLoaded(head, url);
  58 +}
  59 +
  60 +bool _isLoaded(html.Element head, String url) {
  61 + if (url.startsWith('./')) {
  62 + url = url.replaceFirst('./', '');
  63 + }
  64 + for (var element in head.children) {
  65 + if (element is html.ScriptElement) {
  66 + if (element.src.endsWith(url)) {
  67 + return true;
  68 + }
  69 + }
  70 + }
  71 + return false;
  72 +}
@@ -5,7 +5,7 @@ import 'package:js/js.dart'; @@ -5,7 +5,7 @@ import 'package:js/js.dart';
5 import 'package:wakelock_web/src/promise.dart'; 5 import 'package:wakelock_web/src/promise.dart';
6 6
7 /// Toggles the JS wakelock. 7 /// Toggles the JS wakelock.
8 -external toggle(bool enable); 8 +external void toggle(bool enable);
9 9
10 /// Returns a JS promise of whether the wakelock is enabled or not. 10 /// Returns a JS promise of whether the wakelock is enabled or not.
11 external PromiseJsImpl<bool> enabled(); 11 external PromiseJsImpl<bool> enabled();
@@ -11,5 +11,5 @@ class PromiseJsImpl<T> { @@ -11,5 +11,5 @@ class PromiseJsImpl<T> {
11 external PromiseJsImpl(Function resolver); 11 external PromiseJsImpl(Function resolver);
12 12
13 /// Attaches callbacks to a JS promise. 13 /// Attaches callbacks to a JS promise.
14 - external PromiseJsImpl then([Func1 onResolve, Func1 onReject]); 14 + external PromiseJsImpl then([Func1? onResolve, Func1? onReject]);
15 } 15 }
1 import 'dart:async'; 1 import 'dart:async';
2 2
3 import 'package:flutter_web_plugins/flutter_web_plugins.dart'; 3 import 'package:flutter_web_plugins/flutter_web_plugins.dart';
4 -import 'package:import_js_library/import_js_library.dart';  
5 import 'package:js/js.dart'; 4 import 'package:js/js.dart';
6 import 'package:wakelock_platform_interface/wakelock_platform_interface.dart'; 5 import 'package:wakelock_platform_interface/wakelock_platform_interface.dart';
7 -import 'package:wakelock_web/src/js_wakelock.dart' as Wakelock; 6 +import 'package:wakelock_web/src/import_js_library.dart';
  7 +import 'package:wakelock_web/src/js_wakelock.dart' as wakelock_web;
8 8
9 /// The web implementation of the [WakelockPlatformInterface]. 9 /// The web implementation of the [WakelockPlatformInterface].
10 /// 10 ///
@@ -22,17 +22,15 @@ class WakelockWeb extends WakelockPlatformInterface { @@ -22,17 +22,15 @@ class WakelockWeb extends WakelockPlatformInterface {
22 } 22 }
23 23
24 @override 24 @override
25 - Future<void> toggle({bool enable}) async {  
26 - assert(enable != null);  
27 -  
28 - Wakelock.toggle(enable); 25 + Future<void> toggle({required bool enable}) async {
  26 + wakelock_web.toggle(enable);
29 } 27 }
30 28
31 @override 29 @override
32 Future<bool> get enabled async { 30 Future<bool> get enabled async {
33 final completer = Completer<bool>(); 31 final completer = Completer<bool>();
34 32
35 - Wakelock.enabled().then( 33 + wakelock_web.enabled().then(
36 // onResolve 34 // onResolve
37 allowInterop((value) { 35 allowInterop((value) {
38 assert(value is bool); 36 assert(value is bool);
1 name: wakelock_web 1 name: wakelock_web
2 description: Web platform implementation of the wakelock_platform_interface for the wakelock plugin. 2 description: Web platform implementation of the wakelock_platform_interface for the wakelock plugin.
3 -version: 0.1.0+3 3 +version: 0.2.0-nullsafety.0
4 homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock_web 4 homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock_web
5 5
6 environment: 6 environment:
7 - sdk: ">=2.8.0 <3.0.0"  
8 - flutter: ">=1.20.0 <2.0.0" 7 + sdk: '>=2.12.0-0 <3.0.0'
  8 + flutter: '>=1.24.0-0 <2.0.0'
9 9
10 dependencies: 10 dependencies:
11 flutter: 11 flutter:
@@ -13,16 +13,15 @@ dependencies: @@ -13,16 +13,15 @@ dependencies:
13 flutter_web_plugins: 13 flutter_web_plugins:
14 sdk: flutter 14 sdk: flutter
15 15
16 - import_js_library: ^1.0.2  
17 - js: ^0.6.2 16 + js: ^0.6.3-nullsafety.0
18 17
19 - wakelock_platform_interface: ^0.1.0 18 + wakelock_platform_interface: ^0.2.0-nullsafety.2
20 19
21 dev_dependencies: 20 dev_dependencies:
22 flutter_test: 21 flutter_test:
23 sdk: flutter 22 sdk: flutter
24 23
25 - pedantic: ^1.9.2 24 + pedantic: ^1.10.0-nullsafety.0
26 25
27 wakelock: 26 wakelock:
28 path: ../wakelock 27 path: ../wakelock