package com.reactlibrary.data;

import android.content.Context;
import android.content.SharedPreferences;



/**
 * Created by MJX on 2017/1/13.
 */
public class GetSpInsance {
    /**
     * 得到sp对象
     * @param spName sp的名字
     * @return
     */
    public static SharedPreferences getSp(Context context,String spName){
        SharedPreferences sp = context.getSharedPreferences(spName, context.MODE_PRIVATE);
        return sp;
    }

    /**
     * 得到Editor对象
     * @param spName sp的名字
     * @return
     */
    public static SharedPreferences.Editor getEdit(Context context,String spName){
        SharedPreferences sp = context.getSharedPreferences(spName, context.MODE_PRIVATE);
        return  sp.edit();
    }

    /**
     * 保存sp的信息
     * @param spName
     * @param key
     * @param value
     * @param <T>
     */
    public static <T> void saveSp(Context context,String spName,String key,T value){
        final SharedPreferences sp = getSp(context,spName);
        if(value != null){
            if(value instanceof String){
                sp.edit().putString(key,(String)value).commit();
            }else if(value instanceof Boolean){
                sp.edit().putBoolean(key,(Boolean) value).commit();
            }else if(value instanceof  Integer){
                sp.edit().putInt(key,(Integer)value).commit();
            }
        }
    }

    /**
     * 得到保存的数据类型
     * @param spName
     * @param key
     * @param defauleValue
     * @param <T>
     * @return
     */
    public static <T> Object getSpValue(Context context,String spName,String key,T  defauleValue){
        final SharedPreferences sp = getSp(context,spName);
        if(defauleValue instanceof String){
            return  sp.getString(key,(String) defauleValue);
        }else if(defauleValue instanceof Boolean){
            return sp.getBoolean(key,(Boolean) defauleValue);
        }
        return null;
    }
}

