[db] make repo icon and name localizable

One of those last minute requests for changing index structure...

We don't bother with making a new table for localizable repo icons. They just get serialized into the DB. If we ever need to update the structure, we can consider wiping the icons. They can get updated with a full index update.
This commit is contained in:
Torsten Grote
2022-05-04 10:50:52 -03:00
committed by Michael Pöhn
parent 28df05c2c1
commit aecff91dda
5 changed files with 26 additions and 42 deletions

View File

@@ -25,6 +25,16 @@ internal object Converters {
return text?.let { json.encodeToString(localizedTextV2Serializer, it) }
}
@TypeConverter
fun fromStringToLocalizedFileV2(value: String?): LocalizedFileV2? {
return value?.let { json.decodeFromString(localizedFileV2Serializer, it) }
}
@TypeConverter
fun localizedFileV2toString(file: LocalizedFileV2?): String? {
return file?.let { json.encodeToString(localizedFileV2Serializer, it) }
}
@TypeConverter
fun fromStringToMapOfLocalizedTextV2(value: String?): Map<String, LocalizedTextV2>? {
return value?.let { json.decodeFromString(mapOfLocalizedTextV2Serializer, it) }

View File

@@ -16,7 +16,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@Database(
version = 4, // TODO set version to 1 before release and wipe old schemas
version = 5, // TODO set version to 1 before release and wipe old schemas
entities = [
// repo
CoreRepository::class,

View File

@@ -11,6 +11,7 @@ import org.fdroid.index.IndexUtils.getFingerprint
import org.fdroid.index.v2.AntiFeatureV2
import org.fdroid.index.v2.CategoryV2
import org.fdroid.index.v2.FileV2
import org.fdroid.index.v2.LocalizedFileV2
import org.fdroid.index.v2.LocalizedTextV2
import org.fdroid.index.v2.MirrorV2
import org.fdroid.index.v2.ReleaseChannelV2
@@ -19,12 +20,13 @@ import org.fdroid.index.v2.RepoV2
@Entity
public data class CoreRepository(
@PrimaryKey(autoGenerate = true) val repoId: Long = 0,
val name: String,
@Embedded(prefix = "icon_") val icon: FileV2?,
val name: LocalizedTextV2 = emptyMap(),
val icon: LocalizedFileV2?,
val address: String,
val webBaseUrl: String? = null,
val timestamp: Long,
val version: Int?,
val maxAge: Int?,
val description: LocalizedTextV2 = emptyMap(),
val certificate: String?,
)
@@ -41,6 +43,7 @@ internal fun RepoV2.toCoreRepository(
webBaseUrl = webBaseUrl,
timestamp = timestamp,
version = version,
maxAge = null,
description = description,
certificate = certificate,
)
@@ -74,13 +77,13 @@ public data class Repository(
internal val preferences: RepositoryPreferences,
) {
val repoId: Long get() = repository.repoId
val name: String get() = repository.name
val icon: FileV2? get() = repository.icon
internal val name: LocalizedTextV2 get() = repository.name
internal val icon: LocalizedFileV2? get() = repository.icon
val address: String get() = repository.address
val webBaseUrl: String? get() = repository.webBaseUrl
val timestamp: Long get() = repository.timestamp
val version: Int get() = repository.version ?: 0
val description: LocalizedTextV2 get() = repository.description
internal val description: LocalizedTextV2 get() = repository.description
val certificate: String? get() = repository.certificate
val weight: Int get() = preferences.weight
@@ -120,8 +123,11 @@ public data class Repository(
} else emptyList()
}
public fun getName(localeList: LocaleListCompat): String? = name.getBestLocale(localeList)
public fun getDescription(localeList: LocaleListCompat): String? =
description.getBestLocale(localeList)
public fun getIcon(localeList: LocaleListCompat): FileV2? = icon.getBestLocale(localeList)
}
@Entity(

View File

@@ -89,11 +89,12 @@ internal interface RepositoryDaoInt : RepositoryDao {
@Transaction
override fun insert(initialRepo: InitialRepository) {
val repo = CoreRepository(
name = initialRepo.name,
name = mapOf("en-US" to initialRepo.name),
address = initialRepo.address,
icon = null,
timestamp = -1,
version = initialRepo.version,
maxAge = null,
description = mapOf("en-US" to initialRepo.description),
certificate = initialRepo.certificate,
)
@@ -114,11 +115,12 @@ internal interface RepositoryDaoInt : RepositoryDao {
password: String?,
): Long {
val repo = CoreRepository(
name = address,
name = mapOf("en-US" to address),
icon = null,
address = address,
timestamp = System.currentTimeMillis(),
version = null,
maxAge = null,
certificate = null,
)
val repoId = insertOrReplace(repo)

View File

@@ -1,34 +0,0 @@
package org.fdroid.database
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import org.fdroid.index.v2.ReflectionDiffer.applyDiff
import org.fdroid.test.TestRepoUtils.getRandomFileV2
import org.fdroid.test.TestRepoUtils.getRandomRepo
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
internal class ReflectionTest {
@Test
fun testRepository() {
val repo = getRandomRepo().toCoreRepository(version = 42)
val icon = getRandomFileV2()
val description = if (Random.nextBoolean()) mapOf("de" to null, "en" to "foo") else null
val json = """
{
"name": "test",
"timestamp": ${Long.MAX_VALUE},
"icon": ${Json.encodeToString(icon)},
"description": ${Json.encodeToString(description)}
}
""".trimIndent()
val diff = Json.parseToJsonElement(json).jsonObject
val diffed = applyDiff(repo, diff)
println(diffed)
assertEquals(Long.MAX_VALUE, diffed.timestamp)
}
}