Add: Migrate antifeatures, donates, licenses & screenshots into from entity into database.entity Product

This commit is contained in:
machiav3lli 2022-02-20 02:22:15 +01:00
parent 3f490c2487
commit 5f923f70dd
6 changed files with 62 additions and 51 deletions

View File

@ -24,6 +24,10 @@ const val ROW_ICON = "icon"
const val ROW_METADATA_ICON = "metadataIcon"
const val ROW_RELEASES = "releases"
const val ROW_CATEGORIES = "categories"
const val ROW_ANTIFEATURES = "categories"
const val ROW_LICENSES = "categories"
const val ROW_DONATES = "categories"
const val ROW_SCREENSHOTS = "categories"
const val ROW_VERSION = "version"
const val ROW_SIGNATURE = "signature"
const val ROW_ID = "_id"

View File

@ -3,6 +3,8 @@ package com.looker.droidify.database
import androidx.room.TypeConverter
import com.looker.droidify.database.entity.Release
import com.looker.droidify.database.entity.Release.Companion.deserializeIncompatibilities
import com.looker.droidify.entity.Donate
import com.looker.droidify.entity.Screenshot
import com.looker.droidify.utility.extension.json.writeDictionary
import com.looker.droidify.utility.jsonGenerate
import com.looker.droidify.utility.jsonParse
@ -73,4 +75,34 @@ object Converters {
}
}
}
@TypeConverter
@JvmStatic
fun toScreenshots(byteArray: ByteArray): List<Screenshot> {
val string = byteArray.toString()
return if (string == "") emptyList()
else string.split(",").mapNotNull { byteArray.jsonParse { Screenshot.deserialize(it) } }
}
@JvmName("screenshotsToByteArray")
@TypeConverter
@JvmStatic
fun toByteArray(screenshots: List<Screenshot>) = jsonGenerate {
screenshots.forEach { item -> item.serialize(it) }.toString().toByteArray()
}
@TypeConverter
@JvmStatic
fun toDonates(byteArray: ByteArray): List<Donate> {
val string = byteArray.toString()
return if (string == "") emptyList()
else string.split(",").mapNotNull { byteArray.jsonParse { Donate.deserialize(it) } }
}
@JvmName("donatesToByteArray")
@TypeConverter
@JvmStatic
fun toByteArray(donates: List<Donate>) = jsonGenerate {
donates.forEach { item -> item.serialize(it) }.toString().toByteArray()
}
}

View File

