fix (de)serialization of favorites

This commit is contained in:
johan12345
2021-07-11 15:20:34 +02:00
parent d179490891
commit 236aefa34d
7 changed files with 27 additions and 9 deletions

View File

@@ -121,6 +121,7 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.9.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.12.0'
implementation 'com.squareup.moshi:moshi-adapters:1.12.0'
implementation 'moe.banana:moshi-jsonapi:3.5.0'
implementation 'moe.banana:moshi-jsonapi-retrofit-converter:3.5.0'
implementation 'io.coil-kt:coil:1.1.0'

View File

@@ -38,6 +38,8 @@ data class ChargepriceStation(
charger: ChargeLocation,
compatibleConnectors: List<String>,
): ChargepriceStation {
if (charger.chargepriceData == null) throw IllegalArgumentException()
val plugTypes =
charger.chargepriceData.plugTypes ?: charger.chargepoints.map { it.type }
return ChargepriceStation(

View File

@@ -134,7 +134,8 @@ data class GEChargerPhoto(val id: String) {
}
@Parcelize
private class GEChargerPhotoAdapter(override val id: String, private val apikey: String) :
@JsonClass(generateAdapter = true)
class GEChargerPhotoAdapter(override val id: String, val apikey: String) :
ChargerPhoto(id) {
override fun getUrl(height: Int?, width: Int?, size: Int?): String {
return "https://api.goingelectric.de/chargepoints/photo/?key=${apikey}&id=$id" +

View File

@@ -241,10 +241,11 @@ data class OCMStatusType(
)
@Parcelize
private class OCMChargerPhotoAdapter(
@JsonClass(generateAdapter = true)
class OCMChargerPhotoAdapter(
override val id: String,
private val largeUrl: String,
private val thumbUrl: String
val largeUrl: String,
val thumbUrl: String
) : ChargerPhoto(id) {
override fun getUrl(height: Int?, width: Int?, size: Int?): String {
val maxSize = size ?: max(height, width)

View File

@@ -6,6 +6,7 @@ import androidx.core.text.HtmlCompat
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.squareup.moshi.JsonClass
import kotlinx.parcelize.Parcelize
import net.vonforst.evmap.R
import net.vonforst.evmap.adapter.Equatable
@@ -47,7 +48,7 @@ data class ChargeLocation(
@Embedded val openinghours: OpeningHours?,
@Embedded val cost: Cost?,
val license: String?,
@Embedded(prefix = "chargeprice") val chargepriceData: ChargepriceData
@Embedded(prefix = "chargeprice") val chargepriceData: ChargepriceData?
) : ChargepointListItem(), Equatable, Parcelable {
/**
* maximum power available from this charger.
@@ -279,7 +280,8 @@ data class Address(
}
}
@kotlinx.android.parcel.Parcelize
@Parcelize
@JsonClass(generateAdapter = true)
data class Chargepoint(val type: String, val power: Double, val count: Int) : Equatable,
Parcelable {
fun formatPower(): String {
@@ -320,6 +322,7 @@ data class ChargeCard(
)
@Parcelize
@JsonClass(generateAdapter = true)
data class ChargeCardId(
val id: Long
) : Parcelable

View File

@@ -3,6 +3,9 @@ package net.vonforst.evmap.storage
import androidx.room.TypeConverter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import net.vonforst.evmap.api.goingelectric.GEChargerPhotoAdapter
import net.vonforst.evmap.api.openchargemap.OCMChargerPhotoAdapter
import net.vonforst.evmap.model.ChargeCardId
import net.vonforst.evmap.model.Chargepoint
import net.vonforst.evmap.model.ChargerPhoto
@@ -10,7 +13,14 @@ import java.time.Instant
import java.time.LocalTime
class Converters {
val moshi = Moshi.Builder().build()
val moshi = Moshi.Builder()
.add(
PolymorphicJsonAdapterFactory.of(ChargerPhoto::class.java, "type")
.withSubtype(GEChargerPhotoAdapter::class.java, "goingelectric")
.withSubtype(OCMChargerPhotoAdapter::class.java, "openchargemap")
.withDefaultValue(null)
)
.build()
private val chargepointListAdapter by lazy {
val type = Types.newParameterizedType(List::class.java, Chargepoint::class.java)
moshi.adapter<List<Chargepoint>>(type)
@@ -49,7 +59,7 @@ class Converters {
@TypeConverter
fun toChargerPhotoList(value: String): List<ChargerPhoto>? {
return chargerPhotoListAdapter.fromJson(value)
return chargerPhotoListAdapter.fromJson(value)?.filterNotNull()
}
@TypeConverter

View File

@@ -153,7 +153,7 @@ class ChargepriceViewModel(application: Application, chargepriceApiKey: String)
private fun getChargepricePlugType(chargepoint: Chargepoint): String {
val index = charger.value!!.chargepoints.indexOf(chargepoint)
val type = charger.value!!.chargepriceData.plugTypes?.get(index) ?: chargepoint.type
val type = charger.value!!.chargepriceData!!.plugTypes?.get(index) ?: chargepoint.type
return type
}