mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-06-08 00:39:54 +00:00
Add: Room Converters - halfway (2.5/5 in replacing SQLite with Room)
This commit is contained in:
parent
3477685368
commit
39ebb6fdf6
@ -340,11 +340,11 @@ object Database {
|
||||
return ObservableCursor(this, dataObservable(subject))
|
||||
}
|
||||
|
||||
private fun <T> ByteArray.jsonParse(callback: (JsonParser) -> T): T {
|
||||
fun <T> ByteArray.jsonParse(callback: (JsonParser) -> T): T {
|
||||
return Json.factory.createParser(this).use { it.parseDictionary(callback) }
|
||||
}
|
||||
|
||||
private fun jsonGenerate(callback: (JsonGenerator) -> Unit): ByteArray {
|
||||
fun jsonGenerate(callback: (JsonGenerator) -> Unit): ByteArray {
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
Json.factory.createGenerator(outputStream).use { it.writeDictionary(callback) }
|
||||
return outputStream.toByteArray()
|
||||
|
@ -4,6 +4,7 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
@ -14,6 +15,7 @@ import androidx.room.RoomDatabase
|
||||
Lock::class
|
||||
], version = 1
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class DatabaseX : RoomDatabase() {
|
||||
// TODO add the DAOs for the tables
|
||||
|
||||
|
@ -3,6 +3,9 @@ package com.looker.droidify.database
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import androidx.room.TypeConverter
|
||||
import com.looker.droidify.database.Database.jsonGenerate
|
||||
import com.looker.droidify.database.Database.jsonParse
|
||||
import com.looker.droidify.entity.Product
|
||||
import com.looker.droidify.entity.ProductItem
|
||||
import com.looker.droidify.entity.Repository
|
||||
@ -65,3 +68,58 @@ class Lock {
|
||||
|
||||
var version_code = 0
|
||||
}
|
||||
|
||||
class Converters {
|
||||
@TypeConverter
|
||||
fun toRepository(byteArray: ByteArray): Repository {
|
||||
return byteArray.jsonParse {
|
||||
Repository.deserialize(
|
||||
0,//id,
|
||||
it
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toByteArray(repository: Repository): ByteArray {
|
||||
return jsonGenerate(repository::serialize)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toProduct(byteArray: ByteArray): Product {
|
||||
return byteArray.jsonParse {
|
||||
Product.deserialize(
|
||||
0,//repository_id,
|
||||
"",//description,
|
||||
it
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toByteArray(product: Product): ByteArray {
|
||||
return jsonGenerate(product::serialize)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toProductItem(byteArray: ByteArray): ProductItem {
|
||||
return byteArray.jsonParse {
|
||||
ProductItem.deserialize(
|
||||
0,//repository_id,
|
||||
"",//package_name,
|
||||
"",//name,
|
||||
"",//summary,
|
||||
"",//version,
|
||||
true,//compatible,
|
||||
true,//canUpdate,
|
||||
0,//matchRank,
|
||||
it
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toByteArray(productItem: ProductItem): ByteArray {
|
||||
return jsonGenerate(productItem::serialize)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user