@ -109,14 +109,15 @@ interface ProductDao : BaseDao<Product> {
product.${ROW_SIGNATURES} != ''"""
// Select the return fields
builder += """SELECT product.rowid AS _id, product.${ROW_REPOSITORY_ID},
product.${ROW_PACKAGE_NAME}, product.${ROW_NAME},
product.${ROW_SUMMARY}, installed.${ROW_VERSION},
(COALESCE(lock.${ROW_VERSION_CODE}, -1) NOT IN (0, product.${ROW_VERSION_CODE}) AND
product.${ROW_COMPATIBLE} != 0 AND product.${ROW_VERSION_CODE} >
COALESCE(installed.${ROW_VERSION_CODE}, 0xffffffff) AND $signatureMatches)
AS ${ROW_CAN_UPDATE}, product.${ROW_COMPATIBLE},
product.${ROW_ICON}, product.${ROW_METADATA_ICON}, product.${ROW_RELEASES}, product.${ROW_CATEGORIES},"""
builder += """SELECT product.rowid AS _id, product.$ROW_REPOSITORY_ID,
product.$ROW_PACKAGE_NAME, product.$ROW_NAME,
product.$ROW_SUMMARY, installed.$ROW_VERSION,
(COALESCE(lock.$ROW_VERSION_CODE, -1) NOT IN (0, product.$ROW_VERSION_CODE) AND
product.$ROW_COMPATIBLE != 0 AND product.$ROW_VERSION_CODE >
COALESCE(installed.$ROW_VERSION_CODE, 0xffffffff) AND $signatureMatches)
AS $ROW_CAN_UPDATE, product.$ROW_COMPATIBLE, product.$ROW_ICON,
product.$ROW_METADATA_ICON, product.$ROW_RELEASES, product.$ROW_CATEGORIES,
product.$ROW_ANTIFEATURES, product.$ROW_LICENSES, product.$ROW_DONATES, product.$ROW_SCREENSHOTS,"""
// Calculate the matching score with the search query
if (searchQuery.isNotEmpty()) {
@ -283,6 +284,10 @@ interface ProductTempDao : BaseDao<ProductTemp> {
metadataIcon = it.metadataIcon
releases = it.releases
categories = it.categories
antiFeatures = it.antiFeatures
licenses = it.licenses
donates = it.donates
screenshots = it.screenshots
}
})
it.categories.forEach { category ->

View File

@ -21,7 +21,7 @@ import kotlinx.coroutines.launch
CategoryTemp::class,
Installed::class,
Lock::class
], version = 3
], version = 4
)
@TypeConverters(Converters::class)
abstract class DatabaseX : RoomDatabase() {

View File

@ -2,7 +2,9 @@ package com.looker.droidify.database.entity
import androidx.room.ColumnInfo
import androidx.room.Entity
import com.looker.droidify.entity.Donate
import com.looker.droidify.entity.ProductItem
import com.looker.droidify.entity.Screenshot
@Entity(tableName = "product", primaryKeys = ["repository_id", "package_name"])
open class Product {
@ -21,6 +23,10 @@ open class Product {
var metadataIcon = ""
var releases: List<Release> = emptyList()
var categories: List<String> = emptyList()
var antiFeatures: List<String> = emptyList()
var licenses: List<String> = emptyList()
var donates: List<Donate> = emptyList()
var screenshots: List<Screenshot> = emptyList()
// TODO Remove in next iteration
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)

View File

@ -130,9 +130,9 @@ data class Product(
generator.writeArray("screenshots") {
screenshots.forEach {
writeDictionary {
writeStringField("locale", it.locale)
writeStringField("type", it.type.jsonName)
writeStringField("path", it.path)
it.serialize(
this
)
}
}
}
@ -199,46 +199,10 @@ data class Product(
it.array("categories") -> categories = collectNotNullStrings()
it.array("antiFeatures") -> antiFeatures = collectNotNullStrings()
it.array("licenses") -> licenses = collectNotNullStrings()
it.array("donates") -> donates = collectNotNull(JsonToken.START_OBJECT) {
var type = ""
var url = ""
var address = ""
var id = ""
forEachKey {
when {
it.string("type") -> type = valueAsString
it.string("url") -> url = valueAsString
it.string("address") -> address = valueAsString
it.string("id") -> id = valueAsString
else -> skipChildren()
}
}
when (type) {
"" -> Donate.Regular(url)
"bitcoin" -> Donate.Bitcoin(address)
"litecoin" -> Donate.Litecoin(address)
"flattr" -> Donate.Flattr(id)
"liberapay" -> Donate.Liberapay(id)
"openCollective" -> Donate.OpenCollective(id)
else -> null
}
}
it.array("donates") -> donates =
collectNotNull(JsonToken.START_OBJECT, Donate::deserialize)
it.array("screenshots") -> screenshots =
collectNotNull(JsonToken.START_OBJECT) {
var locale = ""
var type = ""
var path = ""
forEachKey {
when {
it.string("locale") -> locale = valueAsString
it.string("type") -> type = valueAsString
it.string("path") -> path = valueAsString
else -> skipChildren()
}
}
Screenshot.Type.values().find { it.jsonName == type }
?.let { Screenshot(locale, it, path) }
}
collectNotNull(JsonToken.START_OBJECT, Screenshot::deserialize)
it.array("releases") -> releases =
collectNotNull(JsonToken.START_OBJECT, Release.Companion::deserialize)
else -> skipChildren()