From 1c7d2e3e08dcbb60316ca3b190c9d4d3218349f8 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Fri, 1 Apr 2022 01:56:18 +0200 Subject: [PATCH] Update Migrate IndexMerger and ProductPreferences to use k-serialization --- .../droidify/content/ProductPreferences.kt | 11 ++---- .../com/looker/droidify/index/IndexMerger.kt | 36 +++++-------------- 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/content/ProductPreferences.kt b/src/main/kotlin/com/looker/droidify/content/ProductPreferences.kt index c20df0b0..6fe14a07 100644 --- a/src/main/kotlin/com/looker/droidify/content/ProductPreferences.kt +++ b/src/main/kotlin/com/looker/droidify/content/ProductPreferences.kt @@ -5,9 +5,6 @@ import android.content.SharedPreferences import com.looker.droidify.database.DatabaseX import com.looker.droidify.database.entity.Lock import com.looker.droidify.entity.ProductPreference -import com.looker.droidify.utility.extension.json.Json -import com.looker.droidify.utility.extension.json.parseDictionary -import com.looker.droidify.utility.extension.json.writeDictionary import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableSharedFlow @@ -52,8 +49,7 @@ object ProductPreferences { operator fun get(packageName: String): ProductPreference { return if (preferences.contains(packageName)) { try { - Json.factory.createParser(preferences.getString(packageName, "{}")) - .use { it.parseDictionary(ProductPreference.Companion::deserialize) } + ProductPreference.fromJson(preferences.getString(packageName, "{}") ?: "{}") } catch (e: Exception) { e.printStackTrace() defaultProductPreference @@ -66,10 +62,7 @@ object ProductPreferences { operator fun set(packageName: String, productPreference: ProductPreference) { val oldProductPreference = this[packageName] preferences.edit().putString(packageName, ByteArrayOutputStream() - .apply { - Json.factory.createGenerator(this) - .use { it.writeDictionary(productPreference::serialize) } - } + .apply { write(productPreference.toJSON().toByteArray()) } .toByteArray().toString(Charset.defaultCharset())).apply() if (oldProductPreference.ignoreUpdates != productPreference.ignoreUpdates || oldProductPreference.ignoreVersionCode != productPreference.ignoreVersionCode diff --git a/src/main/kotlin/com/looker/droidify/index/IndexMerger.kt b/src/main/kotlin/com/looker/droidify/index/IndexMerger.kt index b5fd4a16..5528d763 100644 --- a/src/main/kotlin/com/looker/droidify/index/IndexMerger.kt +++ b/src/main/kotlin/com/looker/droidify/index/IndexMerger.kt @@ -2,14 +2,12 @@ package com.looker.droidify.index import android.content.ContentValues import android.database.sqlite.SQLiteDatabase -import com.fasterxml.jackson.core.JsonToken +import com.looker.droidify.database.Converters.toByteArray +import com.looker.droidify.database.Converters.toReleases import com.looker.droidify.database.entity.Release import com.looker.droidify.entity.Product import com.looker.droidify.utility.extension.android.asSequence import com.looker.droidify.utility.extension.android.execWithResult -import com.looker.droidify.utility.extension.json.Json -import com.looker.droidify.utility.extension.json.collectNotNull -import com.looker.droidify.utility.extension.json.writeDictionary import java.io.ByteArrayOutputStream import java.io.Closeable import java.io.File @@ -28,8 +26,7 @@ class IndexMerger(file: File) : Closeable { fun addProducts(products: List) { for (product in products) { val outputStream = ByteArrayOutputStream() - Json.factory.createGenerator(outputStream) - .use { it.writeDictionary(product::serialize) } + outputStream.write(product.toJSON().toByteArray()) db.insert("product", null, ContentValues().apply { put("package_name", product.packageName) put("description", product.description) @@ -42,13 +39,7 @@ class IndexMerger(file: File) : Closeable { for (pair in pairs) { val (packageName, releases) = pair val outputStream = ByteArrayOutputStream() - Json.factory.createGenerator(outputStream).use { - it.writeStartArray() - for (release in releases) { - it.writeDictionary(release::serialize) - } - it.writeEndArray() - } + outputStream.write(toByteArray(releases)) db.insert("releases", null, ContentValues().apply { put("package_name", packageName) put("data", outputStream.toByteArray()) @@ -72,22 +63,11 @@ class IndexMerger(file: File) : Closeable { ?.use { it -> it.asSequence().map { val description = it.getString(0) - val product = Json.factory.createParser(it.getBlob(1)).use { - it.nextToken() - Product.deserialize(it).apply { - this.repositoryId = repositoryId - this.description = description - } + val product = Product.fromJson(String(it.getBlob(1))).apply { + this.repositoryId = repositoryId + this.description = description } - val releases = it.getBlob(2)?.let { - Json.factory.createParser(it).use { - it.nextToken() - it.collectNotNull( - JsonToken.START_OBJECT, - Release.Companion::deserialize - ) - } - }.orEmpty() + val releases = it.getBlob(2)?.let { toReleases(it) }.orEmpty() product.copy(releases = releases) }.windowed(windowSize, windowSize, true) .forEach { products -> callback(products, it.count) }