Database: use a simpler method for identity usages

This commit is contained in:
dece 2022-02-04 14:58:40 +01:00
parent 11bea0e585
commit 9a91c72d1e
3 changed files with 25 additions and 48 deletions

View file

@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "70da3095877de4a82021855471523b90",
"identityHash": "ffa6a7f7cce2d67541e0fbe28441e780",
"entities": [
{
"tableName": "HistoryEntry",
@ -38,7 +38,7 @@
},
{
"tableName": "Identity",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `key` TEXT NOT NULL, `name` TEXT)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `key` TEXT NOT NULL, `name` TEXT, `urls` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
@ -57,38 +57,12 @@
"columnName": "name",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "IdentityUsage",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `uri` TEXT NOT NULL, `identityId` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "uri",
"columnName": "uri",
"fieldPath": "urls",
"columnName": "urls",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "identityId",
"columnName": "identityId",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
@ -104,7 +78,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '70da3095877de4a82021855471523b90')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'ffa6a7f7cce2d67541e0fbe28441e780')"
]
}
}

View file

@ -1,18 +1,18 @@
package dev.lowrespalmtree.comet
import android.content.Context
import android.util.Base64
import androidx.room.*
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(
entities = [
History.HistoryEntry::class,
Identities.Identity::class,
Identities.IdentityUsage::class,
],
version = 1
)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun historyEntryDao(): History.HistoryEntryDao
abstract fun identityDao(): Identities.IdentityDao
@ -26,4 +26,18 @@ object Database {
return
INSTANCE = Room.databaseBuilder(context, AppDatabase::class.java, "comet.db").build()
}
}
typealias UrlList = ArrayList<String>
class Converters {
@TypeConverter
fun fromUrlList(value: UrlList?): String? =
value?.joinToString("-") {
Base64.encodeToString(it.encodeToByteArray(), Base64.DEFAULT)
}
@TypeConverter
fun stringToUrlList(value: String?): UrlList? =
value?.split("-")?.map { Base64.decode(it, Base64.DEFAULT).decodeToString() } as UrlList?
}

View file

@ -16,16 +16,8 @@ object Identities {
val key: String,
/** Label for this identity. */
var name: String?,
)
@Entity
data class IdentityUsage(
/** ID. */
@PrimaryKey(autoGenerate = true) val id: Int,
/** URL path where an identity can be used. */
val uri: String,
/** ID of the Identity to use. */
val identityId: Int
/** URL paths configured to use this identity. */
var urls: UrlList
)
@Dao
@ -42,15 +34,12 @@ object Identities {
@Update
suspend fun update(vararg identities: Identity)
@Query("SELECT * FROM IdentityUsage WHERE :identityId = identityId")
suspend fun getUsagesFor(identityId: Int): List<IdentityUsage>
@Delete
suspend fun delete(vararg identities: Identity)
}
suspend fun insert(key: String, name: String? = null): Long =
Database.INSTANCE.identityDao().insert(Identity(0, key, name))
Database.INSTANCE.identityDao().insert(Identity(0, key, name, arrayListOf()))
suspend fun get(id: Long): Identity? =
Database.INSTANCE.identityDao().get(id)