diff --git a/pkgs/dartpad_ui/lib/editor/editor.dart b/pkgs/dartpad_ui/lib/editor/editor.dart index ad15708d3..827583736 100644 --- a/pkgs/dartpad_ui/lib/editor/editor.dart +++ b/pkgs/dartpad_ui/lib/editor/editor.dart @@ -183,8 +183,7 @@ class _EditorWidgetState extends State implements EditorService { } Timer? _autosaveTimer; - void _autosave([Timer? _]) { - // (Callbacks to [Timer.periodic] must accept a Timer argument) + void _autosave([Timer? timer]) { final content = widget.appModel.sourceCodeController.text; if (content.isEmpty) return; LocalStorage.instance.saveUserCode(content); diff --git a/pkgs/dartpad_ui/lib/local_storage.dart b/pkgs/dartpad_ui/lib/local_storage.dart index 5445879db..3fc58e03f 100644 --- a/pkgs/dartpad_ui/lib/local_storage.dart +++ b/pkgs/dartpad_ui/lib/local_storage.dart @@ -2,18 +2,12 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:web/web.dart' as web; +import 'local_storage/stub.dart' + if (dart.library.js_util) 'local_storage/web.dart'; -import '../utils.dart'; +abstract class LocalStorage { + static LocalStorage instance = LocalStorageImpl(); -const _userInputKey = 'user_'; - -class LocalStorage { - static final instance = LocalStorage(); - - void saveUserCode(String code) => - web.window.localStorage.setItem(_userInputKey, code); - - String? getUserCode() => - web.window.localStorage.getItem(_userInputKey)?.nullIfEmpty; + void saveUserCode(String code); + String? getUserCode(); } diff --git a/pkgs/dartpad_ui/lib/local_storage/stub.dart b/pkgs/dartpad_ui/lib/local_storage/stub.dart new file mode 100644 index 000000000..72416d6c0 --- /dev/null +++ b/pkgs/dartpad_ui/lib/local_storage/stub.dart @@ -0,0 +1,16 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import '../local_storage.dart'; +import '../utils.dart'; + +class LocalStorageImpl extends LocalStorage { + String? _code; + + @override + void saveUserCode(String code) => _code = code; + + @override + String? getUserCode() => _code?.nullIfEmpty; +} diff --git a/pkgs/dartpad_ui/lib/local_storage/web.dart b/pkgs/dartpad_ui/lib/local_storage/web.dart new file mode 100644 index 000000000..e10088ba4 --- /dev/null +++ b/pkgs/dartpad_ui/lib/local_storage/web.dart @@ -0,0 +1,20 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:web/web.dart' as web; + +import '../local_storage.dart'; +import '../utils.dart'; + +const _userInputKey = 'user_'; + +class LocalStorageImpl extends LocalStorage { + @override + void saveUserCode(String code) => + web.window.localStorage.setItem(_userInputKey, code); + + @override + String? getUserCode() => + web.window.localStorage.getItem(_userInputKey)?.nullIfEmpty; +} diff --git a/pkgs/dartpad_ui/test/autosave_test.dart b/pkgs/dartpad_ui/test/autosave_test.dart index 64f77dece..28d23c450 100644 --- a/pkgs/dartpad_ui/test/autosave_test.dart +++ b/pkgs/dartpad_ui/test/autosave_test.dart @@ -17,7 +17,7 @@ Never throwingFallback() => void main() { const channel = Channel.stable; - group('Autosave', () { + group('Autosave:', () { test('empty content is treated as null', () { expect(''.nullIfEmpty, isNull);