Skip to content

Instantly share code, notes, and snippets.

@shafty023
Last active June 10, 2020 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shafty023/cd04db60d0cb7e7ea267ffff495cf959 to your computer and use it in GitHub Desktop.
Save shafty023/cd04db60d0cb7e7ea267ffff495cf959 to your computer and use it in GitHub Desktop.
Messy preferences
@Singleton
class Preferences(context: Context) {
private val prefs: SharedPreferences = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE)
companion object {
const val PREF_CURRENT_STATE = "CurrentState"
const val PREF_LAST_KNOWN_ERROR = "LastKnownError"
const val PREF_LAST_ACTION_PERFORMED = "LastActionPerformed"
}
fun getCurrentState() = prefs.getInt(PREF_CURRENT_STATE, 0)
fun setCurrentState(currentState: Int) {
prefs.edit().putInt(PREF_CURRENT_STATE, currentState).apply()
}
fun getLastKnownError() = prefs.getString(PREF_LAST_KNOWN_ERROR, "")
fun setLastKnownError(lastKnownError: String) {
prefs.edit().putString(PREF_LAST_KNOWN_ERROR, lastKnownError).apply()
}
fun getLastActionPerformed() = prefs.getString(PREF_LAST_ACTION_PERFORMED, "")
fun setLastActionPerformed(lastAction: String) {
prefs.edit().putString(PREF_LAST_ACTION_PERFORMED, lastAction).apply()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment