mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-24 11:52:13 +00:00
Update: Migrate Subentities and Converters to k-serialization
This commit is contained in:
parent
0487982784
commit
82d00ac6d6
@ -2,14 +2,9 @@ package com.looker.droidify.database
|
|||||||
|
|
||||||
import androidx.room.TypeConverter
|
import androidx.room.TypeConverter
|
||||||
import com.looker.droidify.database.entity.Release
|
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.Donate
|
||||||
|
import com.looker.droidify.entity.Product
|
||||||
import com.looker.droidify.entity.Screenshot
|
import com.looker.droidify.entity.Screenshot
|
||||||
import com.looker.droidify.utility.extension.json.collectList
|
|
||||||
import com.looker.droidify.utility.extension.json.writeDictionary
|
|
||||||
import com.looker.droidify.utility.extension.json.writeList
|
|
||||||
import com.looker.droidify.utility.jsonGenerate
|
|
||||||
import com.looker.droidify.utility.jsonParse
|
|
||||||
|
|
||||||
object Converters {
|
object Converters {
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@ -27,84 +22,62 @@ object Converters {
|
|||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toProduct(byteArray: ByteArray) =
|
fun toProduct(byteArray: ByteArray) = Product.fromJson(String(byteArray))
|
||||||
byteArray.jsonParse { com.looker.droidify.entity.Product.deserialize(it) }
|
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toByteArray(product: com.looker.droidify.entity.Product) = jsonGenerate(product::serialize)
|
fun toByteArray(product: Product) = product.toJSON().toByteArray()
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toReleases(byteArray: ByteArray): List<Release> = byteArray.jsonParse {
|
fun toReleases(byteArray: ByteArray): List<Release> =
|
||||||
it.collectList("releases") { Release.deserialize(it) }
|
if (String(byteArray) == "") emptyList()
|
||||||
}
|
else String(byteArray).split(";").map { Release.fromJson(it) }
|
||||||
|
|
||||||
@JvmName("releasesToByteArray")
|
@JvmName("releasesToByteArray")
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toByteArray(releases: List<Release>) = jsonGenerate { generator ->
|
fun toByteArray(releases: List<Release>) =
|
||||||
generator.writeList("releases", releases) { serialize(generator) }
|
if (releases.isNotEmpty()) releases.joinToString(";") { it.toJSON() }.toByteArray()
|
||||||
}
|
else "".toByteArray()
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toIncompatibilities(byteArray: ByteArray) =
|
fun toIncompatibilities(byteArray: ByteArray): List<Release.Incompatibility> =
|
||||||
byteArray.jsonParse { it.deserializeIncompatibilities() }
|
if (String(byteArray) == "") emptyList()
|
||||||
|
else String(byteArray).split(";").map { Release.Incompatibility.fromJson(it) }
|
||||||
|
|
||||||
@JvmName("incompatibilitiesToByteArray")
|
@JvmName("incompatibilitiesToByteArray")
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toByteArray(list: List<Release.Incompatibility>) =
|
fun toByteArray(incompatibilities: List<Release.Incompatibility>) =
|
||||||
jsonGenerate { generator ->
|
if (incompatibilities.isNotEmpty())
|
||||||
list.forEach {
|
incompatibilities.joinToString(";") { it.toJSON() }.toByteArray()
|
||||||
generator.writeDictionary {
|
else "".toByteArray()
|
||||||
when (it) {
|
|
||||||
is Release.Incompatibility.MinSdk -> {
|
|
||||||
writeStringField("type", "minSdk")
|
|
||||||
}
|
|
||||||
is Release.Incompatibility.MaxSdk -> {
|
|
||||||
writeStringField("type", "maxSdk")
|
|
||||||
}
|
|
||||||
is Release.Incompatibility.Platform -> {
|
|
||||||
writeStringField("type", "platform")
|
|
||||||
}
|
|
||||||
is Release.Incompatibility.Feature -> {
|
|
||||||
writeStringField("type", "feature")
|
|
||||||
writeStringField("feature", it.feature)
|
|
||||||
}
|
|
||||||
}::class
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toScreenshots(byteArray: ByteArray): List<Screenshot> {
|
fun toScreenshots(byteArray: ByteArray): List<Screenshot> =
|
||||||
val string = byteArray.toString()
|
if (String(byteArray) == "") emptyList()
|
||||||
return if (string == "") emptyList()
|
else String(byteArray).split(";").map { Screenshot.fromJson(it) }
|
||||||
else string.split(",").mapNotNull { byteArray.jsonParse { Screenshot.deserialize(it) } }
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmName("screenshotsToByteArray")
|
@JvmName("screenshotsToByteArray")
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toByteArray(screenshots: List<Screenshot>) = jsonGenerate {
|
fun toByteArray(screenshots: List<Screenshot>) =
|
||||||
screenshots.forEach { item -> item.serialize(it) }.toString().toByteArray()
|
if (screenshots.isNotEmpty()) screenshots.joinToString(";") { it.toJSON() }.toByteArray()
|
||||||
}
|
else "".toByteArray()
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toDonates(byteArray: ByteArray): List<Donate> {
|
fun toDonates(byteArray: ByteArray): List<Donate> =
|
||||||
val string = byteArray.toString()
|
if (String(byteArray) == "") emptyList()
|
||||||
return if (string == "") emptyList()
|
else String(byteArray).split(";").map { Donate.fromJson(it) }
|
||||||
else string.split(",").mapNotNull { byteArray.jsonParse { Donate.deserialize(it) } }
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmName("donatesToByteArray")
|
@JvmName("donatesToByteArray")
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun toByteArray(donates: List<Donate>) = jsonGenerate {
|
fun toByteArray(donates: List<Donate>) =
|
||||||
donates.forEach { item -> item.serialize(it) }.toString().toByteArray()
|
if (donates.isNotEmpty()) donates.joinToString(";") { it.toJSON() }.toByteArray()
|
||||||
}
|
else "".toByteArray()
|
||||||
}
|
}
|
@ -1,76 +1,41 @@
|
|||||||
package com.looker.droidify.entity
|
package com.looker.droidify.entity
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator
|
import kotlinx.serialization.Serializable
|
||||||
import com.fasterxml.jackson.core.JsonParser
|
import kotlinx.serialization.decodeFromString
|
||||||
import com.looker.droidify.utility.extension.json.forEachKey
|
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)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
sealed class Donate {
|
sealed class Donate {
|
||||||
|
@Serializable
|
||||||
data class Regular(val url: String) : Donate()
|
data class Regular(val url: String) : Donate()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class Bitcoin(val address: String) : Donate()
|
data class Bitcoin(val address: String) : Donate()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class Litecoin(val address: String) : Donate()
|
data class Litecoin(val address: String) : Donate()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class Flattr(val id: String) : Donate()
|
data class Flattr(val id: String) : Donate()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class Liberapay(val id: String) : Donate()
|
data class Liberapay(val id: String) : Donate()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class OpenCollective(val id: String) : Donate()
|
data class OpenCollective(val id: String) : Donate()
|
||||||
|
|
||||||
fun serialize(generator: JsonGenerator) {
|
fun toJSON() = Json.encodeToString(this)
|
||||||
when (this) {
|
|
||||||
is Regular -> {
|
|
||||||
generator.writeStringField("type", "")
|
|
||||||
generator.writeStringField("url", url)
|
|
||||||
}
|
|
||||||
is Bitcoin -> {
|
|
||||||
generator.writeStringField("type", "bitcoin")
|
|
||||||
generator.writeStringField("address", address)
|
|
||||||
}
|
|
||||||
is Litecoin -> {
|
|
||||||
generator.writeStringField("type", "litecoin")
|
|
||||||
generator.writeStringField("address", address)
|
|
||||||
}
|
|
||||||
is Flattr -> {
|
|
||||||
generator.writeStringField("type", "flattr")
|
|
||||||
generator.writeStringField("id", id)
|
|
||||||
}
|
|
||||||
is Liberapay -> {
|
|
||||||
generator.writeStringField("type", "liberapay")
|
|
||||||
generator.writeStringField("id", id)
|
|
||||||
}
|
|
||||||
is OpenCollective -> {
|
|
||||||
generator.writeStringField("type", "openCollective")
|
|
||||||
generator.writeStringField("id", id)
|
|
||||||
}
|
|
||||||
}::class
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun deserialize(parser: JsonParser): Donate? {
|
fun fromJson(json: String) = Json.decodeFromString<Donate>(json)
|
||||||
var type = ""
|
|
||||||
var url = ""
|
|
||||||
var address = ""
|
|
||||||
var id = ""
|
|
||||||
parser.forEachKey {
|
|
||||||
when {
|
|
||||||
it.string("type") -> type = valueAsString
|
|
||||||
it.string("url") -> url = valueAsString
|
|
||||||
it.string("address") -> address = valueAsString
|
|
||||||
it.string("id") -> id = valueAsString
|
|
||||||
else -> skipChildren()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return when (type) {
|
|
||||||
"" -> Regular(url)
|
|
||||||
"bitcoin" -> Bitcoin(address)
|
|
||||||
"litecoin" -> Litecoin(address)
|
|
||||||
"flattr" -> Flattr(id)
|
|
||||||
"liberapay" -> Liberapay(id)
|
|
||||||
"openCollective" -> OpenCollective(id)
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
class Screenshot(val locale: String, val type: Type, val path: String) {
|
class Screenshot(val locale: String, val type: Type, val path: String) {
|
||||||
enum class Type(val jsonName: String) {
|
enum class Type(val jsonName: String) {
|
||||||
PHONE("phone"),
|
PHONE("phone"),
|
||||||
@ -81,26 +46,9 @@ class Screenshot(val locale: String, val type: Type, val path: String) {
|
|||||||
val identifier: String
|
val identifier: String
|
||||||
get() = "$locale.${type.name}.$path"
|
get() = "$locale.${type.name}.$path"
|
||||||
|
|
||||||
fun serialize(generator: JsonGenerator) {
|
fun toJSON() = Json.encodeToString(this)
|
||||||
generator.writeStringField("locale", locale)
|
|
||||||
generator.writeStringField("type", type.jsonName)
|
|
||||||
generator.writeStringField("path", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun deserialize(parser: JsonParser): Screenshot? {
|
fun fromJson(json: String) = Json.decodeFromString<Screenshot>(json)
|
||||||
var locale = ""
|
|
||||||
var type = ""
|
|
||||||
var path = ""
|
|
||||||
parser.forEachKey {
|
|
||||||
when {
|
|
||||||
it.string("locale") -> locale = valueAsString
|
|
||||||
it.string("type") -> type = valueAsString
|
|
||||||
it.string("path") -> path = valueAsString
|
|
||||||
else -> skipChildren()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Type.values().find { it.jsonName == type }?.let { Screenshot(locale, it, path) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user