package chihane.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

/** SharedPreferences CRUD. */
public class Prefs {
    /** <p>Put a <b>boolean</b>/<b>float</b>/<b>int</b>/<b>long</b>/<b>String</b> value into
     *  preferences file named as {@linkplain Context#getPackageName() packageName}.
     *
     *  @throws IllegalArgumentException If The type of <code>value</code> is not one of those.
     *  @see android.content.SharedPreferences.Editor
     *  */
    public static void put(Context context, String key, Object value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            getValue(context, key, value).apply();
        } else {
            getValue(context, key, value).commit();
        }
    }

    @SuppressLint("CommitPrefEdits")
    private static SharedPreferences.Editor getValue(Context context, String key, Object value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE).edit();

        if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else if (value instanceof String) {
            editor.putString(key, (String) value);
        } else {
            throw new IllegalArgumentException("Argument illegal, see JavaDocs.");
        }

        return editor;
    }

    /** <p>Retrieve a <b>boolean</b>/<b>float</b>/<b>int</b>/<b>long</b>/<b>String</b> value from preferences.
     *
     *  @return value corresponding to <code>key</code> if <code>key</code> exists,
     *  <code>defValue</code> otherwise.
     *  @see SharedPreferences
     *  @throws IllegalArgumentException If The type of <code>defValue</code> is not one of those.
     *  */
    @SuppressWarnings("unchecked")
    public static <P> P get(Context context, String key, P defValue) {
        SharedPreferences prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);

        Object result;

        if (defValue instanceof Boolean) {
            result = prefs.getBoolean(key, (Boolean) defValue);
        } else if (defValue instanceof Float) {
            result = prefs.getFloat(key, (Float) defValue);
        } else if (defValue instanceof Integer) {
            result = prefs.getInt(key, (Integer) defValue);
        } else if (defValue instanceof Long) {
            result = prefs.getLong(key, (Long) defValue);
        } else if (defValue instanceof String) {
            result = prefs.getString(key, (String) defValue);
        } else {
            throw new IllegalArgumentException("Argument illegal, see JavaDocs.");
        }

        return (P) result;
    }

    /**
     * <p>Remove the value corresponding to <code>key</code>.</p>
     */
    public static void remove(Context context, String key) {
        SharedPreferences prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);

        if (prefs.contains(key)) {
            prefs.edit().remove(key).commit();
        }
    }
}
@@ -21,48 +21,8 @@
 s;%0A%0A
-import android.annotation.SuppressLint;%0A
 impo
@@ -49,16 +49,16 @@
 ontext;%0A
+
 import a
@@ -94,33 +94,8 @@
 ces;
-%0Aimport android.os.Build;
 %0A%0A/*
@@ -566,352 +566,8 @@
 ) %7B%0A
-        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.GINGERBREAD) %7B%0A            getValue(context, key, value).apply();%0A        %7D else %7B%0A            getValue(context, key, value).commit();%0A        %7D%0A    %7D%0A%0A    @SuppressLint(%22CommitPrefEdits%22)%0A    private static SharedPreferences.Editor getValue(Context context, String key, Object value) %7B%0A
     
@@ -1284,21 +1284,22 @@
     
-return editor
+editor.apply()
 ;%0A  
@@ -2624,85 +2624,222 @@
     
-/**%0A     * %3Cp%3ERemove the value corresponding to %3Ccode%3Ekey%3C/code%3E.%3C/p%3E
+public static boolean hasKey(Context context, String key) %7B%0A        SharedPreferences prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);%0A        return prefs.contains(key);
 %0A    
- */
+%7D%0A
 %0A   
@@ -3085,14 +3085,13 @@
 ey).
-commit
+apply
 ();%0A

Add hasKey() into Prefs