fix passing data to ChargepriceFragment

(not yet implemented for OpenChargeMap)
This commit is contained in:
johan12345
2021-06-20 20:07:02 +02:00
parent f76b19e818
commit af0fd8bf69
5 changed files with 29 additions and 18 deletions

View File

@@ -84,7 +84,7 @@ interface GoingElectricApi {
companion object {
private val cacheSize = 10L * 1024 * 1024 // 10MB
val moshi = Moshi.Builder()
private val moshi = Moshi.Builder()
.add(ChargepointListItemJsonAdapterFactory())
.add(JsonObjectOrFalseAdapter.Factory())
.add(HoursAdapter())

View File

@@ -43,7 +43,7 @@ interface OpenChargeMapApi {
companion object {
private val cacheSize = 10L * 1024 * 1024 // 10MB
val moshi = Moshi.Builder()
private val moshi = Moshi.Builder()
.add(ZonedDateTimeAdapter())
.build()

View File

@@ -20,7 +20,6 @@ import net.vonforst.evmap.MapsActivity
import net.vonforst.evmap.R
import net.vonforst.evmap.adapter.ChargepriceAdapter
import net.vonforst.evmap.adapter.CheckableConnectorAdapter
import net.vonforst.evmap.api.goingelectric.GoingElectricApi
import net.vonforst.evmap.databinding.FragmentChargepriceBinding
import net.vonforst.evmap.model.ChargeLocation
import net.vonforst.evmap.model.Chargepoint
@@ -51,7 +50,7 @@ class ChargepriceFragment : DialogFragment() {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
): View {
binding = DataBindingUtil.inflate(
inflater,
R.layout.fragment_chargeprice, container, false
@@ -87,8 +86,7 @@ class ChargepriceFragment : DialogFragment() {
(requireActivity() as MapsActivity).appBarConfiguration
)
val jsonAdapter = GoingElectricApi.moshi.adapter(ChargeLocation::class.java)
val charger = jsonAdapter.fromJson(requireArguments().getString(ARG_CHARGER)!!)!!
val charger = requireArguments().getParcelable<ChargeLocation>(ARG_CHARGER)!!
vm.charger.value = charger
if (vm.chargepoint.value == null) {
vm.chargepoint.value = charger.chargepointsMerged.get(0)
@@ -202,9 +200,9 @@ class ChargepriceFragment : DialogFragment() {
fun showCharger(charger: ChargeLocation): Bundle {
return Bundle().apply {
putString(
putParcelable(
ARG_CHARGER,
GoingElectricApi.moshi.adapter(ChargeLocation::class.java).toJson(charger)
charger
)
}
}

View File

@@ -6,6 +6,7 @@ import androidx.core.text.HtmlCompat
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.parcelize.Parcelize
import net.vonforst.evmap.R
import net.vonforst.evmap.adapter.Equatable
import net.vonforst.evmap.api.StringProvider
@@ -23,6 +24,7 @@ import kotlin.math.floor
sealed class ChargepointListItem
@Entity
@Parcelize
data class ChargeLocation(
@PrimaryKey val id: Long,
val name: String,
@@ -45,7 +47,7 @@ data class ChargeLocation(
@Embedded val openinghours: OpeningHours?,
@Embedded val cost: Cost?,
val license: String?
) : ChargepointListItem(), Equatable {
) : ChargepointListItem(), Equatable, Parcelable {
/**
* maximum power available from this charger.
*/
@@ -104,12 +106,13 @@ data class ChargeLocation(
}
}
@Parcelize
data class Cost(
val freecharging: Boolean? = null,
val freeparking: Boolean? = null,
val descriptionShort: String? = null,
val descriptionLong: String? = null
) {
) : Parcelable {
fun getStatusText(ctx: Context, emoji: Boolean = false): CharSequence {
if (freecharging != null && freeparking != null) {
val charging =
@@ -131,11 +134,12 @@ data class Cost(
}
}
@Parcelize
data class OpeningHours(
val twentyfourSeven: Boolean,
val description: String?,
@Embedded val days: OpeningHoursDays?
) {
) : Parcelable {
val isEmpty: Boolean
get() = description == "Leider noch keine Informationen zu Öffnungszeiten vorhanden."
&& days == null && !twentyfourSeven
@@ -173,6 +177,7 @@ data class OpeningHours(
}
}
@Parcelize
data class OpeningHoursDays(
@Embedded(prefix = "mo") val monday: Hours,
@Embedded(prefix = "tu") val tuesday: Hours,
@@ -182,7 +187,7 @@ data class OpeningHoursDays(
@Embedded(prefix = "sa") val saturday: Hours,
@Embedded(prefix = "su") val sunday: Hours,
@Embedded(prefix = "ho") val holiday: Hours
) {
) : Parcelable {
fun getHoursForDate(date: LocalDate): Hours {
// TODO: check for holidays
return getHoursForDayOfWeek(date.dayOfWeek)
@@ -203,10 +208,11 @@ data class OpeningHoursDays(
}
}
@Parcelize
data class Hours(
val start: LocalTime?,
val end: LocalTime?
) {
) : Parcelable {
override fun toString(): String {
if (start != null && end != null) {
val fmt = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
@@ -226,7 +232,8 @@ data class ChargeLocationCluster(
val coordinates: Coordinate
) : ChargepointListItem()
data class Coordinate(val lat: Double, val lng: Double) {
@Parcelize
data class Coordinate(val lat: Double, val lng: Double) : Parcelable {
fun formatDMS(): String {
return "${dms(lat, false)}, ${dms(lng, true)}"
}
@@ -249,18 +256,21 @@ data class Coordinate(val lat: Double, val lng: Double) {
}
}
@Parcelize
data class Address(
val city: String?,
val country: String?,
val postcode: String?,
val street: String?
) {
) : Parcelable {
override fun toString(): String {
return "${street ?: ""}, ${postcode ?: ""} ${city ?: ""}"
}
}
data class Chargepoint(val type: String, val power: Double, val count: Int) : Equatable {
@kotlinx.android.parcel.Parcelize
data class Chargepoint(val type: String, val power: Double, val count: Int) : Equatable,
Parcelable {
fun formatPower(): String {
val powerFmt = if (power - power.toInt() == 0.0) {
"%.0f".format(power)
@@ -288,7 +298,8 @@ data class Chargepoint(val type: String, val power: Double, val count: Int) : Eq
}
}
data class FaultReport(val created: Instant?, val description: String?)
@Parcelize
data class FaultReport(val created: Instant?, val description: String?) : Parcelable
@Entity
data class ChargeCard(
@@ -297,6 +308,7 @@ data class ChargeCard(
val url: String
)
@Parcelize
data class ChargeCardId(
val id: Long
)
) : Parcelable

View File

@@ -183,6 +183,7 @@ class ChargepriceViewModel(application: Application, chargepriceApiKey: String)
}
val cpStation = ChargepriceStation.fromGoingelectric(geCharger, compatibleConnectors)
// TODO: implement this for OpenChargeMap -> https://github.com/chargeprice/chargeprice-api-docs/blob/master/guides/integrate_charge_prices.md#open-charge-map-beta
loadPricesJob?.cancel()
loadPricesJob = viewModelScope.launch {