Last active
July 23, 2017 03:04
-
-
Save marlonlom/54ec95244520710b8c4d3615feac8fc9 to your computer and use it in GitHub Desktop.
Gist for Preferences.java, Utility class for handling read/write operations using SharedPreferences
This file contains 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
/* | |
* Copyright (c) 2017, marlonlom | |
* | |
* 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. | |
*/ | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
/** | |
* Utility class for handling read/write operations using {@link android.content.SharedPreferences} | |
* | |
* @author marlonlom | |
*/ | |
public final class Preferences { | |
/** | |
* Empty string constant for default string preference values. | |
*/ | |
private static final String EMPTY_STRING = ""; | |
/** | |
* Empty string constant for default float preference values. | |
*/ | |
private static final float FLOAT_DEFAULT = -1; | |
/** | |
* With app preferences builder. | |
* | |
* @param context application context | |
* @param packageName application package name | |
* @return the app preferences builder | |
*/ | |
public static Preferences.Builder with(Context context, String packageName) { | |
return new Preferences.Builder(context, packageName); | |
} | |
/** | |
* Builder inner class for Application preferences manager. | |
* | |
* @author marlonlom | |
*/ | |
public static class Builder { | |
/** | |
* Shared preferences manager for general use. | |
*/ | |
private SharedPreferences preferences; | |
/** | |
* Default constructor for builder | |
* | |
* @param context application context | |
* @param packageName application package name | |
*/ | |
public Builder(Context context, String packageName) { | |
this.preferences = | |
context.getSharedPreferences(packageName.concat(".prefs"), Context.MODE_PRIVATE); | |
} | |
/** | |
* Reads and return an int value for preference | |
* | |
* @param key preference key | |
* @return preference int value | |
*/ | |
public int readInt(String key) { | |
return this.preferences.getInt(key, -1); | |
} | |
/** | |
* Put an int value for preference | |
* | |
* @param key preference key | |
* @param value preference value | |
*/ | |
public void saveInt(String key, int value) { | |
final SharedPreferences.Editor editor = this.preferences.edit(); | |
editor.putInt(key, value); | |
editor.apply(); | |
} | |
/** | |
* Put a float value for preference | |
* | |
* @param key preference key | |
* @param value preference value | |
*/ | |
public void saveFloat(String key, float value) { | |
final SharedPreferences.Editor editor = this.preferences.edit(); | |
editor.putFloat(key, value); | |
editor.apply(); | |
} | |
/** | |
* Reads and return an float value for preference | |
* | |
* @param key preference key | |
* @return preference float value | |
*/ | |
public float readFloat(String key) { | |
return this.preferences.getFloat(key, FLOAT_DEFAULT); | |
} | |
/** | |
* Put a string value for preference | |
* | |
* @param key preference key | |
* @param value preference value | |
*/ | |
public void saveString(String key, String value) { | |
final SharedPreferences.Editor editor = this.preferences.edit(); | |
editor.putString(key, value); | |
editor.apply(); | |
} | |
/** | |
* Reads and return an string value for preference | |
* | |
* @param key preference key | |
* @return preference string value | |
*/ | |
public String readString(String key) { | |
return this.preferences.getString(key, EMPTY_STRING); | |
} | |
/** | |
* Put a boolean value for preference. | |
* | |
* @param key preference key | |
* @param value preference value | |
*/ | |
public void saveBoolean(String key, Boolean value) { | |
final SharedPreferences.Editor editor = this.preferences.edit(); | |
editor.putBoolean(key, value); | |
editor.apply(); | |
} | |
/** | |
* Reads and return an boolean value for preference | |
* | |
* @param key preference key | |
* @param defaultValue boolean default value | |
* @return preference string value | |
*/ | |
public Boolean readBoolean(String key, Boolean defaultValue) { | |
return this.preferences.getBoolean(key, defaultValue); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment