作业 android SharedPreferences 代理

来源:7-5 案例:使用属性代理读写 Properties

慕神0090191

2024-06-10

提交给作业,没有使用泛型,没有区分不同的sp文件的极简版 sp 代理

package com.example.oneapplication.sp

import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class Preference(val context: Context, val name:String?, val default:Any):
    ReadWriteProperty<Any?, Any> {
    val prefs:SharedPreferences by lazy {
        context.getSharedPreferences("default",Context.MODE_PRIVATE)
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): Any {
        Log.e("Preference","getValue thisRef=$thisRef")
        Log.e("Preference","getValue property.name=$property.name")
        return getPreference(name?:property.name,default)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: Any) {
        Log.e("Preference","setValue thisRef=$thisRef")
        Log.e("Preference","setValue property.name=$property.name,value=$value")
        setPreference(name?:property.name,value)
    }

    private fun getPreference(name: String, default: Any): Any {
        return when(default){
            is Long -> {
                prefs.getLong(name,default)
            }
            is Int -> prefs.getInt(name,default)
            is Boolean -> prefs.getBoolean(name,default)
            is Float -> prefs.getFloat(name,default)
            is String -> prefs.getString(name,default)
            else -> throw IllegalArgumentException("type of value is error")
        } as Any
    }

    private fun setPreference(name: String, value: Any) {
        val editor = prefs.edit()
        when (value) {
            is String -> editor.putString(name, value)
            is Int -> editor.putInt(name, value)
            is Boolean -> editor.putBoolean(name, value)
            is Float -> editor.putFloat(name, value)
            is Long -> editor.putLong(name, value)
            else -> throw IllegalArgumentException("type of value is error")
        }.apply()
    }
}

使用

package com.example.oneapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.oneapplication.sp.Preference

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var mainActivityName:Any by Preference(this, "currentActivityName", "")
        mainActivityName = MainActivity::class.java.name

        val aa = mainActivityName
        Log.e("Preference","aa=====$aa")

        /*val boss = Boss(Staff())
        boss.doDesign()
        boss.doCode()
        boss.doTest()
        boss.doPublish()

        val company = Company()
        company.doDesign()
        company.doCode()
        company.doTest()
        company.doPublish()*/
    }
}
写回答

1回答

bennyhuo

2024-06-10

挺好~看起来基本都理解了~
0
0

学会Kotlin 突破开发语言瓶颈

如果有一门语言可以取代Java,那么它一定是Kotlin。

1760 学习 · 481 问题

查看课程