diff --git a/src/main/kotlin/com/looker/droidify/Common.kt b/src/main/kotlin/com/looker/droidify/Common.kt index 983ebc96..74f61bad 100644 --- a/src/main/kotlin/com/looker/droidify/Common.kt +++ b/src/main/kotlin/com/looker/droidify/Common.kt @@ -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" diff --git a/src/main/kotlin/com/looker/droidify/database/Converters.kt b/src/main/kotlin/com/looker/droidify/database/Converters.kt index b589d280..383939b6 100644 --- a/src/main/kotlin/com/looker/droidify/database/Converters.kt +++ b/src/main/kotlin/com/looker/droidify/database/Converters.kt @@ -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 { + 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) = jsonGenerate { + screenshots.forEach { item -> item.serialize(it) }.toString().toByteArray() + } + + @TypeConverter + @JvmStatic + fun toDonates(byteArray: ByteArray): List { + 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) = jsonGenerate { + donates.forEach { item -> item.serialize(it) }.toString().toByteArray() + } } \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/database/DAOs.kt b/src/main/kotlin/com/looker/droidify/database/DAOs.kt index 11724c07..c9da3db6 100644 --- a/src/main/kotlin/com/looker/droidify/database/DAOs.kt +++ b/src/main/kotlin/com/looker/droidify/database/DAOs.kt @@ -109,14 +109,15 @@ interface ProductDao : BaseDao { 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 { 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 -> diff --git a/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt b/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt index a37fcb72..dca74ed1 100644 --- a/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt +++ b/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt @@ -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() { diff --git a/src/main/kotlin/com/looker/droidify/database/entity/Product.kt b/src/main/kotlin/com/looker/droidify/database/entity/Product.kt index 7d8dd84d..1d4a6bc5 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Product.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Product.kt @@ -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 = emptyList() var categories: List = emptyList() + var antiFeatures: List = emptyList() + var licenses: List = emptyList() + var donates: List = emptyList() + var screenshots: List = emptyList() // TODO Remove in next iteration @ColumnInfo(typeAffinity = ColumnInfo.BLOB) diff --git a/src/main/kotlin/com/looker/droidify/entity/Product.kt b/src/main/kotlin/com/looker/droidify/entity/Product.kt index 22621289..b19d797f 100644 --- a/src/main/kotlin/com/looker/droidify/entity/Product.kt +++ b/src/main/kotlin/com/looker/droidify/entity/Product.kt @@ -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()