Skip to content

Instantly share code, notes, and snippets.

@CompileConnected
Created September 4, 2020 17:57
Show Gist options
  • Select an option

  • Save CompileConnected/3a3465f8874ad92c7953d1f63c9238f1 to your computer and use it in GitHub Desktop.

Select an option

Save CompileConnected/3a3465f8874ad92c7953d1f63c9238f1 to your computer and use it in GitHub Desktop.
A wrapper to shared preference that has already gone to big
class AppStorage : CoreLocalStorage.Child {
private lateinit var parent: CoreLocalStorage
companion object {
private const val FOS = "fos"
private const val ROH = "roh"
private const val DAA = "daa"
}
override fun setParent(parent: CoreLocalStorage) {
this.parent = parent
}
/**
* if you want the variable is shared public use parent.public
* if not use parent.app
*/
var fos: String?
get() = parent.app[FOS] //how to achieve this? use shared prefernce extensions then import it this code
set(value) {
parent.app[FOS] = value
}
var roh: Boolean
get() = parent.app[ROH] ?: false
set(value) {
parent.app[ROH] = value
}
var daa: String?
get() = parent.app[daa]
set(value) {
parent.app[daa] = value
}
override fun clear() {
this.parent.app.edit()
.remove(FOS)
.remove(ROH)
.remove(DAA)
.apply()
}
}
import android.content.Context
import android.content.SharedPreferences
interface CoreLocalStorage {
val public: SharedPreferences
val app: SharedPreferences
fun <T: Child> adapter(extend: T): T
fun <T: Child> clearAdapterData(extend: T)
fun clearAllData()
interface Child {
fun setParent(parent: CoreLocalStorage)
fun clear()
}
/**
* you can improve this with your favorite injection library
*/
companion object {
@Volatile private var _instance: CoreLocalStorage? = null
fun createInstance(context: Context): CoreLocalStorage = _instance ?: synchronized(this) {
_instance ?: CoreLocalStorageImpl(context).also { _instance = it }
}
fun getInstance() = _instance
}
}
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager //it will deprecated use androidx.preference:preference:1.1.0 or higher
import com.google.gson.Gson
class CoreLocalStorageImpl(private val context: Context) : CoreLocalStorage {
override val public: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(context)
override val app: SharedPreferences
get() = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE)
override fun clearAllData() {
app.edit().clear().apply()
public.edit().clear().apply()
}
override fun <T : CoreLocalStorage.Child> clearAdapterData(extend: T) {
extend.clear()
}
override fun <T : CoreLocalStorage.Child> adapter(extend: T): T {
extend.setParent(this)
return extend
}
}
//first you need to createInstance in Application class, to make the singleton
//then you can use whenever in app, like this
CoreLocalStorage.getInstance()
?.adapter(AppStorage())
?.FOS = "Hello world"
CoreLocalStorage.getInstance()
?.adapter(AppStorage())
?.also { storage ->
storage.FOS = "Hello world"
storage.ROH = "false"
storage.DAA = "DAAAAAAAA"
}
CoreLocalStorage.getInstance()?.app!!["fos"] = "dem"
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = this.edit()
operation(editor)
editor.apply()
}
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")
}
}
inline operator 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")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment