Add: acronyms models

This commit is contained in:
Carlos Martinez
2021-06-16 00:34:11 -04:00
parent 0115216494
commit 205de0ef8d
7 changed files with 85 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ apply from: '../base.gradle'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
dependencies {
implementation project(':core')

View File

@@ -0,0 +1,51 @@
package dev.carlos.acronyms.models
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class ShortformRemote(
@SerializedName("sf") val value: String,
@SerializedName("lfs") val results: List<LongformRemote>
) : Parcelable {
@Parcelize
data class LongformRemote(
@SerializedName("lf") val value: String,
@SerializedName("freq") val corpusFrequency: Int,
@SerializedName("since") val since: Int
) : Parcelable
}
@Entity(tableName = ShortformEntity.TABLE_NAME, indices = [Index(value = ["value"], unique = true)])
@TypeConverters(LongformListConverter::class)
data class ShortformEntity(
@PrimaryKey
val value: String,
val results: List<LongformEntity>
) {
data class LongformEntity(
val value: String,
val corpusFrequency: Int,
val since: Int
)
companion object {
const val TABLE_NAME = "shortforms"
}
}
data class ShortformModel(
val value: String,
val results: List<LongformModel>
) {
data class LongformModel(
val value: String,
val corpusFrequency: Int,
val since: Int
)
}

View File

@@ -0,0 +1,24 @@
package dev.carlos.acronyms.models
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
class LongformListConverter {
@TypeConverter
fun fromString(value: String): List<ShortformEntity.LongformEntity> {
val listType = object : TypeToken<List<ShortformEntity.LongformEntity>>() {}.type
return Gson().fromJson(value, listType)
}
@TypeConverter
fun fromList(list: List<ShortformEntity.LongformEntity>) = Gson().toJson(list)
}
fun ShortformRemote.toShortformEntity() = ShortformEntity(this.value, this.results.map {
ShortformEntity.LongformEntity(it.value, it.corpusFrequency, it.since)
})
fun ShortformEntity.toShortformModel() = ShortformModel(this.value, this.results.map {
ShortformModel.LongformModel(it.value, it.corpusFrequency, it.since)
})