美文网首页
Android (Kotlin)工具之--简单封装SharedP

Android (Kotlin)工具之--简单封装SharedP

作者: 想看烟花么 | 来源:发表于2021-04-17 10:50 被阅读0次

代码的世界虽逻辑繁华,却又大道至简。

可执行代码片段

package com.patrick.utils

import android.content.Context
import android.content.SharedPreferences
import com.patrick.moti.MyApplication

/**
 * SharedPreference tool
 * @author patrick
 */
object SPUtil {
    //tag of SP file name.
    private const val KEY_FILE_NAME_DEFAULT = "U_Smile"

    /**
     * put data into SP file what you wanna to save.
     * @param key | String
     * @param value | T
     * @param fileName | String
     */
    fun <T> putAValue(
        key: String,
        value: T,
        fileName: String = KEY_FILE_NAME_DEFAULT,
        context: (() -> Context?)? = null
    ) {

        getEditor(
            fileName,
            context?.invoke() ?: MyApplication.getApplicationContext()
        )?.let { editor ->
            when (value) {
                is String -> {
                    editor.putString(key, value)
                }
                is Boolean -> {
                    editor.putBoolean(key, value)
                }
                is Int -> {
                    editor.putInt(key, value)
                }
                is Long -> {
                    editor.putLong(key, value)
                }
                is Float -> {
                    editor.putFloat(key, value)
                }
                is Enum<*> -> {
                    editor.putString(key, value.name)
                }
            }
            editor.apply()
        }

    }

    /**
     * get data from the SP file what you had saved.
     * @param key | String
     * @param defaultValue | T
     * @param fileName | String
     */
    fun <T> getAValue(
        key: String,
        defaultValue: T,
        fileName: String = KEY_FILE_NAME_DEFAULT
    ): Comparable<*>? {

        val sharedPreference = MyApplication.getApplicationContext()
            ?.getSharedPreferences("sp_$fileName", Context.MODE_PRIVATE)
        sharedPreference?.let { preference ->
            return when (defaultValue) {
                is String -> {
                    preference.getString(key, defaultValue)
                }
                is Boolean -> {
                    preference.getBoolean(key, defaultValue)
                }
                is Int -> {
                    preference.getInt(key, defaultValue)
                }
                is Long -> {
                    preference.getLong(key, defaultValue)
                }
                is Float -> {
                    preference.getFloat(key, defaultValue)
                }
                is Enum<*> -> {
                    preference.getString(key, defaultValue.name)
                }
                else -> {
                    throw IllegalArgumentException("This type of data cannot be saved!")
                }
            }
        }
        return null

    }

    /**
     * remove a data from the SP file what you had saved.
     * @param key | String
     * @param fileName | String
     */
    fun remove(
        key: String,
        fileName: String = KEY_FILE_NAME_DEFAULT,
        context: (() -> Context?)? = null
    ) {
        getEditor(fileName, context?.invoke() ?: MyApplication.getApplicationContext())?.remove(key)
    }

    /**
     * clear all data in the SP file that you had saved into.
     * @param fileName | String
     */
    fun clear(fileName: String = KEY_FILE_NAME_DEFAULT, context: (() -> Context?)? = null) {
        getEditor(fileName, context?.invoke() ?: MyApplication.getApplicationContext())?.clear()
    }

    /**
     * inner function for supporting SP.
     * @param fileName | String
     */
    private fun getEditor(fileName: String, context: Context?): SharedPreferences.Editor? {
        val sharedPreference = context?.applicationContext
            ?.getSharedPreferences("sp_$fileName", Context.MODE_PRIVATE)
        return sharedPreference?.edit()
    }

}

我也是有底线的,感谢您的耐心。

相关文章

网友评论

      本文标题:Android (Kotlin)工具之--简单封装SharedP

      本文链接:https://www.haomeiwen.com/subject/swjulltx.html