Skip to content

Commit 9f3d30e

Browse files
committed
[1.7.0-feature] 委托封装sp优化
1 parent b961f32 commit 9f3d30e

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

app/src/main/kotlin/org/ninetripods/mq/study/activity/SharedPreferencesActivity.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package org.ninetripods.mq.study.activity
33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
55
import org.ninetripods.mq.study.kotlin.ktx.log
6-
import org.ninetripods.mq.study.util.sp.FirPreferences
7-
import org.ninetripods.mq.study.util.sp.SecPreferences
6+
import org.ninetripods.mq.study.util.sp.CommonPreferences
87

98
/**
109
* SharedPreferences使用kotlin委托方式实现:[org.ninetripods.mq.study.util.sp.SharedPreferencesDelegate]
@@ -15,20 +14,21 @@ class SharedPreferencesActivity : AppCompatActivity() {
1514
/**
1615
* 支持按不同文件名进行存值
1716
*/
18-
private val firSp = FirPreferences(this)
19-
private val secSp = SecPreferences(this)
17+
private val spInfo = CommonPreferences(this)
2018

2119
override fun onCreate(savedInstanceState: Bundle?) {
2220
super.onCreate(savedInstanceState)
2321

2422
//存值
25-
firSp.isShow = true
26-
firSp.name = "小马快跑"
23+
spInfo.isShow = true
24+
spInfo.name = "小马快跑"
2725
//存值
28-
secSp.age = 18
29-
secSp.cost = 123.4f
26+
spInfo.age = 18
27+
spInfo.cost = 123.4f
28+
spInfo.setString = setOf("", "", "")
3029
//取值
31-
log("first: isShow -> ${firSp.isShow}, name -> ${firSp.name}")
32-
log("second: age -> ${secSp.age}, cost -> ${secSp.cost}")
30+
log("isShow -> ${spInfo.isShow}, name -> ${spInfo.name}")
31+
log("age -> ${spInfo.age}, cost -> ${spInfo.cost},setString -> ${spInfo.setString}")
32+
3333
}
3434
}

app/src/main/kotlin/org/ninetripods/mq/study/util/sp/SharedPreferencesDelegate.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SharedPreferencesDelegate<T>(
2020
private val key: String? = null,
2121
) : ReadWriteProperty<Any?, T> {
2222

23-
private val sp: SharedPreferences by lazy {
23+
private val sp: SharedPreferences by lazy(LazyThreadSafetyMode.NONE) {
2424
context.getSharedPreferences(spName, Context.MODE_PRIVATE)
2525
}
2626

@@ -54,24 +54,24 @@ class SharedPreferencesDelegate<T>(
5454
}
5555
}
5656

57-
class FirPreferences(context: Context) {
57+
class CommonPreferences(context: Context) {
5858
companion object {
59-
const val SP_NAME = "FIR_SP_NAME"
59+
/**
60+
* 通过传入不同的SP文件名来存储到不同的XML中
61+
*/
62+
const val FIR_SP_NAME = "FIR_SP_NAME" //文件名1
63+
const val SEC_SP_NAME = "SEC_SP_NAME"//文件名2
6064
}
6165

62-
var isShow by SharedPreferencesDelegate(context, SP_NAME, false, "key_is_show")
66+
var isShow by SharedPreferencesDelegate(context, FIR_SP_NAME, false, "key_is_show")
6367

6468
//这里没有用key值,则会默认使用属性名来当做key值
65-
var name by SharedPreferencesDelegate(context, SP_NAME, "")
66-
}
67-
68-
class SecPreferences(context: Context) {
69-
companion object {
70-
const val SP_NAME = "SEC_SP_NAME"
71-
}
69+
var name by SharedPreferencesDelegate(context, FIR_SP_NAME, "")
7270

73-
var age by SharedPreferencesDelegate(context, SP_NAME, 0, "key_age")
71+
var age by SharedPreferencesDelegate(context, SEC_SP_NAME, 0, "key_age")
7472

7573
//这里没有用key值,则会默认使用属性名来当做key值
76-
var cost by SharedPreferencesDelegate(context, SP_NAME, 0.0f)
77-
}
74+
var cost by SharedPreferencesDelegate(context, SEC_SP_NAME, 0.0f)
75+
76+
var setString by SharedPreferencesDelegate(context, SEC_SP_NAME, setOf("1", "2", "3"))
77+
}

0 commit comments

Comments
 (0)