aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils
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
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')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt25
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/GlideUtils.kt26
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt37
4 files changed, 57 insertions, 33 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt
deleted file mode 100644
index 32d232b7..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt
+++ /dev/null
@@ -1,25 +0,0 @@
-package com.pitchedapps.frost.utils
-
-import android.content.Context
-import com.raizlabs.android.dbflow.config.FlowManager
-import com.raizlabs.android.dbflow.kotlinextensions.*
-import com.raizlabs.android.dbflow.structure.database.transaction.FastStoreModelTransaction
-import com.raizlabs.android.dbflow.structure.database.transaction.Transaction
-
-/**
- * Created by Allan Wang on 2017-05-30.
- */
-
-object DbUtils {
-
- fun db(name: String) = FlowManager.getDatabase(name)
- fun dbName(name: String) = "$name.db"
- fun deleteDatabase(c: Context, name: String) = c.deleteDatabase(dbName(name))
-
-}
-
-inline fun <reified T : Any> List<T>.replace(context: Context, dbName: String) {
- L.d("Replacing $dbName.db")
- DbUtils.db(dbName).reset(context)
- FastStoreModelTransaction.saveBuilder(FlowManager.getModelAdapter(T::class.java)).addAll(this).build()
-} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/GlideUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/GlideUtils.kt
new file mode 100644
index 00000000..6fbcced1
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/GlideUtils.kt
@@ -0,0 +1,26 @@
+package com.pitchedapps.frost.utils
+
+import android.content.Context
+import com.bumptech.glide.Glide
+
+/**
+ * Created by Allan Wang on 2017-05-31.
+ */
+object GlideUtils {
+
+ lateinit var applicationContext: Context
+
+ operator fun invoke(applicationContext: Context) {
+ this.applicationContext = applicationContext
+ }
+
+ fun downloadForLater(url: String) {
+ Glide.with(applicationContext).download(url)
+ }
+
+ fun downloadProfile(id: Long) {
+ L.d("Downloading profile photo")
+ downloadForLater("http://graph.facebook.com/$id/picture?type=large")
+ }
+
+} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
index fe1fdc7c..49cc2f9b 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
@@ -8,7 +8,7 @@ import timber.log.Timber
* Created by Allan Wang on 2017-05-28.
*/
object L {
- val TAG = "Frost: %s"
+ const val TAG = "Frost: %s"
fun e(s: String) = Timber.e(TAG, s)
fun d(s: String) = Timber.d(TAG, s)
}
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
+ }
+}