Update: Replace entities hard strings with constants

This commit is contained in:
machiav3lli 2022-04-08 02:24:45 +02:00
parent 92035ba7da
commit d2c0a997d2
8 changed files with 80 additions and 53 deletions

View File

@ -10,9 +10,23 @@ const val NOTIFICATION_ID_UPDATES = 2
const val NOTIFICATION_ID_DOWNLOADING = 3 const val NOTIFICATION_ID_DOWNLOADING = 3
const val NOTIFICATION_ID_INSTALLER = 4 const val NOTIFICATION_ID_INSTALLER = 4
const val TABLE_CATEGORY = "category"
const val TABLE_CATEGORY_NAME = "category"
const val TABLE_CATEGORY_TEMP_NAME = "temporary_category"
const val TABLE_INSTALLED = "installed"
const val TABLE_INSTALLED_NAME = "memory_installed"
const val TABLE_LOCK = "lock"
const val TABLE_LOCK_NAME = "memory_lock"
const val TABLE_PRODUCT = "product"
const val TABLE_PRODUCT_NAME = "product"
const val TABLE_PRODUCT_TEMP_NAME = "temporary_product"
const val TABLE_RELEASE = "release"
const val TABLE_RELEASE_NAME = "release"
const val TABLE_REPOSITORY = "repository"
const val TABLE_REPOSITORY_NAME = "repository"
const val ROW_REPOSITORY_ID = "repositoryId" const val ROW_REPOSITORY_ID = "repositoryId"
const val ROW_PACKAGE_NAME = "packageName" const val ROW_PACKAGE_NAME = "packageName"
const val ROW_NAME = "name"
const val ROW_LABEL = "label" const val ROW_LABEL = "label"
const val ROW_SUMMARY = "summary" const val ROW_SUMMARY = "summary"
const val ROW_DESCRIPTION = "description" const val ROW_DESCRIPTION = "description"
@ -33,14 +47,8 @@ const val ROW_VERSION = "version"
const val ROW_SIGNATURE = "signature" const val ROW_SIGNATURE = "signature"
const val ROW_ID = "_id" const val ROW_ID = "_id"
const val ROW_ENABLED = "enabled" const val ROW_ENABLED = "enabled"
const val ROW_DELETED = "deleted"
const val ROW_CAN_UPDATE = "can_update" const val ROW_CAN_UPDATE = "can_update"
const val ROW_MATCH_RANK = "match_rank" const val ROW_MATCH_RANK = "match_rank"
const val ROW_REPOSITORY_NAME = "repository"
const val ROW_PRODUCT_NAME = "product"
const val ROW_CATEGORY_NAME = "category"
const val ROW_INSTALLED_NAME = "memory_installed"
const val ROW_LOCK_NAME = "memory_lock"
const val JOB_ID_SYNC = 1 const val JOB_ID_SYNC = 1

View File

