Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions packages/shared_preferences/example/android.iml

This file was deleted.

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions packages/shared_preferences/example/shared_preferences_example.iml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.4+7

* Restructure the project for Web support.

## 0.5.4+6

* Add missing documentation and a lint to prevent further undocumented APIs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: shared_preferences
description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
version: 0.5.4+6
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences
version: 0.5.4+7

flutter:
plugin:
Expand Down
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.
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.
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
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');
}
}
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 {
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
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"
Loading