Skip to content

Instantly share code, notes, and snippets.

@voghDev
Last active June 29, 2020 20:16
Show Gist options
  • Save voghDev/189115ab19f5c9b7cd8d80788219802a to your computer and use it in GitHub Desktop.
Save voghDev/189115ab19f5c9b7cd8d80788219802a to your computer and use it in GitHub Desktop.
Generic Preference Helper to save some lines of code managing shared preferences
/*
* Copyright (C) 2016 Olmo Gallegos Hernández.
*
* 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.
*/
package com.mycompany.myapp.datasource.sharedpreference;
import android.content.Context;
import android.content.SharedPreferences;
public class PreferenceHelper {
public static final String PREFERENCES = "myapp_preferences";
// Getters
private static SharedPreferences getPreferences(Context ctx) {
return ctx.getSharedPreferences(PREFERENCES, 0);
}
public static String getString(Context ctx, String key) {
SharedPreferences prefs = getPreferences(ctx);
return prefs.getString(key, "");
}
public static int getInt(Context ctx, String key) {
SharedPreferences prefs = getPreferences(ctx);
return prefs.getInt(key, 0);
}
public static long getLong(Context ctx, String key) {
SharedPreferences prefs = getPreferences(ctx);
return prefs.getLong(key, 0L);
}
public static boolean getBoolean(Context ctx, String key) {
SharedPreferences prefs = getPreferences(ctx);
return prefs.getBoolean(key, false);
}
public static float getFloat(Context ctx, String key) {
SharedPreferences prefs = getPreferences(ctx);
return prefs.getFloat(key, 0f);
}
// Setters
public static void putString(Context ctx, String key, String value) {
SharedPreferences.Editor editor = getPreferences(ctx).edit();
editor.putString(key, value).apply();
}
public static void putBoolean(Context ctx, String key, boolean value) {
SharedPreferences.Editor editor = getPreferences(ctx).edit();
editor.putBoolean(key, value).apply();
}
public static void putLong(Context ctx, String key, long value) {
SharedPreferences.Editor editor = getPreferences(ctx).edit();
editor.putLong(key, value).apply();
}
public static void putInt(Context ctx, String key, int value) {
SharedPreferences.Editor editor = getPreferences(ctx).edit();
editor.putInt(key, value).apply();
}
public static void putFloat(Context ctx, String key, float value) {
SharedPreferences.Editor editor = getPreferences(ctx).edit();
editor.putFloat(key, value).apply();
}
public static void remove(Context ctx, String key) {
SharedPreferences.Editor editor = getPreferences(ctx).edit();
editor.remove(key).apply();
}
public static void clear(Context ctx) {
SharedPreferences.Editor editor = getPreferences(ctx).edit();
editor.clear().apply();
}
}
@voghDev
Copy link
Author

voghDev commented Aug 11, 2016

Usage:

private static final String PREFERENCE_CALIBRATION_COMPLETED = "calibrationCompleted";

@Override
public void setCalibrationCompleted(boolean value) {
    PreferenceHelper.putBoolean(context, PREFERENCE_CALIBRATION_COMPLETED, value);

}

@Override
public boolean isCalibrationCompleted() {
    return PreferenceHelper.getBoolean(context, PREFERENCE_CALIBRATION_COMPLETED);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment