-
-
Save mauchiyuyo/33b9fd4961f15badbb5a1d2a449be4ad to your computer and use it in GitHub Desktop.
Revisions
-
LutfiTekin revised this gist
Jul 1, 2018 . 1 changed file with 1 addition and 18 deletions.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 @@ -3,7 +3,7 @@ import android.content.Context import android.content.SharedPreferences import android.util.Log private const val SHAREDPREF = "shared" private const val FIRST_TIME = "firstime" @@ -42,17 +42,6 @@ class LocalData(val context: Context) { editor.putBoolean(title, content).apply() } /** * Read value from shared prefs @@ -84,12 +73,6 @@ class LocalData(val context: Context) { return sharedPreferences.getBoolean(title, def) } /** * Returns true for specific action only once -
LutfiTekin revised this gist
Jul 1, 2018 . 1 changed file with 1 addition and 7 deletions.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 @@ -3,9 +3,6 @@ import android.content.Context import android.content.SharedPreferences import android.util.Log import java.lang.reflect.Type private const val SHAREDPREF = "shared" @@ -25,10 +22,7 @@ class LocalData(val context: Context) { get() { return context.applicationContext.getSharedPreferences(FIRST_TIME, Context.MODE_PRIVATE) } /** * Save a key value pair to shared pref -
LutfiTekin revised this gist
Jul 1, 2018 . 1 changed file with 1 addition and 1 deletion.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 @@ -10,7 +10,6 @@ import java.lang.reflect.Type private const val SHAREDPREF = "shared" private const val FIRST_TIME = "firstime" class LocalData(val context: Context) { @@ -123,5 +122,6 @@ class LocalData(val context: Context) { fun with(context: Context): LocalData{ return LocalData(context) } //To use it like LocalData.with(context).read("data") } } -
LutfiTekin created this gist
Jul 1, 2018 .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,127 @@ import android.content.Context import android.content.SharedPreferences import android.util.Log import com.google.firebase.auth.FirebaseAuth import com.google.gson.Gson import com.google.gson.reflect.TypeToken import java.lang.reflect.Type private const val SHAREDPREF = "shared" private const val FIRST_TIME = "firstime" const val ENCOUNTER = "encounteredusers" class LocalData(val context: Context) { private val sharedPreferences: SharedPreferences get() { return context.applicationContext.getSharedPreferences(SHAREDPREF, Context.MODE_PRIVATE) } private val editor: SharedPreferences.Editor get() { return sharedPreferences.edit() } private val firstTimeSharedPreferences: SharedPreferences get() { return context.applicationContext.getSharedPreferences(FIRST_TIME, Context.MODE_PRIVATE) } private val username: String? get() { return FirebaseAuth.getInstance().currentUser?.displayName } /** * Save a key value pair to shared pref * @param title * @param content */ fun write(title: String, content: String?) { editor.putString(title, content).apply() } /** * Save a key value pair to shared pref * @param title * @param content */ fun write(title: String, content: Boolean) { editor.putBoolean(title, content).apply() } fun writeObject(title: String, content: Any){ val string = Gson().toJson(content) editor.putString(title,string) } fun readObject(title: String, type: Type): Any?{ val data = read(title) return Gson().fromJson(data,type) } /** * Read value from shared prefs * @param title * @return */ fun read(title: String): String? { return sharedPreferences.getString(title, null) } /** * Read value from shared prefs * @param title * @return */ fun read(title: String, def: String): String { return sharedPreferences.getString(title, def) } /** * Read value from shared prefs * @param title * @return * @throws NullPointerException */ fun read(title: String, def: Boolean): Boolean { val sharedPreferences = context.applicationContext.getSharedPreferences(SHAREDPREF, Context.MODE_PRIVATE) return sharedPreferences.getBoolean(title, def) } /** * Returns true for specific action only once * @param action String key for action * @return boolean true if not called before, false afterwards */ infix fun isFirsTimeOf(action: String): Boolean { if (firstTimeSharedPreferences.getBoolean(action, true)) { firstTimeSharedPreferences.edit().putBoolean(action, false).apply() return true } return false } /** * Remove all data associated with the SharedPref key */ fun burnEverything() { context.applicationContext.getSharedPreferences(SHAREDPREF, Context.MODE_PRIVATE).edit().clear().apply() context.applicationContext.getSharedPreferences(FIRST_TIME, Context.MODE_PRIVATE).edit().clear().apply() } companion object { fun with(context: Context): LocalData{ return LocalData(context) } } }