Phase 2: Room database (content stored encrypted)
Browse files
app/src/main/java/io/github/abrarul/modularai/data/Db.kt
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package io.github.abrarul.modularai.data
|
| 2 |
+
|
| 3 |
+
import android.content.Context
|
| 4 |
+
import androidx.room.*
|
| 5 |
+
import kotlinx.coroutines.flow.Flow
|
| 6 |
+
|
| 7 |
+
@Entity(tableName = "captures")
|
| 8 |
+
data class Capture(
|
| 9 |
+
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
| 10 |
+
val createdAt: Long = System.currentTimeMillis(),
|
| 11 |
+
val cipher: String // Vault.encrypt(text)
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
@Dao
|
| 15 |
+
interface CaptureDao {
|
| 16 |
+
@Query("SELECT * FROM captures ORDER BY createdAt DESC")
|
| 17 |
+
fun all(): Flow<List<Capture>>
|
| 18 |
+
|
| 19 |
+
@Insert suspend fun insert(c: Capture): Long
|
| 20 |
+
@Delete suspend fun delete(c: Capture)
|
| 21 |
+
@Query("DELETE FROM captures") suspend fun wipe()
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
@Database(entities = [Capture::class], version = 1, exportSchema = true)
|
| 25 |
+
abstract class AppDb : RoomDatabase() {
|
| 26 |
+
abstract fun captures(): CaptureDao
|
| 27 |
+
|
| 28 |
+
companion object {
|
| 29 |
+
@Volatile private var inst: AppDb? = null
|
| 30 |
+
fun get(ctx: Context): AppDb = inst ?: synchronized(this) {
|
| 31 |
+
inst ?: Room.databaseBuilder(ctx.applicationContext, AppDb::class.java, "modularai.db")
|
| 32 |
+
.build().also { inst = it }
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
}
|