From 43180d2844a4cebbbb75d9bf8aa8e21e4de85d43 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Wed, 6 Apr 2022 03:50:52 +0200 Subject: [PATCH] Update: Complement database.entity's Product & make serializable to replace entity' Product --- .../looker/droidify/database/Converters.kt | 11 +- .../com/looker/droidify/database/DAOs.kt | 30 +-- .../com/looker/droidify/database/DatabaseX.kt | 2 +- .../droidify/database/entity/Product.kt | 180 ++++++++++++++++-- .../com/looker/droidify/entity/SubEntities.kt | 8 +- 5 files changed, 182 insertions(+), 49 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/database/Converters.kt b/src/main/kotlin/com/looker/droidify/database/Converters.kt index 0da17cc4..c673b42f 100644 --- a/src/main/kotlin/com/looker/droidify/database/Converters.kt +++ b/src/main/kotlin/com/looker/droidify/database/Converters.kt @@ -1,9 +1,10 @@ package com.looker.droidify.database import androidx.room.TypeConverter +import com.looker.droidify.database.entity.Product import com.looker.droidify.database.entity.Release +import com.looker.droidify.entity.Author import com.looker.droidify.entity.Donate -import com.looker.droidify.entity.Product import com.looker.droidify.entity.Screenshot object Converters { @@ -28,6 +29,14 @@ object Converters { @JvmStatic fun toByteArray(product: Product) = product.toJSON().toByteArray() + @TypeConverter + @JvmStatic + fun toAuthor(byteArray: ByteArray) = Author.fromJson(String(byteArray)) + + @TypeConverter + @JvmStatic + fun toByteArray(author: Author) = author.toJSON().toByteArray() + @TypeConverter @JvmStatic fun toReleases(byteArray: ByteArray): List = diff --git a/src/main/kotlin/com/looker/droidify/database/DAOs.kt b/src/main/kotlin/com/looker/droidify/database/DAOs.kt index 47289972..af5c779d 100644 --- a/src/main/kotlin/com/looker/droidify/database/DAOs.kt +++ b/src/main/kotlin/com/looker/droidify/database/DAOs.kt @@ -148,7 +148,7 @@ interface ProductDao : BaseDao { 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, product.$ROW_DATA,""" + product.$ROW_ANTIFEATURES, product.$ROW_LICENSES, product.$ROW_DONATES, product.$ROW_SCREENSHOTS,""" // Calculate the matching score with the search query if (searchQuery.isNotEmpty()) { @@ -306,33 +306,9 @@ interface ProductTempDao : BaseDao { fun insertCategory(vararg product: CategoryTemp) @Transaction - fun putTemporary(products: List) { + fun putTemporary(products: List) { products.forEach { - val signatures = it.signatures.joinToString { ".$it" } - .let { if (it.isNotEmpty()) "$it." else "" } - insert(it.let { - ProductTemp().apply { - repository_id = it.repositoryId - package_name = it.packageName - name = it.name - summary = it.summary - description = it.description - added = it.added - updated = it.updated - version_code = it.versionCode - this.signatures = signatures - compatible = if (it.compatible) 1 else 0 - data = it - icon = it.icon - metadataIcon = it.metadataIcon - releases = it.releases - categories = it.categories - antiFeatures = it.antiFeatures - licenses = it.licenses - donates = it.donates - screenshots = it.screenshots - } - }) + insert(it.let { it.asProductTemp() }) it.categories.forEach { category -> insertCategory(CategoryTemp().apply { repositoryId = it.repositoryId diff --git a/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt b/src/main/kotlin/com/looker/droidify/database/DatabaseX.kt index dca74ed1..f139becb 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 = 4 + ], version = 5 ) @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 97a0bd68..4830f8ac 100644 --- a/src/main/kotlin/com/looker/droidify/database/entity/Product.kt +++ b/src/main/kotlin/com/looker/droidify/database/entity/Product.kt @@ -1,25 +1,30 @@ package com.looker.droidify.database.entity -import androidx.room.ColumnInfo import androidx.room.Entity +import com.looker.droidify.entity.Author import com.looker.droidify.entity.Donate import com.looker.droidify.entity.ProductItem import com.looker.droidify.entity.Screenshot +import com.looker.droidify.utility.extension.text.nullIfEmpty +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json -open class Product { - - var name = "" - var summary = "" - var description = "" - var added = 0L - var updated = 0L - var signatures = "" - var compatible = 0 - var icon = "" - var metadataIcon = "" +// TODO Add Product Extras to handle favorite lists etc.. @Entity(tableName = "product", primaryKeys = ["repositoryId", "packageName"]) +@Serializable +open class Product( var repositoryId: Long, var packageName: String +) { + var name: String = "" + var summary: String = "" + var description: String = "" + var added: Long = 0L + var updated: Long = 0L + var icon: String = "" + var metadataIcon: String = "" var releases: List = emptyList() var categories: List = emptyList() var antiFeatures: List = emptyList() @@ -27,13 +32,64 @@ open class Product { var donates: List = emptyList() var screenshots: List = emptyList() var versionCode: Long = 0L + var suggestedVersionCode: Long = 0L + var signatures: List = emptyList() + var compatible: Boolean = false + var author: Author = Author() + var source: String = "" + var web: String = "" + var tracker: String = "" + var changelog: String = "" + var whatsNew: String = "" - // TODO Remove in next iteration - @ColumnInfo(typeAffinity = ColumnInfo.BLOB) - var data: com.looker.droidify.entity.Product? = null - - val trueData: com.looker.droidify.entity.Product? - get() = data?.copy(repositoryId = repository_id) + constructor( + repositoryId: Long, + packageName: String, + name: String, + summary: String, + description: String, + added: Long, + updated: Long, + icon: String, + metadataIcon: String, + releases: List, + categories: List, + antiFeatures: List, + licenses: List, + donates: List, + screenshots: List, + suggestedVersionCode: Long = 0L, + author: Author = Author(), + source: String = "", + web: String = "", + tracker: String = "", + changelog: String = "", + whatsNew: String = "" + ) : this(repositoryId, packageName) { + this.name = name + this.summary = summary + this.description = description + this.added = added + this.updated = updated + this.icon = icon + this.metadataIcon = metadataIcon + this.releases = releases + this.categories = categories + this.antiFeatures = antiFeatures + this.licenses = licenses + this.donates = donates + this.screenshots = screenshots + this.versionCode = selectedReleases.firstOrNull()?.versionCode ?: 0L + this.suggestedVersionCode = suggestedVersionCode + this.signatures = selectedReleases.mapNotNull { it.signature.nullIfEmpty() }.distinct() + this.compatible = selectedReleases.firstOrNull()?.incompatibilities?.isEmpty() == true + this.author = author + this.source = source + this.web = web + this.tracker = tracker + this.changelog = changelog + this.whatsNew = whatsNew + } val selectedReleases: List get() = releases.filter { it.selected } @@ -58,7 +114,93 @@ open class Product { canUpdate(installed), 0 ) + + fun canUpdate(installed: Installed?): Boolean = + installed != null && compatible && versionCode > installed.versionCode && installed.signature in signatures + + fun refreshVariables() { + this.versionCode = selectedReleases.firstOrNull()?.versionCode ?: 0L + this.signatures = selectedReleases.mapNotNull { it.signature.nullIfEmpty() }.distinct() + this.compatible = selectedReleases.firstOrNull()?.incompatibilities?.isEmpty() == true + } + + fun toJSON() = Json.encodeToString(this) + + companion object { + fun fromJson(json: String) = Json.decodeFromString(json) + } } @Entity(tableName = "temporary_product") -class ProductTemp : Product() \ No newline at end of file +class ProductTemp( + repositoryId: Long, + packageName: String, + name: String, + summary: String, + description: String, + added: Long, + updated: Long, + icon: String, + metadataIcon: String, + releases: List, + categories: List, + antiFeatures: List, + licenses: List, + donates: List, + screenshots: List, + suggestedVersionCode: Long = 0L, + author: Author = Author(), + source: String = "", + web: String = "", + tracker: String = "", + changelog: String = "", + whatsNew: String = "" +) : Product( + repositoryId = repositoryId, + packageName = packageName, + name = name, + summary = summary, + description = description, + added = added, + updated = updated, + icon = icon, + metadataIcon = metadataIcon, + releases = releases, + categories = categories, + antiFeatures = antiFeatures, + licenses = licenses, + donates = donates, + screenshots = screenshots, + suggestedVersionCode = suggestedVersionCode, + author = author, + source = source, + web = web, + tracker = tracker, + changelog = changelog, + whatsNew = whatsNew +) + +fun Product.asProductTemp(): ProductTemp = ProductTemp( + repositoryId = repositoryId, + packageName = packageName, + name = name, + summary = summary, + description = description, + added = added, + updated = updated, + icon = icon, + metadataIcon = metadataIcon, + releases = releases, + categories = categories, + antiFeatures = antiFeatures, + licenses = licenses, + donates = donates, + screenshots = screenshots, + suggestedVersionCode = suggestedVersionCode, + author = author, + source = source, + web = web, + tracker = tracker, + changelog = changelog, + whatsNew = whatsNew +) \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt b/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt index 70173524..5a50a435 100644 --- a/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt +++ b/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt @@ -6,7 +6,13 @@ import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json @Serializable -data class Author(val name: String, val email: String, val web: String) +data class Author(val name: String = "", val email: String = "", val web: String = "") { + fun toJSON() = Json.encodeToString(this) + + companion object { + fun fromJson(json: String) = Json.decodeFromString(json) + } +} @Serializable sealed class Donate {