diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt index 52a75929..d99e8417 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt @@ -5,6 +5,8 @@ import android.content.SharedPreferences /** * Created by Allan Wang on 2017-05-28. + * + * Shared Preference object with lazy cached retrievals */ private val PREFERENCE_NAME = "${com.pitchedapps.frost.BuildConfig.APPLICATION_ID}.prefs" @@ -13,6 +15,8 @@ private val USER_ID = "user_id" object Prefs { + private const val prefDefaultLong = -2L + lateinit private var c: Context operator fun invoke(c: Context) { this.c = c @@ -21,17 +25,36 @@ object Prefs { private val sp: SharedPreferences by lazy { c.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE) } - var lastActive: Long - get() = sp.getLong(LAST_ACTIVE, -1) - set(value) = set(LAST_ACTIVE, System.currentTimeMillis()) + var lastActive: Long = prefDefaultLong + get() { + if (field == prefDefaultLong) field = sp.getLong(LAST_ACTIVE, -1) + return field + } + set(value) { + field = value + if (value != prefDefaultLong) set(LAST_ACTIVE, System.currentTimeMillis()) + } const val userIdDefault = -1L - var userId: Long - get() = sp.getLong(USER_ID, userIdDefault) - set(value) = set(USER_ID, value) + var userId: Long = prefDefaultLong + get() { + if (field == prefDefaultLong) field = sp.getLong(USER_ID, userIdDefault) + return field + } + set(value) { + field = value + if (value != prefDefaultLong) set(USER_ID, value) + } private fun set(key: String, value: Boolean) = sp.edit().putBoolean(key, value).apply() private fun set(key: String, value: Int) = sp.edit().putInt(key, value).apply() private fun set(key: String, value: Long) = sp.edit().putLong(key, value).apply() private fun set(key: String, value: String) = sp.edit().putString(key, value).apply() -}
\ No newline at end of file + + fun clear() { + L.d("Clearing Prefs") + sp.edit().clear().apply() + lastActive = prefDefaultLong + userId = prefDefaultLong + } +} |