分享一个kotlin中很好用的工具类

Author Avatar
贾康 1月 15, 2018

效果


//get default prefs
val prefs = PreferenceHelper.defaultPrefs(this)
//set any type of value in prefs
prefs[Constants.PREF_NAME] = “name”
//get any value from prefs
val name: String? = prefs[Constants.PREF_NAME]
//get value from prefs (with default value)
val age: Int? = prefs[Constants.PREF_AGE, 23]

怎么样?读写sharedPreference 就像读写数组那样简单.

源码


object PreferenceHelper {

fun defaultPrefs(context: Context): SharedPreferences
        = PreferenceManager.getDefaultSharedPreferences(context)

fun customPrefs(context: Context, name: String): SharedPreferences
        = context.getSharedPreferences(name, Context.MODE_PRIVATE)

inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
    val editor = this.edit()
    operation(editor)
    editor.apply()
}

/**
 * puts a key value pair in shared prefs if doesn't exists, otherwise updates value on given [key]
 */
operator fun SharedPreferences.set(key: String, value: Any?) {
    when (value) {
        is String? -> edit({ it.putString(key, value) })
        is Int -> edit({ it.putInt(key, value) })
        is Boolean -> edit({ it.putBoolean(key, value) })
        is Float -> edit({ it.putFloat(key, value) })
        is Long -> edit({ it.putLong(key, value) })
        else -> throw UnsupportedOperationException("Not yet implemented")
    }
}

/**
 * finds value on given key.
 * [T] is the type of value
 * @param defaultValue optional default value - will take null for strings, false for bool and -1 for numeric values if [defaultValue] is not specified
 */
operator inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T? {
    return when (T::class) {
        String::class -> getString(key, defaultValue as? String) as T?
        Int::class -> getInt(key, defaultValue as? Int ?: -1) as T?
        Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T?
        Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T?
        Long::class -> getLong(key, defaultValue as? Long ?: -1) as T?
        else -> throw UnsupportedOperationException("Not yet implemented")
    }
}

}

格式有点差,凑活看…

来源

感触

kotlin真的比java好用多了.