aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/db
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-03-07 03:12:27 -0500
committerAllan Wang <me@allanwang.ca>2019-03-07 03:12:27 -0500
commiteabc8e3393cbd6b3dba5c70a5920075b8158d84e (patch)
tree32b8add65065b113e4952cb123c7a5be96181fad /app/src/main/kotlin/com/pitchedapps/frost/db
parentf717953debf1cd023b2f612e0ea9b2eede4c37c3 (diff)
downloadfrost-eabc8e3393cbd6b3dba5c70a5920075b8158d84e.tar.gz
frost-eabc8e3393cbd6b3dba5c70a5920075b8158d84e.tar.bz2
frost-eabc8e3393cbd6b3dba5c70a5920075b8158d84e.zip
Add cache entityt
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/db')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt56
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt3
2 files changed, 58 insertions, 1 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
new file mode 100644
index 00000000..bd6bff4b
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/CacheDb.kt
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.pitchedapps.frost.db
+
+import android.os.Parcelable
+import androidx.room.Dao
+import androidx.room.Entity
+import androidx.room.Insert
+import androidx.room.OnConflictStrategy
+import androidx.room.Query
+import kotlinx.android.parcel.Parcelize
+
+/**
+ * Created by Allan Wang on 2017-05-30.
+ */
+
+/**
+ * Generic cache to store serialized content
+ */
+@Entity(tableName = "frost_cache")
+@Parcelize
+data class CacheEntity(
+ @androidx.room.PrimaryKey
+ val id: String,
+ val lastUpdated: Long,
+ val contents: String
+) : Parcelable
+
+@Dao
+interface CacheDao {
+
+ @Query("SELECT * FROM frost_cache WHERE id = :id")
+ suspend fun selectById(id: Long): CacheEntity?
+
+ @Insert(onConflict = OnConflictStrategy.REPLACE)
+ suspend fun insertCache(cache: CacheEntity)
+
+ @Query("DELETE FROM frost_cache WHERE id = :id")
+ suspend fun deleteById(id: Long)
+}
+
+suspend fun CacheDao.save(id: String, contents: String) = insertCache(CacheEntity(id, System.currentTimeMillis(), contents)) \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
index 1ce9e7c2..b83fce52 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/db/Database.kt
@@ -11,10 +11,11 @@ import org.koin.standalone.StandAloneContext
interface FrostPrivateDao {
fun cookieDao(): CookieDao
fun notifDao(): NotificationDao
+ fun cacheDao(): CacheDao
}
@Database(
- entities = [CookieEntity::class, NotificationEntity::class],
+ entities = [CookieEntity::class, NotificationEntity::class, CacheEntity::class],
version = 1,
exportSchema = true
)