Update: Complement database.entity's Product & make serializable to replace entity' Product

This commit is contained in:
machiav3lli 2022-04-06 03:50:52 +02:00
parent 2d7e6a7e90
commit 43180d2844
5 changed files with 182 additions and 49 deletions

View File

@ -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<Release> =

View File

@ -148,7 +148,7 @@ interface ProductDao : BaseDao<Product> {
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<ProductTemp> {
fun insertCategory(vararg product: CategoryTemp)
@Transaction
fun putTemporary(products: List<com.looker.droidify.entity.Product>) {
fun putTemporary(products: List<Product>) {
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

View File

@ -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() {

View File

@ -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<Release> = emptyList()
var categories: List<String> = emptyList()
var antiFeatures: List<String> = emptyList()
@ -27,13 +32,64 @@ open class Product {
var donates: List<Donate> = emptyList()
var screenshots: List<Screenshot> = emptyList()
var versionCode: Long = 0L
var suggestedVersionCode: Long = 0L
var signatures: List<String> = 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<Release>,
categories: List<String>,
antiFeatures: List<String>,
licenses: List<String>,
donates: List<Donate>,
screenshots: List<Screenshot>,
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<Release>
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<Product>(json)
}
}
@Entity(tableName = "temporary_product")
class ProductTemp : Product()
class ProductTemp(
repositoryId: Long,
packageName: String,
name: String,
summary: String,
description: String,
added: Long,
updated: Long,
icon: String,
metadataIcon: String,
releases: List<Release>,
categories: List<String>,
antiFeatures: List<String>,
licenses: List<String>,
donates: List<Donate>,
screenshots: List<Screenshot>,
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
)

View File

@ -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<Author>(json)
}
}
@Serializable
sealed class Donate {