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.7k
add shared_preferences_platform_interface #2291
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
...red_preferences/example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
packages/shared_preferences/example/ios/Runner.xcworkspace/contents.xcworkspacedata
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
packages/shared_preferences/example/shared_preferences_example.iml
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
packages/shared_preferences/CHANGELOG.md → ...eferences/shared_preferences/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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
packages/shared_preferences/shared_preferences_platform_interface/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,4 @@ | ||
| ## 1.0.0 | ||
|
|
||
| * Initial release. Contains the interface and an implementation based on | ||
| method channels. |
27 changes: 27 additions & 0 deletions
27
packages/shared_preferences/shared_preferences_platform_interface/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 2017 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. |
25 changes: 25 additions & 0 deletions
25
packages/shared_preferences/shared_preferences_platform_interface/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,25 @@ | ||
| # shared_preferences_platform_interface | ||
|
|
||
| A common platform interface for the [`shared_preferences`][1] plugin. | ||
|
|
||
| This interface allows platform-specific implementations of the `shared_preferences` | ||
| plugin, as well as the plugin itself, to ensure they are supporting the | ||
| same interface. | ||
|
|
||
| # Usage | ||
|
|
||
| To implement a new platform-specific implementation of `shared_preferences`, extend | ||
| [`SharedPreferencesPlatform`][2] with an implementation that performs the | ||
| platform-specific behavior, and when you register your plugin, set the default | ||
| `SharedPreferencesLoader` by calling the `SharedPreferencesPlatform.loader` setter. | ||
|
|
||
| # Note on breaking changes | ||
|
|
||
| Strongly prefer non-breaking changes (such as adding a method to the interface) | ||
| over breaking changes for this package. | ||
|
|
||
| See https://flutter.dev/go/platform-interface-breaking-changes for a discussion | ||
| on why a less-clean interface is preferable to a breaking change. | ||
|
|
||
| [1]: ../shared_preferences | ||
| [2]: lib/shared_preferences_platform_interface.dart |
56 changes: 56 additions & 0 deletions
56
...ferences/shared_preferences_platform_interface/lib/method_channel_shared_preferences.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,56 @@ | ||
| // 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. | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
|
|
||
| import 'shared_preferences_platform_interface.dart'; | ||
|
|
||
| const MethodChannel _kChannel = | ||
| MethodChannel('plugins.flutter.io/shared_preferences'); | ||
|
|
||
| /// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing | ||
| /// a persistent store for simple data. | ||
| /// | ||
| /// Data is persisted to disk asynchronously. | ||
| class MethodChannelSharedPreferencesStore | ||
| extends SharedPreferencesStorePlatform { | ||
| @override | ||
| Future<bool> remove(String key) { | ||
| return _invokeBoolMethod('remove', <String, dynamic>{ | ||
| 'key': key, | ||
| }); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> setValue(String valueType, String key, Object value) { | ||
| return _invokeBoolMethod('set$valueType', <String, dynamic>{ | ||
| 'key': key, | ||
| 'value': value, | ||
| }); | ||
| } | ||
|
|
||
| Future<bool> _invokeBoolMethod(String method, Map<String, dynamic> params) { | ||
| return _kChannel | ||
| .invokeMethod<bool>(method, params) | ||
| // TODO(yjbanov): I copied this from the original | ||
| // shared_preferences.dart implementation, but I | ||
| // actually do not know why it's necessary to pipe the | ||
| // result through an identity function. | ||
| // | ||
| // Source: https://github.com/flutter/plugins/blob/3a87296a40a2624d200917d58f036baa9fb18df8/packages/shared_preferences/lib/shared_preferences.dart#L134 | ||
| .then<bool>((dynamic result) => result); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> clear() { | ||
| return _kChannel.invokeMethod<bool>('clear'); | ||
| } | ||
|
|
||
| @override | ||
| Future<Map<String, Object>> getAll() { | ||
| return _kChannel.invokeMapMethod<String, Object>('getAll'); | ||
| } | ||
| } | ||
108 changes: 108 additions & 0 deletions
108
...nces/shared_preferences_platform_interface/lib/shared_preferences_platform_interface.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,108 @@ | ||
| // 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. | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| import 'method_channel_shared_preferences.dart'; | ||
|
|
||
| /// The interface that implementations of shared_preferences must implement. | ||
| /// | ||
| /// Platform implementations should extend this class rather than implement it as `shared_preferences` | ||
| /// does not consider newly added methods to be breaking changes. Extending this class | ||
| /// (using `extends`) ensures that the subclass will get the default implementation, while | ||
| /// platform implementations that `implements` this interface will be broken by newly added | ||
| /// [SharedPreferencesStorePlatform] methods. | ||
| abstract class SharedPreferencesStorePlatform { | ||
| /// The default instance of [SharedPreferencesStorePlatform] to use. | ||
| /// | ||
| /// Defaults to [MethodChannelSharedPreferencesStore]. | ||
| static SharedPreferencesStorePlatform get instance => _instance; | ||
|
|
||
| /// Platform-specific plugins should set this with their own platform-specific | ||
| /// class that extends [SharedPreferencesStorePlatform] when they register themselves. | ||
| static set instance(SharedPreferencesStorePlatform value) { | ||
| try { | ||
| instance._verifyProvidesDefaultImplementations(); | ||
| _instance = value; | ||
| } on NoSuchMethodError catch (_) {} | ||
| } | ||
|
|
||
| static SharedPreferencesStorePlatform _instance = | ||
| MethodChannelSharedPreferencesStore(); | ||
|
|
||
| /// Only mock implementations should set this to true. | ||
| /// | ||
| /// Mockito mocks are implementing this class with `implements` which is forbidden for anything | ||
| /// other than mocks (see class docs). This property provides a backdoor for mockito mocks to | ||
| /// skip the verification that the class isn't implemented with `implements`. | ||
| @visibleForTesting | ||
| bool get isMock => false; | ||
|
|
||
| /// Removes the value associated with the [key]. | ||
| Future<bool> remove(String key); | ||
|
|
||
| /// Stores the [value] associated with the [key]. | ||
| /// | ||
| /// The [valueType] must match the type of [value] as follows: | ||
| /// | ||
| /// * Value type "Bool" must be passed if the value is of type `bool`. | ||
| /// * Value type "Double" must be passed if the value is of type `double`. | ||
| /// * Value type "Int" must be passed if the value is of type `int`. | ||
| /// * Value type "String" must be passed if the value is of type `String`. | ||
| /// * Value type "StringList" must be passed if the value is of type `List<String>`. | ||
| Future<bool> setValue(String valueType, String key, Object value); | ||
|
|
||
| /// Removes all keys and values in the store. | ||
| Future<bool> clear(); | ||
|
|
||
| /// Returns all key/value pairs persisted in this store. | ||
| Future<Map<String, Object>> getAll(); | ||
|
|
||
| // This method makes sure that SharedPreferencesStorePlatform isn't implemented with `implements`. | ||
| // | ||
| // See class doc for more details on why implementing this class is forbidden. | ||
| // | ||
| // This private method is called by the instance setter, which fails if the class is | ||
| // implemented with `implements`. | ||
| void _verifyProvidesDefaultImplementations() {} | ||
| } | ||
|
|
||
| /// Stores data in memory. | ||
| /// | ||
| /// Data does not persist across application restarts. This is useful in unit-tests. | ||
| class InMemorySharedPreferencesStore extends SharedPreferencesStorePlatform { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| /// Instantiates an empty in-memory preferences store. | ||
| InMemorySharedPreferencesStore.empty() : _data = <String, Object>{}; | ||
|
|
||
| /// Instantiates an in-memory preferences store containing a copy of [data]. | ||
| InMemorySharedPreferencesStore.withData(Map<String, Object> data) | ||
| : _data = Map<String, Object>.from(data); | ||
|
|
||
| final Map<String, Object> _data; | ||
|
|
||
| @override | ||
| Future<bool> clear() async { | ||
| _data.clear(); | ||
| return true; | ||
| } | ||
|
|
||
| @override | ||
| Future<Map<String, Object>> getAll() async { | ||
| return Map<String, Object>.from(_data); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> remove(String key) async { | ||
| _data.remove(key); | ||
| return true; | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> setValue(String valueType, String key, Object value) async { | ||
| _data[key] = value; | ||
| return true; | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
packages/shared_preferences/shared_preferences_platform_interface/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,18 @@ | ||
| name: shared_preferences_platform_interface | ||
| description: A common platform interface for the shared_preferences plugin. | ||
| author: Flutter Team <flutter-dev@googlegroups.com> | ||
| homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_platform_interface | ||
| version: 1.0.0 | ||
|
|
||
| dependencies: | ||
| meta: ^1.0.4 | ||
| flutter: | ||
| sdk: flutter | ||
|
|
||
| dev_dependencies: | ||
| flutter_test: | ||
| sdk: flutter | ||
|
|
||
| environment: | ||
| sdk: ">=2.0.0-dev.28.0 <3.0.0" | ||
| flutter: ">=1.6.7 <2.0.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.