Skip to content

Commit

Permalink
Merge pull request #1 from ShawnLin013/add_object_pref
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
ShawnLin013 committed Apr 15, 2016
2 parents 5ea87fb + 7237c35 commit 9f1c015
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 53 deletions.
6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Preferences Manager

The android library that is used to manage the preferences.

[![Platform](http://img.shields.io/badge/platform-android-brightgreen.svg?style=flat)](http://developer.android.com/index.html) [![Language](http://img.shields.io/badge/language-java-orange.svg?style=flat)](http://www.oracle.com/technetwork/java/javase/downloads/index.html) [![License](http://img.shields.io/badge/license-apache2.0-lightgrey.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)

## Usage

Initialize the preferences manager

```java
new PreferencesManager(this)
.setName("prefs")
.init();
```

Example 1:

```java
// put int to preferences
PreferencesManager.putInt(key, value);

// get int from preferences
PreferencesManager.getInt(key)
```

Example 2:

```java
// your object
class Person {

public String name;

public Person(String name) {
this.name = name;
}

}

// put object to preferences
PreferencesManager.putObject(key, new Person(name));

// get object from preferences
PreferencesManager.getObject(key, Person.class);
```

## Gradle

Add the dependency in your `build.gradle`

```groovy
buildscript {
repositories {
jcenter()
}
}
dependencies {
compile 'com.shawnlin:PreferencesManager:1.0.1'
}
```

## License

```
Copyright (C) 2016 ShawnLin013(Shawn Lin)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.novoda:bintray-release:0.3.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
30 changes: 14 additions & 16 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# Project-wide Gradle settings.
VERSION_CODE=2
VERSION_NAME=1.0.1

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
GROUP=com.shawnlin
ARTIFACT_ID=PreferencesManager

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
POM_DESCRIPTION=The android library that is used to manage the preferences.
POM_URL=https://github.com/ShawnLin013/PreferencesManager
POM_SCM_URL[email protected]:ShawnLin013/PreferencesManager.git
POM_LICENSE_NAME=The Apache Software License, Version 2.0
POM_LICENSE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENSE_DIST=repo
POM_DEVELOPER_ID=shawnlin013
POM_DEVELOPER_NAME=shawnlin013
POM_DEVELOPER_EMAIL[email protected]
ISSUE_URL=https://github.com/ShawnLin013/PreferencesManager/issues
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Oct 21 11:34:03 PDT 2015
#Thu Apr 14 15:31:39 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
26 changes: 18 additions & 8 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0.0"
versionCode VERSION_CODE as int
versionName VERSION_NAME
}
buildTypes {
release {
Expand All @@ -18,11 +18,21 @@ android {
}
}
}

dependencies {
compile 'com.google.code.gson:gson:2.6.2'
}

publish {
userOrg = 'shawnlin013'
groupId = 'com.shawnlin'
artifactId = rootProject.name
publishVersion = '1.0.0'
desc = 'The utility that is used to manage the preferences.'
website = 'https://github.com/ShawnLin013/${rootProject.name}'
userOrg = POM_DEVELOPER_ID
groupId = GROUP
artifactId = ARTIFACT_ID
publishVersion = VERSION_NAME
licences = ['Apache-2.0']
desc = POM_DESCRIPTION
website = POM_URL
issueTracker = ISSUE_URL
repository = POM_SCM_URL
autoPublish = false
dryRun = false
}
15 changes: 15 additions & 0 deletions library/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Gson
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;

/**
* The utility that is used to manage the preferences
*/
public class PreferencesManager {

private static SharedPreferences mSharedPreferences;
private static Gson mGson;

private Context mContext;
private String mName;

Expand All @@ -20,6 +24,7 @@ public class PreferencesManager {
*/
public PreferencesManager(Context context) {
mContext = context;
mGson = new Gson();
}

/**
Expand Down Expand Up @@ -181,4 +186,30 @@ public static boolean getBoolean(String key) {
return mSharedPreferences.getBoolean(key, false);
}

/**
* Put a object in the preferences editor.
* @param key The name of the preference to modify.
* @param value The new value for the preference.
*/
public static void putObject(String key, Object value) {
if (mGson == null || value == null) {
return;
}

putString(key, mGson.toJson(value));
}

/**
* Retrieval a object from the preferences.
* @param key The name of the preference to retrieve.
* @param type The class of the preference to retrieve.
* @return Returns the preference values if they exist, or defValues.
*/
public static <T> T getObject(String key, Class<T> type) {
if (mSharedPreferences == null || mGson == null) {
return null;
}
return mGson.fromJson(getString(key), type);
}

}
38 changes: 31 additions & 7 deletions sample/src/main/java/com/shawnlin/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.shawnlin.sample;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.shawnlin.preferencesmanager.PreferencesManager;

public class MainActivity extends AppCompatActivity {

private static final String KEY_INT = "int";
private static final String KEY_OBJECT = "object";

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Expand All @@ -22,14 +24,36 @@ public class MainActivity extends AppCompatActivity {
.setName("prefs")
.init();

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {

Button intButton = (Button) findViewById(R.id.int_button);
intButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
PreferencesManager.putInt("test", (int) (Math.random() * 10 + 10));
Toast.makeText(MainActivity.this, String.valueOf(PreferencesManager.getInt("test")),
Toast.LENGTH_SHORT).show();
PreferencesManager.putInt(KEY_INT, (int) (Math.random() * 10 + 10));
makeToast(String.valueOf(PreferencesManager.getInt(KEY_INT)));
}
});

Button objectButton = (Button) findViewById(R.id.object_button);
objectButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
PreferencesManager.putObject(KEY_OBJECT, new Person(getString(R.string.app_name)));
makeToast(PreferencesManager.getObject(KEY_OBJECT, Person.class).name);
}
});
}

private void makeToast(String str) {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}

class Person {

public String name;

public Person(String name) {
this.name = name;
}

}

}
8 changes: 0 additions & 8 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,4 @@

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
28 changes: 17 additions & 11 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.shawnlin.sample.MainActivity"
tools:showIn="@layout/activity_main">
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
android:layout_width="wrap_content"
<Button
android:id="@+id/int_button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
android:text="putInt()" />

<Button
android:id="@+id/object_button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="putObject()" />

</LinearLayout>

0 comments on commit 9f1c015

Please sign in to comment.