aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/db
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-03-07 14:59:40 -0500
committerAllan Wang <me@allanwang.ca>2019-03-07 14:59:40 -0500
commit12f491737ec2e2d774a816e84170ff352d1b6cd6 (patch)
treef61b20f5ba7630fecef50976733e26fbc4ef5a52 /app/src/main/kotlin/com/pitchedapps/frost/db
parentf1878133d8af686ce8c27acffe28f26e9dda5165 (diff)
downloadfrost-12f491737ec2e2d774a816e84170ff352d1b6cd6.tar.gz
frost-12f491737ec2e2d774a816e84170ff352d1b6cd6.tar.bz2
frost-12f491737ec2e2d774a816e84170ff352d1b6cd6.zip
Add cache test
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/db')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt38
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt2
2 files changed, 30 insertions, 10 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt
index 4d6bc938..8c3c9c6b 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt
@@ -19,9 +19,11 @@ package com.pitchedapps.frost.db
import android.os.Parcelable
import androidx.room.Dao
import androidx.room.Entity
+import androidx.room.ForeignKey
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
+import com.pitchedapps.frost.utils.L
import kotlinx.android.parcel.Parcelize
/**
@@ -31,11 +33,20 @@ import kotlinx.android.parcel.Parcelize
/**
* Generic cache to store serialized content
*/
-@Entity(tableName = "frost_cache")
+@Entity(
+ tableName = "frost_cache",
+ primaryKeys = ["id", "type"],
+ foreignKeys = [ForeignKey(
+ entity = CookieEntity::class,
+ parentColumns = ["cookie_id"],
+ childColumns = ["id"],
+ onDelete = ForeignKey.CASCADE
+ )]
+)
@Parcelize
data class CacheEntity(
- @androidx.room.PrimaryKey
- val id: String,
+ val id: Long,
+ val type: String,
val lastUpdated: Long,
val contents: String
) : Parcelable
@@ -43,15 +54,24 @@ data class CacheEntity(
@Dao
interface CacheDao {
- @Query("SELECT * FROM frost_cache WHERE id = :id")
- suspend fun selectById(id: Long): CacheEntity?
+ @Query("SELECT * FROM frost_cache WHERE id = :id AND type = :type")
+ suspend fun select(id: Long, type: String): CacheEntity?
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertCache(cache: CacheEntity)
- @Query("DELETE FROM frost_cache WHERE id = :id")
- suspend fun deleteById(id: Long)
+ @Query("DELETE FROM frost_cache WHERE id = :id AND type = :type")
+ suspend fun delete(id: Long, type: String)
}
-suspend fun CacheDao.save(id: String, contents: String) =
- insertCache(CacheEntity(id, System.currentTimeMillis(), contents)) \ No newline at end of file
+/**
+ * Returns true if successful, given that there are constraints to the insertion
+ */
+suspend fun CacheDao.save(id: Long, type: String, contents: String): Boolean =
+ try {
+ insertCache(CacheEntity(id, type, System.currentTimeMillis(), contents))
+ true
+ } catch (e: Exception) {
+ L.e(e) { "Cache save failed for $type" }
+ false
+ } \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt
index 7f41fbf8..d2771754 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/NotificationDb.kt
@@ -146,7 +146,7 @@ suspend fun NotificationDao.saveNotifications(type: String, notifs: List<Notific
_saveNotifications(type, notifs)
true
} catch (e: Exception) {
- L.e(e) { "Notif save failed" }
+ L.e(e) { "Notif save failed for $type" }
false
}
}