Skip to content

Instantly share code, notes, and snippets.

@albaspazio
Last active December 19, 2020 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albaspazio/97cb957d8909841efc3095889ca0a54a to your computer and use it in GitHub Desktop.
Save albaspazio/97cb957d8909841efc3095889ca0a54a to your computer and use it in GitHub Desktop.
Singleton SharedPreferences manager. Is based on general abstract class (SharedPreferenceWrapper) and a project-specific Singleton (ProjectPreferences)
import android.content.Context
import android.content.res.Resources
import androidx.preference.PreferenceManager
import org.albspazio.myproject.R
import org.albaspazio.core.sharedpreferences.SharedPreferenceWrapper
// this must be implemented for a specific project
object ProjectPreferences: SharedPreferenceWrapper() {
// init values of each Preference are stored in a specific resource. associate them!
private val keysHashMap:HashMap<String,Int> = hashMapOf(
"propr1" to R.integer.pref_propr1,
"propr2" to R.string.pref_propr2,)
private lateinit var resources: Resources
//call it once
fun init(context:Context, pref_name:String="", mode:Int = Context.MODE_PRIVATE){
if(isInitialized()) return // prevent multiple init
prefs = if(pref_name.isEmpty()) PreferenceManager.getDefaultSharedPreferences(context)
else context.getSharedPreferences(pref_name, mode)
resources = context.resources
setDefault()
}
override fun read(key: String, value: Any): Any?{
return if(!keysHashMap.contains(key) || !isInitialized()) null
else super.read(key, value)
}
override fun write(key: String, value: Any): Any?{
return if(!keysHashMap.contains(key) || !isInitialized()) null
else super.write(key, value)
}
//==============================================================================================
// if a preference key is still unset, init it taking values from resources
private fun setDefault(){
keysHashMap.map{
if(!prefs.contains(it.key))
when(it.key){
"pref_propr1" -> write(it.key, resources.getInteger(it.value).toString())
"pref_propr2" -> write(it.key, resources.getString(it.value))
}
it
}
}
//==============================================================================================
}
package org.albaspazio.core.sharedpreferences
import android.content.SharedPreferences
// this class is project-independent.
// can be defined in an external module/library
abstract class SharedPreferenceWrapper {
protected lateinit var prefs: SharedPreferences
fun isInitialized() = ::prefs.isInitialized
open fun read(key: String, defvalue: Any): Any? {
return when(defvalue){
is String -> prefs.getString(key, defvalue)
is Int -> prefs.getInt(key, defvalue)
is Boolean -> prefs.getBoolean(key, defvalue)
is Long -> prefs.getLong(key, defvalue)
is Float -> prefs.getFloat(key, defvalue)
is Set<*> -> {
if(defvalue.isNotEmpty() && defvalue.elementAt(0) is String)
prefs.getStringSet(key, defvalue as Set<String>)
else return null
}
else -> null
}
}
open fun write(key: String, value: Any):Any? {
val prefsEditor: SharedPreferences.Editor = prefs.edit()
with(prefsEditor) {
when(value){
is String -> putString(key, value)
is Int -> putInt(key, value)
is Boolean -> putBoolean(key, value)
is Long -> putLong(key, value)
is Float -> putFloat(key, value)
is Set<*> -> {
if(value.isNotEmpty() && value.elementAt(0) is String) putStringSet(key, value as Set<String>)
else return null
}
else -> return null
}
commit()
}
return value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment