aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-05-31 17:11:46 -0700
committerAllan Wang <me@allanwang.ca>2017-05-31 17:11:46 -0700
commit8618670b82641d5fbaec9c333f1290bab429ce27 (patch)
tree737c9a04f108ea68547eef2db1ae6e96caa64df6 /app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
parent9a41937a33539dbfaae4d072361caaec79865c29 (diff)
downloadfrost-8618670b82641d5fbaec9c333f1290bab429ce27.tar.gz
frost-8618670b82641d5fbaec9c333f1290bab429ce27.tar.bz2
frost-8618670b82641d5fbaec9c333f1290bab429ce27.zip
add more cookie handling
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.kt37
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
+ }
+}