Skip to content

Commit 8a2aaf2

Browse files
committed
增加EasyShardPreferences工具方类方法,冰修改示例
1 parent bf41ff7 commit 8a2aaf2

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

app/src/main/java/com/haoge/sample/easyandroid/activities/EasySharedPreferencesActivity.kt

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import butterknife.OnClick
55
import com.haoge.easyandroid.easy.*
66
import com.haoge.sample.easyandroid.BaseActivity
77
import com.haoge.sample.easyandroid.R
8+
import kotlin.reflect.KProperty
89

910
/**
1011
* @author haoge on 2018/6/26
@@ -48,5 +49,15 @@ class Entity:PreferenceSupport() {
4849
var mLong:Long = 0L
4950
var mStr:String = ""
5051
@PreferenceIgnore
51-
var ignore:String = "This is ignore text"
52-
}
52+
var password:String by EncryptTool()
53+
}
54+
55+
class EncryptTool{
56+
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
57+
return "这是一个解密后的密码"
58+
}
59+
60+
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
61+
println("这里对密码进行了加密")
62+
}
63+
}

utils/src/main/java/com/haoge/easyandroid/easy/EasySharedPreferences.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
@file:Suppress("unused")
1617
package com.haoge.easyandroid.easy
1718

1819
import android.content.Context
@@ -234,6 +235,18 @@ class EasySharedPreferences(clazz: Class<*>):SharedPreferences.OnSharedPreferenc
234235
}
235236
}
236237

238+
@JvmStatic
239+
fun <E>getSPValue(key: String, default: E):E{
240+
val result:E by PreferenceUtil(key, default)
241+
return result
242+
}
243+
244+
@JvmStatic
245+
fun <E>putSPValue(key: String, value: E){
246+
var old:E by PreferenceUtil(key, value)
247+
old = value
248+
}
249+
237250
internal fun find(clazz: Class<*>):EasySharedPreferences {
238251
return container[clazz]?:throw RuntimeException("Could not find EasySharedPreferences by this clazz:[${clazz.canonicalName}]")
239252
}
@@ -257,5 +270,5 @@ abstract class PreferenceSupport {
257270
annotation class PreferenceRename(val value:String = "")
258271

259272
@Retention(AnnotationRetention.RUNTIME)
260-
@Target(AnnotationTarget.FIELD)
273+
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
261274
annotation class PreferenceIgnore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.haoge.easyandroid.easy
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.content.SharedPreferences
6+
import com.haoge.easyandroid.EasyAndroid
7+
import kotlin.reflect.KProperty
8+
9+
class PreferenceUtil<T>(val name: String, private val default: T) {
10+
11+
12+
private val prefs: SharedPreferences by lazy { EasyAndroid.getApplicationContext().getSharedPreferences(name, Context.MODE_PRIVATE) }
13+
14+
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
15+
return getSharePreferences(name, default)
16+
}
17+
18+
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
19+
putSharePreferences(name, value)
20+
}
21+
22+
@SuppressLint("CommitPrefEdits")
23+
fun putSharePreferences(name: String, value: T) = with(prefs.edit()) {
24+
when (value) {
25+
is Long -> putLong(name, value)
26+
is String -> putString(name, value)
27+
is Int -> putInt(name, value)
28+
is Boolean -> putBoolean(name, value)
29+
is Float -> putFloat(name, value)
30+
else -> throw IllegalArgumentException("This type of data cannot be saved!")
31+
}.apply()
32+
}
33+
34+
@Suppress("UNCHECKED_CAST")
35+
fun getSharePreferences(name: String, default: T): T = with(prefs) {
36+
val res: Any = when (default) {
37+
is Long -> getLong(name, default)
38+
is String -> getString(name, default)!!
39+
is Int -> getInt(name, default)
40+
is Boolean -> getBoolean(name, default)
41+
is Float -> getFloat(name, default)
42+
else -> throw IllegalArgumentException("This type of data cannot be saved!")
43+
}
44+
return res as T
45+
}
46+
}

0 commit comments

Comments
 (0)