From 354a192d879ecb992f6e0f4633065da99353a793 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Tue, 16 Jun 2020 11:32:09 +1200 Subject: [PATCH 01/11] [shared_preferences_linux] Add support for Linux Fixes https://github.com/flutter/flutter/issues/41720 --- .../shared_preferences/CHANGELOG.md | 4 + .../lib/shared_preferences.dart | 13 ++- .../shared_preferences/pubspec.yaml | 5 +- .../shared_preferences_linux/.gitignore | 7 ++ .../shared_preferences_linux/.metadata | 10 ++ .../shared_preferences_linux/CHANGELOG.md | 2 + .../shared_preferences_linux/LICENSE | 27 ++++++ .../shared_preferences_linux/README.md | 23 +++++ .../example/.gitignore | 43 +++++++++ .../example/.metadata | 10 ++ .../example/README.md | 8 ++ .../example/lib/main.dart | 87 +++++++++++++++++ .../example/pubspec.yaml | 14 +++ .../lib/shared_preferences_linux.dart | 93 +++++++++++++++++++ .../shared_preferences_linux/pubspec.yaml | 29 ++++++ .../test/shared_preferences_linux_test.dart | 72 ++++++++++++++ 16 files changed, 444 insertions(+), 3 deletions(-) create mode 100644 packages/shared_preferences/shared_preferences_linux/.gitignore create mode 100644 packages/shared_preferences/shared_preferences_linux/.metadata create mode 100644 packages/shared_preferences/shared_preferences_linux/CHANGELOG.md create mode 100644 packages/shared_preferences/shared_preferences_linux/LICENSE create mode 100644 packages/shared_preferences/shared_preferences_linux/README.md create mode 100644 packages/shared_preferences/shared_preferences_linux/example/.gitignore create mode 100644 packages/shared_preferences/shared_preferences_linux/example/.metadata create mode 100644 packages/shared_preferences/shared_preferences_linux/example/README.md create mode 100644 packages/shared_preferences/shared_preferences_linux/example/lib/main.dart create mode 100644 packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml create mode 100644 packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart create mode 100644 packages/shared_preferences/shared_preferences_linux/pubspec.yaml create mode 100644 packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart diff --git a/packages/shared_preferences/shared_preferences/CHANGELOG.md b/packages/shared_preferences/shared_preferences/CHANGELOG.md index 8c7ee24dfa6b..72962a08859a 100644 --- a/packages/shared_preferences/shared_preferences/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.8 + +* Adds support for Linux. + ## 0.5.7+3 * Post-v2 Android embedding cleanup. diff --git a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart index 62160dee20fd..f0147cab6bca 100644 --- a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart +++ b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart @@ -4,8 +4,11 @@ import 'dart:async'; +import 'package:flutter/foundation.dart'; import 'package:meta/meta.dart'; +import 'package:platform/platform.dart'; +import 'package:shared_preferences_linux/shared_preferences_linux.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; /// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing @@ -18,8 +21,14 @@ class SharedPreferences { static const String _prefix = 'flutter.'; static Completer _completer; - static SharedPreferencesStorePlatform get _store => - SharedPreferencesStorePlatform.instance; + static SharedPreferencesStorePlatform get _store { + // This is a workaround until https://github.com/flutter/flutter/issues/52267 + // is fixed. There is no way currently to register a Dart-only plugin. + if (!kIsWeb && LocalPlatform().isLinux) { + return SharedPreferencesLinux(); + } + return SharedPreferencesStorePlatform.instance; + } /// Loads and parses the [SharedPreferences] for this app from disk. /// diff --git a/packages/shared_preferences/shared_preferences/pubspec.yaml b/packages/shared_preferences/shared_preferences/pubspec.yaml index 2b278248745a..b6806aba76cf 100644 --- a/packages/shared_preferences/shared_preferences/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/shared_prefere # 0.5.y+z is compatible with 1.0.0, if you land a breaking change bump # the version to 2.0.0. # See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0 -version: 0.5.7+3 +version: 0.5.8 flutter: plugin: @@ -24,6 +24,9 @@ dependencies: meta: ^1.0.4 flutter: sdk: flutter + platform: 2.2.1 + shared_preferences_linux: + path: ../shared_preferences_linux shared_preferences_platform_interface: ^1.0.0 # The design on https://flutter.dev/go/federated-plugins was to leave # this constraint as "any". We cannot do it right now as it fails pub publish diff --git a/packages/shared_preferences/shared_preferences_linux/.gitignore b/packages/shared_preferences/shared_preferences_linux/.gitignore new file mode 100644 index 000000000000..e9dc58d3d6e2 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +.dart_tool/ + +.packages +.pub/ + +build/ diff --git a/packages/shared_preferences/shared_preferences_linux/.metadata b/packages/shared_preferences/shared_preferences_linux/.metadata new file mode 100644 index 000000000000..9615744e96d1 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: e491544588e8d34fdf31d5f840b4649850ef167a + channel: master + +project_type: plugin diff --git a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md new file mode 100644 index 000000000000..11694802aacf --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md @@ -0,0 +1,2 @@ +## 0.0.1 +* Initial release to support shared_preferences on Linux. diff --git a/packages/shared_preferences/shared_preferences_linux/LICENSE b/packages/shared_preferences/shared_preferences_linux/LICENSE new file mode 100644 index 000000000000..0c91662b3f2f --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/LICENSE @@ -0,0 +1,27 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/shared_preferences/shared_preferences_linux/README.md b/packages/shared_preferences/shared_preferences_linux/README.md new file mode 100644 index 000000000000..b348b9fd677f --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/README.md @@ -0,0 +1,23 @@ +# shared_preferences_linux + +The Linux implementation of [`shared_preferences`][1]. + +## Usage + +### Import the package + +This package has been endorsed, meaning that you only need to add `shared_preferences` +as a dependency in your `pubspec.yaml`. It will be automatically included in your app +when you depend on `package:shared_preferences`. + +This is what the above means to your `pubspec.yaml`: + +```yaml +... +dependencies: + ... + shared_preferences: ^0.5.8 + ... +``` + +[1]: ../ diff --git a/packages/shared_preferences/shared_preferences_linux/example/.gitignore b/packages/shared_preferences/shared_preferences_linux/example/.gitignore new file mode 100644 index 000000000000..1ba9c339effb --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/.gitignore @@ -0,0 +1,43 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.packages +.pub-cache/ +.pub/ +/build/ + +# Web related +lib/generated_plugin_registrant.dart + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json + +# Exceptions to above rules. +!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages diff --git a/packages/shared_preferences/shared_preferences_linux/example/.metadata b/packages/shared_preferences/shared_preferences_linux/example/.metadata new file mode 100644 index 000000000000..c0bc9a90268a --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: e491544588e8d34fdf31d5f840b4649850ef167a + channel: master + +project_type: app diff --git a/packages/shared_preferences/shared_preferences_linux/example/README.md b/packages/shared_preferences/shared_preferences_linux/example/README.md new file mode 100644 index 000000000000..9d3bf1faf406 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/README.md @@ -0,0 +1,8 @@ +# shared_preferences_example + +Demonstrates how to use the shared_preferences plugin. + +## Getting Started + +For help getting started with Flutter, view our online +[documentation](http://flutter.io/). diff --git a/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart new file mode 100644 index 000000000000..46daeff6706f --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart @@ -0,0 +1,87 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ignore_for_file: public_member_api_docs + +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +void main() { + runApp(MyApp()); +} + +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'SharedPreferences Demo', + home: SharedPreferencesDemo(), + ); + } +} + +class SharedPreferencesDemo extends StatefulWidget { + SharedPreferencesDemo({Key key}) : super(key: key); + + @override + SharedPreferencesDemoState createState() => SharedPreferencesDemoState(); +} + +class SharedPreferencesDemoState extends State { + Future _prefs = SharedPreferences.getInstance(); + Future _counter; + + Future _incrementCounter() async { + final SharedPreferences prefs = await _prefs; + final int counter = (prefs.getInt('counter') ?? 0) + 1; + + setState(() { + _counter = prefs.setInt("counter", counter).then((bool success) { + return counter; + }); + }); + } + + @override + void initState() { + super.initState(); + _counter = _prefs.then((SharedPreferences prefs) { + return (prefs.getInt('counter') ?? 0); + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("SharedPreferences Demo"), + ), + body: Center( + child: FutureBuilder( + future: _counter, + builder: (BuildContext context, AsyncSnapshot snapshot) { + switch (snapshot.connectionState) { + case ConnectionState.waiting: + return const CircularProgressIndicator(); + default: + if (snapshot.hasError) { + return Text('Error: ${snapshot.error}'); + } else { + return Text( + 'Button tapped ${snapshot.data} time${snapshot.data == 1 ? '' : 's'}.\n\n' + 'This should persist across restarts.', + ); + } + } + })), + floatingActionButton: FloatingActionButton( + onPressed: _incrementCounter, + tooltip: 'Increment', + child: const Icon(Icons.add), + ), + ); + } +} diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml new file mode 100644 index 000000000000..9073e654d790 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml @@ -0,0 +1,14 @@ +name: shared_preferences_example +description: Demonstrates how to use the shared_preferences plugin. + +dependencies: + flutter: + sdk: flutter + shared_preferences: + path: ../../shared_preferences/ + shared_preferences_linux: + path: ../ + +flutter: + uses-material-design: true + diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart new file mode 100644 index 000000000000..5d407eeb0cc2 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -0,0 +1,93 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:convert' show json; +import 'package:file/file.dart'; +import 'package:file/local.dart'; +import 'package:meta/meta.dart'; +import 'package:path/path.dart' as path; +import 'package:path_provider/path_provider.dart'; +import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; + +/// The Linux implementation of [SharedPreferencesStorePlatform]. +/// +/// This class implements the `package:shared_preferences` functionality for Linux. +class SharedPreferencesLinux extends SharedPreferencesStorePlatform { + /// Local copy of preferences + Map _cachedPreferences; + + /// File system used to store to disk. Exposed for testing only. + @visibleForTesting + FileSystem fs = LocalFileSystem(); + + /// Gets the file were the preferences are stored. + Future _getLocalDataFile() async { + var directory = await getApplicationSupportDirectory(); + var filePath = path.join(directory.path, 'shared_preferences.json'); + return fs.file(filePath); + } + + /// Gets the preferences from the stored file. Once read, the preferences are + /// maintained in memory. + Future> _readPreferences() async { + if (_cachedPreferences != null) { + return _cachedPreferences; + } + + _cachedPreferences = {}; + var localDataFile = await _getLocalDataFile(); + if (localDataFile.existsSync()) { + String stringMap = localDataFile.readAsStringSync(); + if (stringMap.isNotEmpty) { + _cachedPreferences = json.decode(stringMap) as Map; + } + } + + return _cachedPreferences; + } + + /// Writes the cached preferences to disk. Returns [true] if the operation + /// succeeded. + Future _writePreferences(Map preferences) async { + try { + var localDataFile = await _getLocalDataFile(); + if (!localDataFile.existsSync()) { + localDataFile.createSync(recursive: true); + } + var stringMap = json.encode(preferences); + localDataFile.writeAsStringSync(stringMap); + } catch (e) { + print("Error saving preferences to disk: $e"); + return false; + } + return true; + } + + @override + Future clear() async { + var preferences = await _readPreferences(); + preferences.clear(); + return _writePreferences(preferences); + } + + @override + Future> getAll() async { + return _readPreferences(); + } + + @override + Future remove(String key) async { + var preferences = await _readPreferences(); + preferences.remove(key); + return _writePreferences(preferences); + } + + @override + Future setValue(String valueType, String key, Object value) async { + var preferences = await _readPreferences(); + preferences[key] = value; + return _writePreferences(preferences); + } +} diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml new file mode 100644 index 000000000000..2b82c12eba0d --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -0,0 +1,29 @@ +name: shared_preferences_linux +description: Linux implementation of the shared_preferences plugin +version: 0.0.1 +homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_linux + +flutter: + plugin: + platforms: + linux: + dartPluginClass: SharedPreferencesLinux + pluginClass: none + +environment: + sdk: ">=2.1.0 <3.0.0" + flutter: ">=1.12.8 <2.0.0" + +dependencies: + file: ^5.1.0 + flutter: + sdk: flutter + meta: ^1.0.4 + path: ^1.7.0 + path_provider: ^1.6.10 + shared_preferences_platform_interface: ^1.0.0 + +dev_dependencies: + flutter_test: + sdk: flutter + pedantic: ^1.8.0 diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart new file mode 100644 index 000000000000..d82ddcbb2a7e --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart @@ -0,0 +1,72 @@ +// Copyright 2020 The Chromium Authors. 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:file/memory.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:path/path.dart' as path; +import 'package:path_provider/path_provider.dart'; +import 'package:shared_preferences_linux/shared_preferences_linux.dart'; + +MemoryFileSystem fs = MemoryFileSystem.test(); + +void main() { + setUp(() {}); + + tearDown(() {}); + + Future _getFilePath() async { + var directory = await getApplicationSupportDirectory(); + return path.join(directory.path, 'shared_preferences.json'); + } + + _writeTestFile(String value) async { + await fs.file(await _getFilePath()).writeAsString(value); + } + + Future _readTestFile() async { + return await fs.file(await _getFilePath()).readAsString(); + } + + SharedPreferencesLinux _getPreferences() { + var prefs = SharedPreferencesLinux(); + prefs.fs = fs; + return prefs; + } + + test('getAll', () async { + await _writeTestFile('{"key1": "one", "key2", 2}'); + var prefs = _getPreferences(); + + var values = await prefs.getAll(); + expect(values, hasLength(2)); + expect(values['key1'], 'one'); + expect(values['key2'], 2); + }); + + test('remove', () async { + await _writeTestFile('{"key1": "one", "key2", 2}'); + var prefs = _getPreferences(); + + await prefs.remove('key2'); + + expect(_readTestFile(), '{"key1": "one"}'); + }); + + test('setValue', () async { + await _writeTestFile('{}'); + var prefs = _getPreferences(); + + await prefs.setValue('', 'key1', 'one'); + await prefs.setValue('', 'key2', 2); + + expect(await _readTestFile(), '{"key1": "one", "key2", 2}'); + }); + + test('clear', () async { + await _writeTestFile('{"key1": "one", "key2", 2}'); + var prefs = _getPreferences(); + + await prefs.clear(); + expect(_readTestFile(), '{}'); + }); +} From d7891c7a7d97d0726ce073669b62bc0af071ae75 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 24 Jun 2020 15:44:23 +1200 Subject: [PATCH 02/11] Fix typo --- .../shared_preferences_linux/lib/shared_preferences_linux.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index 5d407eeb0cc2..978de17510a7 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -22,7 +22,7 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { @visibleForTesting FileSystem fs = LocalFileSystem(); - /// Gets the file were the preferences are stored. + /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { var directory = await getApplicationSupportDirectory(); var filePath = path.join(directory.path, 'shared_preferences.json'); From dbe291abaf722f2fd1f6532707c22442f9b892c9 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 24 Jun 2020 15:45:31 +1200 Subject: [PATCH 03/11] Bump version number --- packages/shared_preferences/shared_preferences/CHANGELOG.md | 2 +- packages/shared_preferences/shared_preferences/pubspec.yaml | 2 +- packages/shared_preferences/shared_preferences_linux/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/shared_preferences/shared_preferences/CHANGELOG.md b/packages/shared_preferences/shared_preferences/CHANGELOG.md index 72962a08859a..83e14c335ba4 100644 --- a/packages/shared_preferences/shared_preferences/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.5.8 +## 0.6.0 * Adds support for Linux. diff --git a/packages/shared_preferences/shared_preferences/pubspec.yaml b/packages/shared_preferences/shared_preferences/pubspec.yaml index b6806aba76cf..9350503228b9 100644 --- a/packages/shared_preferences/shared_preferences/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/shared_prefere # 0.5.y+z is compatible with 1.0.0, if you land a breaking change bump # the version to 2.0.0. # See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0 -version: 0.5.8 +version: 0.6.0 flutter: plugin: diff --git a/packages/shared_preferences/shared_preferences_linux/README.md b/packages/shared_preferences/shared_preferences_linux/README.md index b348b9fd677f..5427e803127c 100644 --- a/packages/shared_preferences/shared_preferences_linux/README.md +++ b/packages/shared_preferences/shared_preferences_linux/README.md @@ -16,7 +16,7 @@ This is what the above means to your `pubspec.yaml`: ... dependencies: ... - shared_preferences: ^0.5.8 + shared_preferences: ^0.6.0 ... ``` From 1fc3c4762a4b93c7b43c7a8de6b261eb86414f21 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 24 Jun 2020 15:49:49 +1200 Subject: [PATCH 04/11] Don't include shared_preferences_linux in shared_preferences - this will be done once shared_preferences_linux is published --- .../shared_preferences/CHANGELOG.md | 4 ---- .../shared_preferences/lib/shared_preferences.dart | 13 ++----------- .../shared_preferences/pubspec.yaml | 5 +---- 3 files changed, 3 insertions(+), 19 deletions(-) diff --git a/packages/shared_preferences/shared_preferences/CHANGELOG.md b/packages/shared_preferences/shared_preferences/CHANGELOG.md index 83e14c335ba4..8c7ee24dfa6b 100644 --- a/packages/shared_preferences/shared_preferences/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences/CHANGELOG.md @@ -1,7 +1,3 @@ -## 0.6.0 - -* Adds support for Linux. - ## 0.5.7+3 * Post-v2 Android embedding cleanup. diff --git a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart index f0147cab6bca..62160dee20fd 100644 --- a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart +++ b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart @@ -4,11 +4,8 @@ import 'dart:async'; -import 'package:flutter/foundation.dart'; import 'package:meta/meta.dart'; -import 'package:platform/platform.dart'; -import 'package:shared_preferences_linux/shared_preferences_linux.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; /// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing @@ -21,14 +18,8 @@ class SharedPreferences { static const String _prefix = 'flutter.'; static Completer _completer; - static SharedPreferencesStorePlatform get _store { - // This is a workaround until https://github.com/flutter/flutter/issues/52267 - // is fixed. There is no way currently to register a Dart-only plugin. - if (!kIsWeb && LocalPlatform().isLinux) { - return SharedPreferencesLinux(); - } - return SharedPreferencesStorePlatform.instance; - } + static SharedPreferencesStorePlatform get _store => + SharedPreferencesStorePlatform.instance; /// Loads and parses the [SharedPreferences] for this app from disk. /// diff --git a/packages/shared_preferences/shared_preferences/pubspec.yaml b/packages/shared_preferences/shared_preferences/pubspec.yaml index 9350503228b9..2b278248745a 100644 --- a/packages/shared_preferences/shared_preferences/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/shared_prefere # 0.5.y+z is compatible with 1.0.0, if you land a breaking change bump # the version to 2.0.0. # See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0 -version: 0.6.0 +version: 0.5.7+3 flutter: plugin: @@ -24,9 +24,6 @@ dependencies: meta: ^1.0.4 flutter: sdk: flutter - platform: 2.2.1 - shared_preferences_linux: - path: ../shared_preferences_linux shared_preferences_platform_interface: ^1.0.0 # The design on https://flutter.dev/go/federated-plugins was to leave # this constraint as "any". We cannot do it right now as it fails pub publish From 7efb1afbb2892c24b47ed15a8e6b4ad73af7e08d Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 24 Jun 2020 16:08:39 +1200 Subject: [PATCH 05/11] Access SharedPreferencesLinux as a singleton --- .../shared_preferences_linux/lib/shared_preferences_linux.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index 978de17510a7..dc93100c3277 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -15,6 +15,9 @@ import 'package:shared_preferences_platform_interface/shared_preferences_platfor /// /// This class implements the `package:shared_preferences` functionality for Linux. class SharedPreferencesLinux extends SharedPreferencesStorePlatform { + /// The default instance of [SharedPreferencesLinux] to use. + static SharedPreferencesLinux instance = SharedPreferencesLinux(); + /// Local copy of preferences Map _cachedPreferences; From f6569abdf723af5710ba9ce5bb744718ba281ef2 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 24 Jun 2020 16:08:51 +1200 Subject: [PATCH 06/11] Update example to use shared_preferences_linux directly --- .../shared_preferences_linux/example/lib/main.dart | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart index 46daeff6706f..ceacf2f95f28 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart +++ b/packages/shared_preferences/shared_preferences_linux/example/lib/main.dart @@ -7,7 +7,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; +import 'package:shared_preferences_linux/shared_preferences_linux.dart'; void main() { runApp(MyApp()); @@ -31,15 +31,15 @@ class SharedPreferencesDemo extends StatefulWidget { } class SharedPreferencesDemoState extends State { - Future _prefs = SharedPreferences.getInstance(); + final prefs = SharedPreferencesLinux.instance; Future _counter; Future _incrementCounter() async { - final SharedPreferences prefs = await _prefs; - final int counter = (prefs.getInt('counter') ?? 0) + 1; + final values = await prefs.getAll(); + final int counter = (values['counter'] as int ?? 0) + 1; setState(() { - _counter = prefs.setInt("counter", counter).then((bool success) { + _counter = prefs.setValue(null, "counter", counter).then((bool success) { return counter; }); }); @@ -48,8 +48,8 @@ class SharedPreferencesDemoState extends State { @override void initState() { super.initState(); - _counter = _prefs.then((SharedPreferences prefs) { - return (prefs.getInt('counter') ?? 0); + _counter = prefs.getAll().then((Map values) { + return (values['counter'] ?? 0); }); } From 92eeeac1fe29cbf35e9c073dbf321ae492385634 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 25 Jun 2020 14:36:15 +1200 Subject: [PATCH 07/11] Make example and docs not refer to shared_preferences - this package is not endorsed yet --- .../shared_preferences_linux/README.md | 9 ++++----- .../shared_preferences_linux/example/pubspec.yaml | 9 +++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/README.md b/packages/shared_preferences/shared_preferences_linux/README.md index 5427e803127c..1894f50ae99e 100644 --- a/packages/shared_preferences/shared_preferences_linux/README.md +++ b/packages/shared_preferences/shared_preferences_linux/README.md @@ -6,17 +6,16 @@ The Linux implementation of [`shared_preferences`][1]. ### Import the package -This package has been endorsed, meaning that you only need to add `shared_preferences` -as a dependency in your `pubspec.yaml`. It will be automatically included in your app -when you depend on `package:shared_preferences`. +This package is an unendorsed Linux implementation of `shared_preferences`. -This is what the above means to your `pubspec.yaml`: +In order to use this now, you'll need to depend on `shared_preferences_linux`. +When this package is endorsed it will be automatically used by the `shared_preferences` package and you can switch to that API. ```yaml ... dependencies: ... - shared_preferences: ^0.6.0 + shared_preferences_linux: ^0.0.1 ... ``` diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml index 9073e654d790..1c0624035c54 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml @@ -1,11 +1,12 @@ -name: shared_preferences_example -description: Demonstrates how to use the shared_preferences plugin. +name: shared_preferences_linux_example +description: Demonstrates how to use the shared_preferences_linux plugin. dependencies: flutter: sdk: flutter - shared_preferences: - path: ../../shared_preferences/ + shared_preferences_linux: ^0.1.0 + +dependency_overrides: shared_preferences_linux: path: ../ From e3b0b7590c4f5a934358f77fa5e1e1efef26801e Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Tue, 30 Jun 2020 15:21:30 +1200 Subject: [PATCH 08/11] Fix tests --- .../test/shared_preferences_linux_test.dart | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart index d82ddcbb2a7e..8a794b1fa7c2 100644 --- a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart +++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart @@ -20,11 +20,13 @@ void main() { } _writeTestFile(String value) async { - await fs.file(await _getFilePath()).writeAsString(value); + fs.file(await _getFilePath()) + ..createSync(recursive: true) + ..writeAsStringSync(value); } Future _readTestFile() async { - return await fs.file(await _getFilePath()).readAsString(); + return fs.file(await _getFilePath()).readAsStringSync(); } SharedPreferencesLinux _getPreferences() { @@ -34,7 +36,7 @@ void main() { } test('getAll', () async { - await _writeTestFile('{"key1": "one", "key2", 2}'); + await _writeTestFile('{"key1": "one", "key2": 2}'); var prefs = _getPreferences(); var values = await prefs.getAll(); @@ -44,12 +46,12 @@ void main() { }); test('remove', () async { - await _writeTestFile('{"key1": "one", "key2", 2}'); + await _writeTestFile('{"key1":"one","key2":2}'); var prefs = _getPreferences(); await prefs.remove('key2'); - expect(_readTestFile(), '{"key1": "one"}'); + expect(await _readTestFile(), '{"key1":"one"}'); }); test('setValue', () async { @@ -59,14 +61,14 @@ void main() { await prefs.setValue('', 'key1', 'one'); await prefs.setValue('', 'key2', 2); - expect(await _readTestFile(), '{"key1": "one", "key2", 2}'); + expect(await _readTestFile(), '{"key1":"one","key2":2}'); }); test('clear', () async { - await _writeTestFile('{"key1": "one", "key2", 2}'); + await _writeTestFile('{"key1":"one","key2":2}'); var prefs = _getPreferences(); await prefs.clear(); - expect(_readTestFile(), '{}'); + expect(await _readTestFile(), '{}'); }); } From 5cefe9972a1f80b859720345bd00294396e27751 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Tue, 30 Jun 2020 15:46:00 +1200 Subject: [PATCH 09/11] Reduce version of path provided --- .../shared_preferences/shared_preferences_linux/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index 2b82c12eba0d..82a6a1517899 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -19,7 +19,7 @@ dependencies: flutter: sdk: flutter meta: ^1.0.4 - path: ^1.7.0 + path: ^1.6.4 path_provider: ^1.6.10 shared_preferences_platform_interface: ^1.0.0 From aed93213add2ed6f5d044985553dee922341a88e Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Tue, 30 Jun 2020 16:29:26 +1200 Subject: [PATCH 10/11] try depending on the current version of path_provider --- .../shared_preferences/shared_preferences_linux/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index 82a6a1517899..2539b937e006 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -20,7 +20,7 @@ dependencies: sdk: flutter meta: ^1.0.4 path: ^1.6.4 - path_provider: ^1.6.10 + path_provider: ^1.6.11 shared_preferences_platform_interface: ^1.0.0 dev_dependencies: From 816d7a27861d9b94cb341a3bc7315a344cfb29d5 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Tue, 30 Jun 2020 16:48:56 +1200 Subject: [PATCH 11/11] Don't build shared_preferences_linux - the other ones aren't built --- script/build_all_plugins_app.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/script/build_all_plugins_app.sh b/script/build_all_plugins_app.sh index 2c0a90693851..262c4ed7b1d3 100755 --- a/script/build_all_plugins_app.sh +++ b/script/build_all_plugins_app.sh @@ -26,6 +26,7 @@ readonly EXCLUDED_PLUGINS_LIST=( "path_provider_platform_interface" "path_provider_web" "plugin_platform_interface" + "shared_preferences_linux" "shared_preferences_macos" "shared_preferences_platform_interface" "shared_preferences_web"