Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit d238a53

Browse files
Merge pull request #16 from collinjackson/shared_preferences
Add Shared Preferences plugin
2 parents ea3623c + d7d4c77 commit d238a53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1763
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
.atom/
3+
.idea
4+
.packages
5+
.pub/
6+
build/
7+
ios/.generated/
8+
packages
9+
pubspec.lock
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [0.1.0] - 2017-05-05
2+
3+
* Initial Open Source release.

packages/shared-preferences/LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright 2017, the Flutter project authors. All rights reserved.
2+
Redistribution and use in source and binary forms, with or without
3+
modification, are permitted provided that the following conditions are
4+
met:
5+
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above
9+
copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided
11+
with the distribution.
12+
* Neither the name of Google Inc. nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

packages/shared-preferences/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# shared_preferences
2+
3+
Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing
4+
a persistent store for simple data. Data is persisted to disk automatically
5+
and asynchronously.
6+
7+
## Usage
8+
To use this plugin, add shared_preferences as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).
9+
10+
### Example
11+
12+
``` dart
13+
import 'package:flutter/material.dart';
14+
import 'package:shared_preferences/shared_preferences.dart';
15+
16+
void main() {
17+
runApp(new MaterialApp(
18+
home: new Scaffold(
19+
body: new Center(
20+
child: new RaisedButton(
21+
onPressed: _incrementCounter,
22+
child: new Text('Increment Counter'),
23+
),
24+
),
25+
),
26+
));
27+
}
28+
29+
_incrementCounter() async {
30+
SharedPreferences prefs = await SharedPreferences.getInstance();
31+
int counter = (prefs.getInt('counter') ?? 0) + 1;
32+
print('Pressed $counter times.');
33+
prefs.setInt('counter', counter);
34+
}
35+
```
36+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
10+
/gradle
11+
/gradlew
12+
/gradlew.bat
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
group 'io.flutter.plugins.shared_preferences'
2+
version '1.0-SNAPSHOT'
3+
4+
buildscript {
5+
repositories {
6+
jcenter()
7+
}
8+
9+
dependencies {
10+
classpath 'com.android.tools.build:gradle:2.3.0'
11+
}
12+
}
13+
14+
allprojects {
15+
repositories {
16+
jcenter()
17+
}
18+
}
19+
20+
apply plugin: 'com.android.library'
21+
22+
android {
23+
compileSdkVersion 25
24+
buildToolsVersion '25.0.0'
25+
26+
defaultConfig {
27+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
28+
}
29+
lintOptions {
30+
disable 'InvalidPackage'
31+
}
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.jvmargs=-Xmx1536M
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'shared_preferences'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="io.flutter.plugins.shared_preferences"
3+
android:versionCode="1"
4+
android:versionName="0.0.1">
5+
6+
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
7+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.shared_preferences;
6+
7+
import android.content.Context;
8+
import io.flutter.app.FlutterActivity;
9+
import io.flutter.plugin.common.MethodChannel;
10+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
11+
import io.flutter.plugin.common.MethodChannel.Result;
12+
import io.flutter.plugin.common.MethodCall;
13+
import io.flutter.view.FlutterView;
14+
import java.util.HashMap;
15+
import java.util.HashSet;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
/**
20+
* SharedPreferencesPlugin
21+
*/
22+
public class SharedPreferencesPlugin implements MethodCallHandler {
23+
private static final String SHARED_PREFERENCES_NAME = "FlutterSharedPreferences";
24+
private static final String CHANNEL_NAME = "plugins.flutter.io/shared_preferences";
25+
26+
private final android.content.SharedPreferences preferences;
27+
private final android.content.SharedPreferences.Editor editor;
28+
29+
public static SharedPreferencesPlugin register(FlutterActivity activity) {
30+
return new SharedPreferencesPlugin(activity);
31+
}
32+
33+
private SharedPreferencesPlugin(FlutterActivity activity) {
34+
preferences = activity.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
35+
editor = preferences.edit();
36+
new MethodChannel(activity.getFlutterView(), CHANNEL_NAME).setMethodCallHandler(this);
37+
}
38+
39+
// Filter preferences to only those set by the flutter app.
40+
private Map<String, Object> getAllPrefs() {
41+
Map<String, ?> allPrefs = preferences.getAll();
42+
Map<String, Object> filteredPrefs = new HashMap<>();
43+
for (String key : allPrefs.keySet()) {
44+
if (key.startsWith("flutter.")) {
45+
filteredPrefs.put(key, allPrefs.get(key));
46+
}
47+
}
48+
return filteredPrefs;
49+
}
50+
51+
@Override
52+
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
53+
String key = call.argument("key");
54+
switch (call.method) {
55+
case "setBool":
56+
editor.putBoolean(key, (boolean) call.argument("value")).apply();
57+
result.success(null);
58+
break;
59+
case "setDouble":
60+
editor.putFloat(key, (float) call.argument("value")).apply();
61+
result.success(null);
62+
break;
63+
case "setInt":
64+
editor.putInt(key, (int) call.argument("value")).apply();
65+
result.success(null);
66+
break;
67+
case "setString":
68+
editor.putString(key, (String) call.argument("value")).apply();
69+
result.success(null);
70+
break;
71+
case "setStringSet":
72+
editor.putStringSet(key, new HashSet<>((List<String>) call.argument("value"))).apply();
73+
result.success(null);
74+
break;
75+
case "commit":
76+
result.success(editor.commit());
77+
break;
78+
case "getAll":
79+
result.success(getAllPrefs());
80+
break;
81+
case "clear":
82+
for (String keyToDelete : getAllPrefs().keySet()) {
83+
editor.remove(keyToDelete);
84+
}
85+
result.success(editor.commit());
86+
break;
87+
default:
88+
result.notImplemented();
89+
break;
90+
}
91+
}
92+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
.atom/
3+
.idea
4+
.packages
5+
.pub/
6+
build/
7+
ios/.generated/
8+
packages
9+
pubspec.lock
10+
.flutter-plugins
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# shared_preferences_example
2+
3+
Demonstrates how to use the shared_preferences plugin.
4+
5+
## Getting Started
6+
7+
For help getting started with Flutter, view our online
8+
[documentation](http://flutter.io/).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$/android">
6+
<sourceFolder url="file://$MODULE_DIR$/android/app/src/main/java" isTestSource="false" />
7+
</content>
8+
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="Flutter for Android" level="project" />
11+
</component>
12+
</module>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
PluginRegistry.java
10+
11+
/gradle
12+
/gradlew
13+
/gradlew.bat
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
def localProperties = new Properties()
2+
def localPropertiesFile = rootProject.file('local.properties')
3+
if (localPropertiesFile.exists()) {
4+
localPropertiesFile.withInputStream { stream ->
5+
localProperties.load(stream)
6+
}
7+
}
8+
9+
def flutterRoot = localProperties.getProperty('flutter.sdk')
10+
if (flutterRoot == null) {
11+
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12+
}
13+
14+
apply plugin: 'com.android.application'
15+
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
16+
17+
android {
18+
compileSdkVersion 25
19+
buildToolsVersion '25.0.2'
20+
21+
lintOptions {
22+
disable 'InvalidPackage'
23+
}
24+
25+
defaultConfig {
26+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
27+
28+
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
29+
applicationId "com.yourcompany.shared_preferences_example"
30+
}
31+
32+
buildTypes {
33+
release {
34+
// TODO: Add your own signing config for the release build.
35+
// Signing with the debug keys for now, so `flutter run --release` works.
36+
signingConfig signingConfigs.debug
37+
}
38+
}
39+
}
40+
41+
flutter {
42+
source '../..'
43+
}
44+
45+
dependencies {
46+
androidTestCompile 'com.android.support:support-annotations:25.0.0'
47+
androidTestCompile 'com.android.support.test:runner:0.5'
48+
androidTestCompile 'com.android.support.test:rules:0.5'
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="io.flutter.plugins.shared_preferences_example"
3+
android:versionCode="1"
4+
android:versionName="0.0.1">
5+
6+
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
7+
8+
<!-- The INTERNET permission is required for development. Specifically,
9+
flutter needs it to communicate with the running application
10+
to allow setting breakpoints, to provide hot reload, etc.
11+
-->
12+
<uses-permission android:name="android.permission.INTERNET"/>
13+
14+
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
15+
calls FlutterMain.startInitialization(this); in its onCreate method.
16+
In most cases you can leave this as-is, but you if you want to provide
17+
additional functionality it is fine to subclass or reimplement
18+
FlutterApplication and put your custom class here. -->
19+
<application android:name="io.flutter.app.FlutterApplication" android:label="shared_preferences_example" android:icon="@mipmap/ic_launcher">
20+
<activity android:name=".MainActivity"
21+
android:launchMode="singleTop"
22+
android:theme="@android:style/Theme.Black.NoTitleBar"
23+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
24+
android:hardwareAccelerated="true"
25+
android:windowSoftInputMode="adjustResize">
26+
<intent-filter>
27+
<action android:name="android.intent.action.MAIN"/>
28+
<category android:name="android.intent.category.LAUNCHER"/>
29+
</intent-filter>
30+
</activity>
31+
</application>
32+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.flutter.plugins.shared_preferences_example;
2+
3+
import android.os.Bundle;
4+
import io.flutter.app.FlutterActivity;
5+
import io.flutter.plugins.PluginRegistry;
6+
7+
public class MainActivity extends FlutterActivity {
8+
PluginRegistry pluginRegistry;
9+
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
pluginRegistry = new PluginRegistry();
14+
pluginRegistry.registerAll(this);
15+
}
16+
}
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)