aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt33
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt11
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/LazyResettable.kt51
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt5
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Realm.kt18
5 files changed, 89 insertions, 29 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
new file mode 100644
index 00000000..dc16f6cc
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt
@@ -0,0 +1,33 @@
+package com.pitchedapps.frost.utils
+
+import android.content.Context
+import com.raizlabs.android.dbflow.config.FlowManager
+import com.raizlabs.android.dbflow.kotlinextensions.processInTransactionAsync
+import com.raizlabs.android.dbflow.structure.BaseModel
+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"
+
+ inline fun <reified T : BaseModel> replace(
+ context: Context, dbName: String, type: Class<T>, data: List<T>,
+ crossinline callback: ((successful: Boolean) -> Unit)) {
+ db(dbName).reset(context)
+ data.processInTransactionAsync({
+ t, databaseWrapper ->
+ t.save(databaseWrapper)
+ },
+ Transaction.Success {
+ callback.invoke(true)
+ },
+ Transaction.Error { _, error ->
+ L.e(error.message ?: "DbReplace error")
+ callback.invoke(false)
+ })
+ }
+}
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 0151b0ae..fe1fdc7c 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
@@ -4,16 +4,13 @@ import android.util.Log
import timber.log.Timber
-
/**
* Created by Allan Wang on 2017-05-28.
*/
-class L {
- companion object {
- val TAG = "Frost: %s"
- fun e(s: String) = Timber.e(TAG, s)
- fun d(s: String) = Timber.d(TAG, s)
- }
+object L {
+ val TAG = "Frost: %s"
+ fun e(s: String) = Timber.e(TAG, s)
+ fun d(s: String) = Timber.d(TAG, s)
}
internal class CrashReportingTree : Timber.Tree() {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/LazyResettable.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/LazyResettable.kt
new file mode 100644
index 00000000..c8365f5c
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/LazyResettable.kt
@@ -0,0 +1,51 @@
+package com.pitchedapps.frost.utils
+
+import java.io.Serializable
+import kotlin.reflect.KProperty
+
+/**
+ * Created by Allan Wang on 2017-05-30.
+ *
+ * Lazy delegate that can be invalidated if needed
+ * https://stackoverflow.com/a/37294840/4407321
+ */
+private object UNINITIALIZED
+
+fun <T : Any> lazyResettable(initializer: () -> T): LazyResettable<T> = LazyResettable<T>(initializer)
+
+class LazyResettable<T : Any>(private val initializer: () -> T, lock: Any? = null) : Lazy<T>, Serializable {
+ @Volatile private var _value: Any = UNINITIALIZED
+ private val lock = lock ?: this
+
+ fun invalidate() {
+ _value = UNINITIALIZED
+ }
+
+ override val value: T
+ get() {
+ val _v1 = _value
+ if (_v1 !== UNINITIALIZED)
+ @Suppress("UNCHECKED_CAST")
+ return _v1 as T
+
+ return synchronized(lock) {
+ val _v2 = _value
+ if (_v2 !== UNINITIALIZED) {
+ @Suppress("UNCHECKED_CAST")
+ _v2 as T
+ } else {
+ val typedValue = initializer()
+ _value = typedValue
+ typedValue
+ }
+ }
+ }
+
+ override fun isInitialized(): Boolean = _value !== UNINITIALIZED
+
+ override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."
+
+ operator fun setValue(any: Any, property: KProperty<*>, t: T) {
+ _value = t
+ }
+} \ No newline at end of file
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 ad222145..710da845 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
@@ -17,6 +17,7 @@ object Prefs {
lateinit private var c: Context
operator fun invoke(c: Context) {
this.c = c
+ lastActive = 0
}
private val sp: SharedPreferences by lazy { c.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE) }
@@ -25,10 +26,6 @@ object Prefs {
get() = sp.getLong(LAST_ACTIVE, -1)
set(value) = set(LAST_ACTIVE, System.currentTimeMillis())
- init {
- lastActive = 0
- }
-
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()
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Realm.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Realm.kt
deleted file mode 100644
index 1eded8c9..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Realm.kt
+++ /dev/null
@@ -1,18 +0,0 @@
-package com.pitchedapps.frost.utils
-
-import io.realm.Realm
-import io.realm.RealmConfiguration
-
-/**
- * Created by Allan Wang on 2017-05-29.
- */
-@JvmOverloads fun realm(name: String = RealmFiles.main, transaction: Realm.Transaction) {
- val realm = Realm.getInstance(RealmConfiguration.Builder().name(name).build())
- realm.executeTransaction(transaction)
- realm.close()
-}
-
-object RealmFiles {
- val main = "frost.realm"
- val TABS = "tabs.realm"
-} \ No newline at end of file