OpenChargeMap: implement charger photos

This commit is contained in:
johan12345
2021-06-23 18:25:32 +02:00
parent af0fd8bf69
commit 9b80f03993
2 changed files with 54 additions and 4 deletions

View File

@@ -60,4 +60,17 @@ fun <T> Collection<Iterable<T>>.cartesianProduct(): Set<Set<T>> =
if (isEmpty()) emptySet()
else drop(1).fold(first().map(::setOf)) { acc, iterable ->
acc.flatMap { list -> iterable.map(list::plus) }
}.toSet()
}.toSet()
fun max(a: Int?, b: Int?): Int? {
/**
* Returns the maximum of two values of both are non-null,
* otherwise the non-null value or null
*/
return if (a != null && b != null) {
max(a, b)
} else {
a ?: b
}
}

View File

@@ -4,6 +4,8 @@ import androidx.room.Entity
import androidx.room.PrimaryKey
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.parcelize.Parcelize
import net.vonforst.evmap.max
import net.vonforst.evmap.model.*
import java.time.ZonedDateTime
@@ -27,7 +29,8 @@ data class OCMChargepoint(
@Json(name = "NumberOfPoints") val numPoints: Int?,
@Json(name = "GeneralComments") val generalComments: String?,
@Json(name = "OperatorInfo") val operatorInfo: OCMOperator?,
@Json(name = "DataProvider") val dataProvider: OCMDataProvider?
@Json(name = "DataProvider") val dataProvider: OCMDataProvider?,
@Json(name = "MediaItems") val mediaItems: List<OCMMediaItem>?
) {
fun convert(refData: OCMReferenceData) = ChargeLocation(
id,
@@ -45,7 +48,7 @@ data class OCMChargepoint(
generalComments,
null,
addressInfo.accessComments,
null, // TODO: MediaItems,
mediaItems?.mapNotNull { it.convert() },
null,
null,
cost?.let { Cost(descriptionShort = it) },
@@ -154,4 +157,38 @@ data class OCMOperator(
@Json(name = "ContactEmail") val contactEmail: String?,
@Json(name = "PhonePrimaryContact") val contactTelephone1: String?,
@Json(name = "PhoneSecondaryContact") val contactTelephone2: String?,
)
)
@JsonClass(generateAdapter = true)
data class OCMMediaItem(
@Json(name = "ID") val id: Long,
@Json(name = "ItemURL") val url: String,
@Json(name = "ItemThumbnailURL") val thumbUrl: String,
@Json(name = "IsVideo") val isVideo: Boolean,
@Json(name = "IsExternalResource") val isExternalResource: Boolean,
@Json(name = "Comment") val comment: String?
) {
fun convert(): ChargerPhoto? {
if (isVideo or isExternalResource) return null
return OCMChargerPhotoAdapter(id.toString(), url, thumbUrl)
}
}
@Parcelize
private class OCMChargerPhotoAdapter(
override val id: String,
private val largeUrl: String,
private val thumbUrl: String
) : ChargerPhoto(id) {
override fun getUrl(height: Int?, width: Int?, size: Int?): String {
val maxSize = size ?: max(height, width)
val mediumUrl = thumbUrl.replace(".thmb.", ".medi.")
return when (maxSize) {
0 -> mediumUrl
in 1..100 -> thumbUrl
in 101..400 -> mediumUrl
else -> largeUrl
}
}
}