Created
September 4, 2020 17:57
-
-
Save CompileConnected/3a3465f8874ad92c7953d1f63c9238f1 to your computer and use it in GitHub Desktop.
A wrapper to shared preference that has already gone to big
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment