This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[shared_preferences_linux] Add support for Linux #2836
Merged
stuartmorgan-g
merged 13 commits into
flutter:master
from
robert-ancell:linux-shared-preferences
Jun 30, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
354a192
[shared_preferences_linux] Add support for Linux
robert-ancell dfafad1
Merge branch 'master' into linux-shared-preferences
robert-ancell d7891c7
Fix typo
robert-ancell dbe291a
Bump version number
robert-ancell 1fc3c47
Don't include shared_preferences_linux in shared_preferences - this w…
robert-ancell 7efb1af
Access SharedPreferencesLinux as a singleton
robert-ancell f6569ab
Update example to use shared_preferences_linux directly
robert-ancell 92eeeac
Make example and docs not refer to shared_preferences - this package …
robert-ancell 8d6e3d8
Merge branch 'master' into linux-shared-preferences
robert-ancell e3b0b75
Fix tests
robert-ancell 5cefe99
Reduce version of path provided
robert-ancell aed9321
try depending on the current version of path_provider
robert-ancell 816d7a2
Don't build shared_preferences_linux - the other ones aren't built
robert-ancell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/shared_preferences/shared_preferences_linux/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.DS_Store | ||
.dart_tool/ | ||
|
||
.packages | ||
.pub/ | ||
|
||
build/ |
10 changes: 10 additions & 0 deletions
10
packages/shared_preferences/shared_preferences_linux/.metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
2 changes: 2 additions & 0 deletions
2
packages/shared_preferences/shared_preferences_linux/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## 0.0.1 | ||
* Initial release to support shared_preferences on Linux. |
27 changes: 27 additions & 0 deletions
27
packages/shared_preferences/shared_preferences_linux/LICENSE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
22 changes: 22 additions & 0 deletions
22
packages/shared_preferences/shared_preferences_linux/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# shared_preferences_linux | ||
|
||
The Linux implementation of [`shared_preferences`][1]. | ||
|
||
## Usage | ||
|
||
### Import the package | ||
|
||
This package is an unendorsed Linux implementation of `shared_preferences`. | ||
|
||
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_linux: ^0.0.1 | ||
... | ||
``` | ||
|
||
[1]: ../ |
43 changes: 43 additions & 0 deletions
43
packages/shared_preferences/shared_preferences_linux/example/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
10 changes: 10 additions & 0 deletions
10
packages/shared_preferences/shared_preferences_linux/example/.metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
8 changes: 8 additions & 0 deletions
8
packages/shared_preferences/shared_preferences_linux/example/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/). |
87 changes: 87 additions & 0 deletions
87
packages/shared_preferences/shared_preferences_linux/example/lib/main.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_linux/shared_preferences_linux.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<SharedPreferencesDemo> { | ||
final prefs = SharedPreferencesLinux.instance; | ||
Future<int> _counter; | ||
|
||
Future<void> _incrementCounter() async { | ||
final values = await prefs.getAll(); | ||
final int counter = (values['counter'] as int ?? 0) + 1; | ||
|
||
setState(() { | ||
_counter = prefs.setValue(null, "counter", counter).then((bool success) { | ||
return counter; | ||
}); | ||
}); | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_counter = prefs.getAll().then((Map<String, Object> values) { | ||
return (values['counter'] ?? 0); | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("SharedPreferences Demo"), | ||
), | ||
body: Center( | ||
child: FutureBuilder<int>( | ||
future: _counter, | ||
builder: (BuildContext context, AsyncSnapshot<int> 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), | ||
), | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: shared_preferences_linux_example | ||
description: Demonstrates how to use the shared_preferences_linux plugin. | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
shared_preferences_linux: ^0.1.0 | ||
|
||
dependency_overrides: | ||
shared_preferences_linux: | ||
path: ../ | ||
|
||
flutter: | ||
uses-material-design: true | ||
|
96 changes: 96 additions & 0 deletions
96
packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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 { | ||
/// The default instance of [SharedPreferencesLinux] to use. | ||
static SharedPreferencesLinux instance = SharedPreferencesLinux(); | ||
|
||
/// Local copy of preferences | ||
Map<String, Object> _cachedPreferences; | ||
|
||
/// File system used to store to disk. Exposed for testing only. | ||
@visibleForTesting | ||
FileSystem fs = LocalFileSystem(); | ||
|
||
/// Gets the file where the preferences are stored. | ||
Future<File> _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<Map<String, Object>> _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<String, Object>; | ||
} | ||
} | ||
|
||
return _cachedPreferences; | ||
} | ||
|
||
/// Writes the cached preferences to disk. Returns [true] if the operation | ||
/// succeeded. | ||
Future<bool> _writePreferences(Map<String, Object> 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<bool> clear() async { | ||
var preferences = await _readPreferences(); | ||
preferences.clear(); | ||
return _writePreferences(preferences); | ||
} | ||
|
||
@override | ||
Future<Map<String, Object>> getAll() async { | ||
return _readPreferences(); | ||
} | ||
|
||
@override | ||
Future<bool> remove(String key) async { | ||
var preferences = await _readPreferences(); | ||
preferences.remove(key); | ||
return _writePreferences(preferences); | ||
} | ||
|
||
@override | ||
Future<bool> setValue(String valueType, String key, Object value) async { | ||
var preferences = await _readPreferences(); | ||
preferences[key] = value; | ||
return _writePreferences(preferences); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/shared_preferences/shared_preferences_linux/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.6.4 | ||
path_provider: ^1.6.11 | ||
shared_preferences_platform_interface: ^1.0.0 | ||
|
||
dev_dependencies: | ||
flutter_test: | ||
sdk: flutter | ||
pedantic: ^1.8.0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.