Skip to content

Commit

Permalink
Merge pull request #3 from ShawnLin013/custom_methods
Browse files Browse the repository at this point in the history
v1.0.2
  • Loading branch information
ShawnLin013 committed Apr 19, 2016
2 parents d4157a6 + 996775e commit 1814a01
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 23 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Initialize the preferences manager

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

Expand All @@ -22,6 +22,8 @@ PreferencesManager.putInt(key, value);

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

Example 2:
Expand Down Expand Up @@ -57,7 +59,7 @@ buildscript {
}
dependencies {
compile 'com.shawnlin:PreferencesManager:1.0.1'
compile 'com.shawnlin:PreferencesManager:1.0.2'
}
```

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_CODE=2
VERSION_NAME=1.0.1
VERSION_CODE=3
VERSION_NAME=1.0.2

GROUP=com.shawnlin
ARTIFACT_ID=PreferencesManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package com.shawnlin.preferencesmanager;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;

import java.util.HashSet;
import java.util.Set;

import static android.content.Context.MODE_PRIVATE;
import static android.content.Context.MODE_WORLD_READABLE;
import static android.content.Context.MODE_WORLD_WRITEABLE;

/**
* The utility that is used to manage the preferences
* The {@link PreferencesManager} is a utility that is used to manage the preferences.
*/
public class PreferencesManager {

private static SharedPreferences mSharedPreferences;
private static Gson mGson;
private static int INVALID_VALUE = -1;

private Context mContext;
private String mName;
private int mMode;

/**
* Initial the preferences manager.
Expand All @@ -25,6 +33,7 @@ public class PreferencesManager {
public PreferencesManager(Context context) {
mContext = context;
mGson = new Gson();
mMode = INVALID_VALUE;
}

/**
Expand All @@ -36,6 +45,15 @@ public PreferencesManager setName(String name) {
return this;
}

/**
* Set the mode of the preferences.
* @param mode The mode of the preferences.
*/
public PreferencesManager setMode(int mode) {
mMode = mode;
return this;
}

/**
* Initial the instance of the preferences manager.
*/
Expand All @@ -48,7 +66,12 @@ public void init() {
mName = mContext.getPackageName();
}

mSharedPreferences = mContext.getSharedPreferences(mName, Activity.MODE_PRIVATE);
if (mMode == INVALID_VALUE || (mMode != MODE_PRIVATE && mMode != MODE_WORLD_READABLE
&& mMode != MODE_WORLD_WRITEABLE)) {
mMode = MODE_PRIVATE;
}

mSharedPreferences = mContext.getSharedPreferences(mName, mMode);
}

/**
Expand All @@ -69,15 +92,62 @@ public static void putString(String key, String value) {
/**
* Retrieval a String value from the preferences.
* @param key The name of the preference to retrieve.
* @param defValue Value to return if this preference does not exist.
* @return Returns the preference values if they exist, or defValues.
*/
public static String getString(String key) {
public static String getString(String key, String defValue) {
if (mSharedPreferences == null) {
return "";
return defValue;
}
return mSharedPreferences.getString(key, "");
return mSharedPreferences.getString(key, defValue);
}

/**
* Retrieval a String value from the preferences.
* @param key The name of the preference to retrieve.
* @return Returns the preference values if they exist, or defValues.
*/
public static String getString(String key) {
return getString(key, "");
}

/**
* Put a set of String values in the preferences editor.
* @param key The name of the preference to modify.
* @param values The set of new values for the preference.
*/
public static void putStringSet(String key, Set<String> values) {
if (mSharedPreferences == null) {
return;
}

Editor editor = mSharedPreferences.edit();
editor.putStringSet(key, values);
editor.apply();
}

/**
* Retrieval a set of String values from the preferences.
* @param key The name of the preference to retrieve.
* @param defValues Values to return if this preference does not exist.
* @return Returns the preference values if they exist, or defValues.
*/
public static Set<String> getStringSet(String key, Set<String> defValues) {
if (mSharedPreferences == null) {
return defValues;
}
return mSharedPreferences.getStringSet(key, defValues);
}

/**
* Retrieval a set of String values from the preferences.
* @param key The name of the preference to retrieve.
* @return Returns the preference values if they exist, or defValues.
*/
public static Set<String> getStringSet(String key) {
return getStringSet(key, new HashSet<String>());
}

/**
* Put an int value in the preferences editor.
* @param key The name of the preference to modify.
Expand All @@ -93,16 +163,27 @@ public static void putInt(String key, int value) {
editor.apply();
}


/**
* Retrieval an int value from the preferences.
* @param key The name of the preference to retrieve.
* @param defValue Value to return if this preference does not exist.
* @return Returns the preference values if they exist, or defValues.
*/
public static int getInt(String key) {
public static int getInt(String key, int defValue) {
if (mSharedPreferences == null) {
return 0;
return defValue;
}
return mSharedPreferences.getInt(key, 0);
return mSharedPreferences.getInt(key, defValue);
}

/**
* Retrieval an int value from the preferences.
* @param key The name of the preference to retrieve.
* @return Returns the preference values if they exist, or defValues.
*/
public static int getInt(String key) {
return getInt(key, 0);
}

/**
Expand All @@ -123,13 +204,23 @@ public static void putFloat(String key, float value) {
/**
* Retrieval a float value from the preferences.
* @param key The name of the preference to retrieve.
* @param defValue Value to return if this preference does not exist.
* @return Returns the preference values if they exist, or defValues.
*/
public static float getFloat(String key) {
public static float getFloat(String key, float defValue) {
if (mSharedPreferences == null) {
return 0;
return defValue;
}
return mSharedPreferences.getFloat(key, 0);
return mSharedPreferences.getFloat(key, defValue);
}

/**
* Retrieval a float value from the preferences.
* @param key The name of the preference to retrieve.
* @return Returns the preference values if they exist, or defValues.
*/
public static float getFloat(String key) {
return getFloat(key, 0);
}

/**
Expand All @@ -150,13 +241,23 @@ public static void putLong(String key, long value) {
/**
* Retrieval a long value from the preferences.
* @param key The name of the preference to retrieve.
* @param defValue Value to return if this preference does not exist.
* @return Returns the preference values if they exist, or defValues.
*/
public static long getLong(String key) {
public static long getLong(String key, long defValue) {
if (mSharedPreferences == null) {
return 0;
return defValue;
}
return mSharedPreferences.getInt(key, 0);
return mSharedPreferences.getLong(key, defValue);
}

/**
* Retrieval a long value from the preferences.
* @param key The name of the preference to retrieve.
* @return Returns the preference values if they exist, or defValues.
*/
public static long getLong(String key) {
return getLong(key, 0);
}

/**
Expand All @@ -177,13 +278,23 @@ public static void putBoolean(String key, boolean value) {
/**
* Retrieval a boolean value from the preferences.
* @param key The name of the preference to retrieve.
* @param defValue Value to return if this preference does not exist.
* @return Returns the preference values if they exist, or defValues.
*/
public static boolean getBoolean(String key) {
public static boolean getBoolean(String key, boolean defValue) {
if (mSharedPreferences == null) {
return false;
return defValue;
}
return mSharedPreferences.getBoolean(key, false);
return mSharedPreferences.getBoolean(key, defValue);
}

/**
* Retrieval a boolean value from the preferences.
* @param key The name of the preference to retrieve.
* @return Returns the preference values if they exist, or defValues.
*/
public static boolean getBoolean(String key) {
return getBoolean(key, false);
}

/**
Expand Down Expand Up @@ -212,4 +323,25 @@ public static <T> T getObject(String key, Class<T> type) {
return mGson.fromJson(getString(key), type);
}

/**
* Remove a preference from the preferences editor.
* @param key The name of the preference to remove.
*/
public static void remove(String key) {
if (mSharedPreferences == null) {
return;
}
mSharedPreferences.edit().remove(key).apply();
}

/**
* Remove all values from the preferences editor.
*/
public static void clear() {
if (mSharedPreferences == null) {
return;
}
mSharedPreferences.edit().clear().apply();
}

}
2 changes: 1 addition & 1 deletion sample/src/main/java/com/shawnlin/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// Initialize the preferences manager
new PreferencesManager(this)
.setName("prefs")
.init();


Button intButton = (Button) findViewById(R.id.int_button);
intButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Expand Down

0 comments on commit 1814a01

Please sign in to comment.