美文网首页程序员Android开发经验谈
Kotlin委托优化SharedPreferences存储

Kotlin委托优化SharedPreferences存储

作者: JingChen_ | 来源:发表于2020-09-24 15:43 被阅读0次

分享下最近优化的关于SP存储的代码

private var test by Test("123")

class Test(val key:String){
    operator fun getValue(any: Any, property: KProperty<*>): Any {
        return SharedPreferencesUtil.getInstance().get(key,"")
    }

    operator fun setValue(any: Any, property: KProperty<*>, value: Any) {
        SharedPreferencesUtil.getInstance().put(key,value)
    }
}

这里用到了Kotlin的委托模式

在这里我们先写好一个SP的工具类

public class SharedPreferencesUtil {
    private static SharedPreferencesUtil instance;

    private SharedPreferences.Editor editor;
    private SharedPreferences prefer;

    @SuppressLint("CommitPrefEdits")
    private SharedPreferencesUtil() {
        this.prefer = PreferenceManager.getDefaultSharedPreferences(BaseApplication.getAppContext());
        this.editor = this.prefer.edit();
    }

    public static SharedPreferencesUtil getInstance() {
        if (instance == null) {
            synchronized (SharedPreferencesUtil.class) {
                if (instance == null) {
                    instance = new SharedPreferencesUtil();
                }
            }
        }

        return instance;
    }

    //存入sp
    public void put(String name, Object data) {
        if(data instanceof String){
            this.editor.putString(name, (String) data);
        }else if(data instanceof Integer){
            this.editor.putInt(name, (Integer) data);
        }else if(data instanceof Boolean){
            this.editor.putBoolean(name, (Boolean) data);
        }else if(data instanceof Float){
            this.editor.putFloat(name, (Float) data);
        }else if(data instanceof Long){
            this.editor.putLong(name, (Long) data);
        }else{
            if (data != null) {
                this.editor.putString(name, data.toString());
            }
        }

        this.editor.apply();
    }

    //获取sp
    public Object get(String key, Object defaultObject) {
        if(defaultObject instanceof String) {
            return this.prefer.getString(key, (String) defaultObject);
        }else if(defaultObject instanceof Integer){
            return this.prefer.getInt(key, (Integer) defaultObject);
        }else if(defaultObject instanceof Float){
            return this.prefer.getFloat(key, (Float) defaultObject);
        }else if(defaultObject instanceof Boolean){
            return this.prefer.getBoolean(key, (Boolean) defaultObject);
        }else if(defaultObject instanceof Long){
            return this.prefer.getLong(key, (Long) defaultObject);
        }

        return null;
    }

    /**
     * 移除某个key值对应的值
     * */
    public void remove(String key){
        this.editor.remove(key);
        this.editor.apply();
    }

    /**
     * 清除所有的内容
     *
     * */
    public void clear(){
        this.editor.clear();
        this.editor.apply();
    }

    /**
     * 查询某个key是否存在
     * */
    public boolean contains(String key){
        return this.prefer.contains(key);
    }
}

刚才用var定义的test变量,用by关键字委托给Test(),属性的 get() 方法(以及set() 方法)将被委托给这个对象的 getValue() 和 setValue() 方法。属性委托不必实现任何接口, 但必须提供 getValue() 函数(对于 var属性,还需要 setValue() 函数)。

class Test(val key:String){
    operator fun getValue(any: Any, property: KProperty<*>): Any {
        return SharedPreferencesUtil.getInstance().get(key,"")
    }

    operator fun setValue(any: Any, property: KProperty<*>, value: Any) {
        SharedPreferencesUtil.getInstance().put(key,value)
    }
}

test变量调用getValue()方法,假如SP中已经存了刚才传进来的"123"key值,就直接取到对应的值,如果没有,就给定设定好的默认值""。这样做的好处是,当我要去系统中取到之前已经存储的东西,不用每次都调用SharedPreferencesUtil.getInstance().get(key,""),当我们去个他设定一个值时,就调用了setValue方法,传进来的值就是参数value,然后通过SP的存方式存在缓存中。通过委托的方式直接传值取值,使得代码简洁许多。

相关文章

网友评论

    本文标题:Kotlin委托优化SharedPreferences存储

    本文链接:https://www.haomeiwen.com/subject/jfnzyktx.html