@ -135,65 +135,65 @@ interface ProductDao : BaseDao<Product> {
): SupportSQLiteQuery { ): SupportSQLiteQuery {
val builder = QueryBuilder() val builder = QueryBuilder()
val signatureMatches = """installed.${ROW_SIGNATURE} IS NOT NULL AND val signatureMatches = """$TABLE_INSTALLED.$ROW_SIGNATURE IS NOT NULL AND
product.${ROW_SIGNATURES} LIKE ('%.' || installed.${ROW_SIGNATURE} || '.%') AND $TABLE_PRODUCT.$ROW_SIGNATURES LIKE ('%.' || $TABLE_INSTALLED.$ROW_SIGNATURE || '.%') AND
product.${ROW_SIGNATURES} != ''""" $TABLE_PRODUCT.$ROW_SIGNATURES != ''"""
// Select the return fields // Select the return fields
builder += """SELECT product.rowid AS _id, product.$ROW_REPOSITORY_ID, builder += """SELECT $TABLE_PRODUCT.rowid AS $ROW_ID, $TABLE_PRODUCT.$ROW_REPOSITORY_ID,
product.$ROW_PACKAGE_NAME, product.$ROW_NAME, $TABLE_PRODUCT.$ROW_PACKAGE_NAME, $TABLE_PRODUCT.$ROW_LABEL,
product.$ROW_SUMMARY, installed.$ROW_VERSION, $TABLE_PRODUCT.$ROW_SUMMARY, $TABLE_INSTALLED.$ROW_VERSION,
(COALESCE(lock.$ROW_VERSION_CODE, -1) NOT IN (0, product.$ROW_VERSION_CODE) AND (COALESCE($TABLE_LOCK.$ROW_VERSION_CODE, -1) NOT IN (0, $TABLE_PRODUCT.$ROW_VERSION_CODE) AND
product.$ROW_COMPATIBLE != 0 AND product.$ROW_VERSION_CODE > $TABLE_PRODUCT.$ROW_COMPATIBLE != 0 AND $TABLE_PRODUCT.$ROW_VERSION_CODE >
COALESCE(installed.$ROW_VERSION_CODE, 0xffffffff) AND $signatureMatches) COALESCE($TABLE_INSTALLED.$ROW_VERSION_CODE, 0xffffffff) AND $signatureMatches)
AS $ROW_CAN_UPDATE, product.$ROW_COMPATIBLE, product.$ROW_ICON, AS $ROW_CAN_UPDATE, $TABLE_PRODUCT.$ROW_COMPATIBLE, $TABLE_PRODUCT.$ROW_ICON,
product.$ROW_METADATA_ICON, product.$ROW_RELEASES, product.$ROW_CATEGORIES, $TABLE_PRODUCT.$ROW_METADATA_ICON, $TABLE_PRODUCT.$ROW_RELEASES, $TABLE_PRODUCT.$ROW_CATEGORIES,
product.$ROW_ANTIFEATURES, product.$ROW_LICENSES, product.$ROW_DONATES, product.$ROW_SCREENSHOTS,""" $TABLE_PRODUCT.$ROW_ANTIFEATURES, $TABLE_PRODUCT.$ROW_LICENSES, $TABLE_PRODUCT.$ROW_DONATES, $TABLE_PRODUCT.$ROW_SCREENSHOTS,"""
// Calculate the matching score with the search query // Calculate the matching score with the search query
if (searchQuery.isNotEmpty()) { if (searchQuery.isNotEmpty()) {
builder += """(((product.${ROW_NAME} LIKE ? OR builder += """((($TABLE_PRODUCT.$ROW_LABEL LIKE ? OR
product.${ROW_SUMMARY} LIKE ?) * 7) | $TABLE_PRODUCT.$ROW_SUMMARY LIKE ?) * 7) |
((product.${ROW_PACKAGE_NAME} LIKE ?) * 3) | (($TABLE_PRODUCT.$ROW_PACKAGE_NAME LIKE ?) * 3) |
(product.${ROW_DESCRIPTION} LIKE ?)) AS ${ROW_MATCH_RANK},""" ($TABLE_PRODUCT.$ROW_DESCRIPTION LIKE ?)) AS $ROW_MATCH_RANK,"""
builder %= List(4) { "%$searchQuery%" } builder %= List(4) { "%$searchQuery%" }
} else { } else {
builder += "0 AS ${ROW_MATCH_RANK}," builder += "0 AS $ROW_MATCH_RANK,"
} }
// Take product as main table // Take product as main table
builder += """MAX((product.${ROW_COMPATIBLE} AND builder += """MAX(($TABLE_PRODUCT.$ROW_COMPATIBLE AND
(installed.${ROW_SIGNATURE} IS NULL OR $signatureMatches)) || ($TABLE_INSTALLED.$ROW_SIGNATURE IS NULL OR $signatureMatches)) ||
PRINTF('%016X', product.${ROW_VERSION_CODE})) FROM $ROW_PRODUCT_NAME AS product""" PRINTF('%016X', $TABLE_PRODUCT.$ROW_VERSION_CODE)) FROM $TABLE_PRODUCT_NAME AS $TABLE_PRODUCT"""
// Merge the matching repositories // Merge the matching repositories
builder += """JOIN $ROW_REPOSITORY_NAME AS repository builder += """JOIN $TABLE_REPOSITORY_NAME AS $TABLE_REPOSITORY
ON product.${ROW_REPOSITORY_ID} = repository.${ROW_ID}""" ON $TABLE_PRODUCT.$ROW_REPOSITORY_ID = $TABLE_REPOSITORY.$ROW_ID"""
// Merge the matching locks // Merge the matching locks
builder += """LEFT JOIN $ROW_LOCK_NAME AS lock builder += """LEFT JOIN $TABLE_LOCK_NAME AS $TABLE_LOCK
ON product.${ROW_PACKAGE_NAME} = lock.${ROW_PACKAGE_NAME}""" ON $TABLE_PRODUCT.$ROW_PACKAGE_NAME = $TABLE_LOCK.$ROW_PACKAGE_NAME"""
// Merge the matching installed // Merge the matching installed
if (!installed && !updates) builder += "LEFT" if (!installed && !updates) builder += "LEFT"
builder += """JOIN $ROW_INSTALLED_NAME AS installed builder += """JOIN $TABLE_INSTALLED_NAME AS $TABLE_INSTALLED
ON product.${ROW_PACKAGE_NAME} = installed.${ROW_PACKAGE_NAME}""" ON $TABLE_PRODUCT.$ROW_PACKAGE_NAME = $TABLE_INSTALLED.$ROW_PACKAGE_NAME"""
// Merge the matching category // Merge the matching category
if (section is Section.Category) { if (section is Section.Category) {
builder += """JOIN $ROW_CATEGORY_NAME AS category builder += """JOIN $TABLE_CATEGORY_NAME AS $TABLE_CATEGORY
ON product.${ROW_PACKAGE_NAME} = category.${ROW_PACKAGE_NAME}""" ON $TABLE_PRODUCT.$ROW_PACKAGE_NAME = $TABLE_CATEGORY.$ROW_PACKAGE_NAME"""
} }
// Filter only active repositories // Filter only active repositories
builder += """WHERE repository.${ROW_ENABLED} != 0""" builder += """WHERE $TABLE_REPOSITORY.$ROW_ENABLED != 0"""
// Filter only the selected repository/category // Filter only the selected repository/category
if (section is Section.Category) { if (section is Section.Category) {
builder += "AND category.${ROW_LABEL} = ?" builder += "AND $TABLE_CATEGORY.$ROW_LABEL = ?"
builder %= section.name builder %= section.name
} else if (section is Section.Repository) { } else if (section is Section.Repository) {
builder += "AND product.${ROW_REPOSITORY_ID} = ?" builder += "AND $TABLE_PRODUCT.$ROW_REPOSITORY_ID = ?"
builder %= section.id.toString() builder %= section.id.toString()
} }
@ -204,12 +204,12 @@ interface ProductDao : BaseDao<Product> {
when (updateCategory) { when (updateCategory) {
UpdateCategory.ALL -> Unit UpdateCategory.ALL -> Unit
UpdateCategory.NEW -> builder += """AND product.${ROW_ADDED} = product.${ROW_UPDATED}""" UpdateCategory.NEW -> builder += """AND $TABLE_PRODUCT.$ROW_ADDED = $TABLE_PRODUCT.$ROW_UPDATED"""
UpdateCategory.UPDATED -> builder += """AND product.${ROW_ADDED} < product.${ROW_UPDATED}""" UpdateCategory.UPDATED -> builder += """AND $TABLE_PRODUCT.$ROW_ADDED < $TABLE_PRODUCT.$ROW_UPDATED"""
} }
// Sum up all products with the same package name // Sum up all products with the same package name
builder += "GROUP BY product.${ROW_PACKAGE_NAME} HAVING 1" builder += "GROUP BY $TABLE_PRODUCT.$ROW_PACKAGE_NAME HAVING 1"
// Filter if only can update // Filter if only can update
if (updates) { if (updates) {
@ -221,10 +221,10 @@ interface ProductDao : BaseDao<Product> {
if (searchQuery.isNotEmpty()) builder += """$ROW_MATCH_RANK DESC,""" if (searchQuery.isNotEmpty()) builder += """$ROW_MATCH_RANK DESC,"""
when (order) { when (order) {
Order.NAME -> Unit Order.NAME -> Unit
Order.DATE_ADDED -> builder += "product.${ROW_ADDED} DESC," Order.DATE_ADDED -> builder += "$TABLE_PRODUCT.$ROW_ADDED DESC,"
Order.LAST_UPDATE -> builder += "product.${ROW_UPDATED} DESC," Order.LAST_UPDATE -> builder += "$TABLE_PRODUCT.$ROW_UPDATED DESC,"
}::class }::class
builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}" builder += "$TABLE_PRODUCT.$ROW_LABEL COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}"
return SimpleSQLiteQuery(builder.build(), builder.arguments.toTypedArray()) return SimpleSQLiteQuery(builder.build(), builder.arguments.toTypedArray())
} }

