Skip to content

Instantly share code, notes, and snippets.

@mauchiyuyo
Forked from LutfiTekin/LocalData.kt
Created April 28, 2021 01:08
Show Gist options
  • Select an option

  • Save mauchiyuyo/33b9fd4961f15badbb5a1d2a449be4ad to your computer and use it in GitHub Desktop.

Select an option

Save mauchiyuyo/33b9fd4961f15badbb5a1d2a449be4ad to your computer and use it in GitHub Desktop.

Revisions

  1. @LutfiTekin LutfiTekin revised this gist Jul 1, 2018. 1 changed file with 1 addition and 18 deletions.
    19 changes: 1 addition & 18 deletions LocalData.kt
    Original 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
    import java.lang.reflect.Type


    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()
    }

    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
    @@ -84,12 +73,6 @@ class LocalData(val context: Context) {
    return sharedPreferences.getBoolean(title, def)
    }








    /**
    * Returns true for specific action only once
  2. @LutfiTekin LutfiTekin revised this gist Jul 1, 2018. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions LocalData.kt
    Original 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 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"
    @@ -25,10 +22,7 @@ class LocalData(val context: Context) {
    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
  3. @LutfiTekin LutfiTekin revised this gist Jul 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LocalData.kt
    Original 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"
    const val ENCOUNTER = "encounteredusers"

    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")
    }
    }
  4. @LutfiTekin LutfiTekin created this gist Jul 1, 2018.
    127 changes: 127 additions & 0 deletions LocalData.kt
    Original 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)
    }
    }
    }