PrefUtils

PHOTO EMBED

Wed Mar 30 2022 05:05:19 GMT+0000 (UTC)

Saved by @thiennt8 #android #kotlin

import androidx.preference.PreferenceManager
import com.delfi.area51.ktstart.KtApp

open class PrefUtils {

    companion object {
        private fun getSharePreferences() =
            PreferenceManager.getDefaultSharedPreferences(KtApp.getInstance())

        private fun getEditor() = getSharePreferences().edit()

        fun getString(key: String) = getSharePreferences().getString(key, "")
        fun setString(key: String, value: String) = getEditor().putString(key, value).apply()

        fun getLong(key: String) = getSharePreferences().getLong(key, 0)
        fun setLong(key: String, value: Long) = getEditor().putLong(key, value).apply()

        fun getInt(key: String): Int = getSharePreferences().getInt(key, 0)
        fun setInt(key: String, value: Int) = getEditor().putInt(key, value).apply()
        
        fun getBoolean(key: String): Boolean = getSharePreferences().getBoolean(key, false)
        fun setBoolean(key: String, value: Boolean) = getEditor().putBoolean(key, value).apply()
    }
}
content_copyCOPY