View File

@ -1,13 +1,17 @@
package com.looker.droidify.database.entity package com.looker.droidify.database.entity
import androidx.room.Entity import androidx.room.Entity
import com.looker.droidify.*
@Entity(tableName = "category", primaryKeys = ["repositoryId", "packageName", "label"]) @Entity(
tableName = TABLE_CATEGORY_NAME,
primaryKeys = [ROW_REPOSITORY_ID, ROW_PACKAGE_NAME, ROW_LABEL]
)
open class Category { open class Category {
var repositoryId: Long = 0 var repositoryId: Long = 0
var packageName = "" var packageName = ""
var label = "" var label = ""
} }
@Entity(tableName = "temporary_category") @Entity(tableName = TABLE_CATEGORY_TEMP_NAME)
class CategoryTemp : Category() class CategoryTemp : Category()

View File

@ -2,8 +2,9 @@ package com.looker.droidify.database.entity
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.looker.droidify.TABLE_INSTALLED_NAME
@Entity(tableName = "memory_installed") @Entity(tableName = TABLE_INSTALLED_NAME)
data class Installed( data class Installed(
@PrimaryKey @PrimaryKey
var packageName: String = "", var packageName: String = "",

View File

@ -2,8 +2,9 @@ package com.looker.droidify.database.entity
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.looker.droidify.TABLE_LOCK_NAME
@Entity(tableName = "memory_lock") @Entity(tableName = TABLE_LOCK_NAME)
data class Lock( data class Lock(
@PrimaryKey @PrimaryKey
var packageName: String = "", var packageName: String = "",

View File

@ -1,6 +1,10 @@
package com.looker.droidify.database.entity package com.looker.droidify.database.entity
import androidx.room.Entity import androidx.room.Entity
import com.looker.droidify.ROW_PACKAGE_NAME
import com.looker.droidify.ROW_REPOSITORY_ID
import com.looker.droidify.TABLE_PRODUCT_NAME
import com.looker.droidify.TABLE_PRODUCT_TEMP_NAME
import com.looker.droidify.entity.Author import com.looker.droidify.entity.Author
import com.looker.droidify.entity.Donate import com.looker.droidify.entity.Donate
import com.looker.droidify.entity.ProductItem import com.looker.droidify.entity.ProductItem
@ -12,7 +16,7 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
// TODO Add Product Extras to handle favorite lists etc.. // TODO Add Product Extras to handle favorite lists etc..
@Entity(tableName = "product", primaryKeys = ["repositoryId", "packageName"]) @Entity(tableName = TABLE_PRODUCT_NAME, primaryKeys = [ROW_REPOSITORY_ID, ROW_PACKAGE_NAME])
@Serializable @Serializable
open class Product( open class Product(
var repositoryId: Long, var repositoryId: Long,
@ -131,7 +135,7 @@ open class Product(
} }
} }
@Entity(tableName = "temporary_product") @Entity(tableName = TABLE_PRODUCT_TEMP_NAME)
class ProductTemp( class ProductTemp(
repositoryId: Long, repositoryId: Long,
packageName: String, packageName: String,

View File

@ -2,13 +2,20 @@ package com.looker.droidify.database.entity
import android.net.Uri import android.net.Uri
import androidx.room.Entity import androidx.room.Entity
import com.looker.droidify.ROW_PACKAGE_NAME
import com.looker.droidify.ROW_SIGNATURE
import com.looker.droidify.ROW_VERSION_CODE
import com.looker.droidify.TABLE_RELEASE_NAME
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
// TODO make a Room entity // TODO make a Room entity
@Entity(primaryKeys = ["packageName", "versionCode", "signature"]) @Entity(
tableName = TABLE_RELEASE_NAME,
primaryKeys = [ROW_PACKAGE_NAME, ROW_VERSION_CODE, ROW_SIGNATURE]
)
@Serializable @Serializable
data class Release( data class Release(
val packageName: String, val packageName: String,

View File

@ -3,17 +3,19 @@ package com.looker.droidify.database.entity
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.looker.droidify.ROW_ID
import com.looker.droidify.TABLE_REPOSITORY_NAME
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.net.URL import java.net.URL
@Entity @Entity(tableName = TABLE_REPOSITORY_NAME)
@Serializable @Serializable
data class Repository( data class Repository(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id") @ColumnInfo(name = ROW_ID)
var id: Long = 0, var id: Long = 0,
var address: String = "", var address: String = "",
var mirrors: List<String> = emptyList(), var mirrors: List<String> = emptyList(),