Last active
April 14, 2021 19:47
-
-
Save ketzalv/8b9dfc6b813d60eb20291e02cb8cb5ab to your computer and use it in GitHub Desktop.
This file contains 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.SharedPreferences | |
import androidx.preference.PreferenceManager | |
/** | |
* @author ketzalv | |
* @version 1.0 | |
* Need add library this dependency to gradle | |
* { implementation 'androidx.preference:preference-ktx:1.1.1' } | |
*/ | |
object Preferences { | |
private val preferences: SharedPreferences = | |
PreferenceManager.getDefaultSharedPreferences(context)//general Context of app example App.instance | |
private fun saveData(key: String, data: String) { | |
val editor = preferences.edit() | |
editor.putString(key, data) | |
editor.apply() | |
} | |
private fun saveDataBool(key: String?, data: Boolean) { | |
val editor = preferences.edit() | |
editor.putBoolean(key, data) | |
editor.apply() | |
} | |
private fun saveString(key: String, data: String) { | |
val editor = preferences.edit() | |
editor.putString(key, data) | |
editor.apply() | |
} | |
private fun saveLong(key: String, data: Long) { | |
val editor = preferences.edit() | |
editor.putLong(key, data) | |
editor.apply() | |
} | |
private fun containsData(key: String?): Boolean { | |
return preferences.contains(key) | |
} | |
private fun loadBoolean(key: String?): Boolean { | |
return preferences.getBoolean(key, false) | |
} | |
private fun loadLong(key: String?): Long { | |
return preferences.getLong(key, 0) | |
} | |
private fun loadString(key: String?): String? { | |
return preferences.getString(key, "") | |
} | |
private fun clearPreferences() { | |
val editor = preferences.edit() | |
editor.clear() | |
editor.apply() | |
} | |
private fun clearPreference(key: String?) { | |
val editor = preferences.edit() | |
editor.remove(key) | |
editor.apply() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment