From 9a91c72d1e8dd963cdee18230c19fa7a953e0fc5 Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 4 Feb 2022 14:58:40 +0100 Subject: [PATCH] Database: use a simpler method for identity usages --- .../1.json | 36 +++---------------- .../java/dev/lowrespalmtree/comet/Database.kt | 20 +++++++++-- .../dev/lowrespalmtree/comet/Identities.kt | 17 ++------- 3 files changed, 25 insertions(+), 48 deletions(-) diff --git a/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json b/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json index 07172fb..9e36488 100644 --- a/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json +++ b/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json @@ -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')" ] } } \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/Database.kt b/app/src/main/java/dev/lowrespalmtree/comet/Database.kt index 402e362..8e0a999 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/Database.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/Database.kt @@ -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 + +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? } \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/Identities.kt b/app/src/main/java/dev/lowrespalmtree/comet/Identities.kt index 209153e..51cc337 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/Identities.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/Identities.kt @@ -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 - @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)