Created
April 13, 2021 02:53
-
-
Save MtaufiqH/ec401f7282d46b5c2b0222be3449acc1 to your computer and use it in GitHub Desktop.
Revisions
-
MtaufiqH created this gist
Apr 13, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ // to use it. private val preferencesManager by lazy { SharedPrefreencesManager(context) } private val key_uname = "username" preferencesManager.add(key_uname, "isfaaghyth") ... println(preferencesManager.get(key_name)) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ internal class SharedPrefreencesManager { private var preferences: SharedPreferences? = null fun init(context: Context) { val prefName = "pref_sample" preferences = context.getSharedPreferences( prefName, Context.MODE_PRIVATE ) } inline fun <reified T> add(key: String, obj: T) { val objString = Gson().toJson(obj, T::class.java) preferences?.edit()?.putString(key, objString) } inline fun <reified T> get(key: String): T? { val obj = preferences?.getString(key, null) return Gson().fromJson(obj, T::class.java) } }