lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cn.sinata.xldutils.utils
 
import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import cn.sinata.xldutils.xldUtils
 
/**
 * SharedPreferences 工具,链式调用.
 */
class SPUtils private constructor() {
    private var sp: SharedPreferences? = null
    private var editor: SharedPreferences.Editor? = null
    init {
        val name = if (TextUtils.isEmpty(xldUtils.SP_NAME)) {
            xldUtils.context?.packageName + ".sp"
        } else {
            xldUtils.SP_NAME!!
        }
        sp = xldUtils.context?.getSharedPreferences(name, Context.MODE_PRIVATE)
        editor = sp!!.edit()
    }
 
    fun getString(key: String,defValue:String=""): String = sp!!.getString(key, defValue)!!
 
    fun getInt(key: String, defValue: Int=-1):Int = sp!!.getInt(key,defValue)
 
    fun getFloat(key: String,defValue: Float=0f):Float = sp!!.getFloat(key,defValue)
 
    fun getLong(key: String,defValue: Long=0):Long = sp!!.getLong(key,defValue)
 
    fun getBoolean(key: String,defValue: Boolean=false):Boolean = sp!!.getBoolean(key,defValue)
 
    fun put(key: String, value: Int): SPUtils {
        editor?.putInt(key, value)
        return this
    }
 
    fun put(key: String, value: Float): SPUtils {
        editor?.putFloat(key, value)
        return this
    }
 
    fun put(key: String, value: Boolean): SPUtils {
        editor?.putBoolean(key, value)
        return this
    }
 
    fun put(key: String, value: Long): SPUtils {
        editor?.putLong(key, value)
        return this
    }
 
    fun put(key: String, value: String?): SPUtils {
        if (value == null) {
            return this
        }
        editor?.putString(key, value)
        return this
    }
 
    fun remove(key: String):SPUtils {
        editor?.remove(key)
        return this
    }
 
    fun clear():SPUtils {
        editor?.clear()
        return this
    }
 
    @Synchronized fun apply() {
        editor?.apply()
    }
 
    companion object {
        fun instance() : SPUtils = SPUtils()
    }
}