add filter by minimum number of connectors (#9)

This commit is contained in:
Johan von Forstner
2020-05-09 12:48:28 +02:00
parent 22744da54b
commit cb6abe53fc
6 changed files with 40 additions and 27 deletions

View File

@@ -6,6 +6,7 @@ import com.squareup.moshi.Moshi
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
@@ -13,7 +14,7 @@ import retrofit2.http.Query
interface GoingElectricApi {
@GET("chargepoints/")
fun getChargepoints(
suspend fun getChargepoints(
@Query("sw_lat") swlat: Double, @Query("sw_lng") sw_lng: Double,
@Query("ne_lat") ne_lat: Double, @Query("ne_lng") ne_lng: Double,
@Query("clustering") clustering: Boolean,
@@ -22,7 +23,7 @@ interface GoingElectricApi {
@Query("freecharging") freecharging: Boolean,
@Query("freeparking") freeparking: Boolean,
@Query("min_power") minPower: Int
): Call<ChargepointList>
): Response<ChargepointList>
@GET("chargepoints/")
fun getChargepointDetail(@Query("ge_id") id: Long): Call<ChargepointList>

View File

@@ -18,7 +18,13 @@ fun getFilters(application: Application): List<Filter<FilterValue>> {
return listOf(
BooleanFilter(application.getString(R.string.filter_free), "freecharging"),
BooleanFilter(application.getString(R.string.filter_free_parking), "freeparking"),
SliderFilter(application.getString(R.string.filter_min_power), "min_power", 350)
SliderFilter(application.getString(R.string.filter_min_power), "min_power", 350, "kW"),
SliderFilter(
application.getString(R.string.filter_min_connectors),
"min_connectors",
10,
""
)
)
}
@@ -81,7 +87,8 @@ data class MultipleChoiceFilter(
data class SliderFilter(
override val name: String,
override val key: String,
val max: Int
val max: Int,
val unit: String
) : Filter<SliderFilterValue>() {
override val valueClass: KClass<SliderFilterValue> = SliderFilterValue::class
override fun defaultValue() = SliderFilterValue(key, 0)

View File

@@ -130,46 +130,49 @@ class MapViewModel(application: Application, geApiKey: String) : AndroidViewMode
chargepoints.value = Resource.loading(chargepoints.value?.data)
val bounds = mapPosition.bounds
val zoom = mapPosition.zoom
getChargepointsWithFilters(bounds, zoom, filters).enqueue(object :
Callback<ChargepointList> {
override fun onFailure(call: Call<ChargepointList>, t: Throwable) {
chargepoints.value = Resource.error(t.message, chargepoints.value?.data)
t.printStackTrace()
}
override fun onResponse(
call: Call<ChargepointList>,
response: Response<ChargepointList>
) {
if (!response.isSuccessful || response.body()!!.status != "ok") {
chargepoints.value =
Resource.error(response.message(), chargepoints.value?.data)
} else {
chargepoints.value = Resource.success(response.body()!!.chargelocations)
}
}
})
viewModelScope.launch {
chargepoints.value = getChargepointsWithFilters(bounds, zoom, filters)
}
}
private fun getChargepointsWithFilters(
private suspend fun getChargepointsWithFilters(
bounds: LatLngBounds,
zoom: Float,
filters: List<FilterWithValue<out FilterValue>>
): Call<ChargepointList> {
): Resource<List<ChargepointListItem>> {
val freecharging =
(filters.find { it.value.key == "freecharging" }!!.value as BooleanFilterValue).value
val freeparking =
(filters.find { it.value.key == "freeparking" }!!.value as BooleanFilterValue).value
val minPower =
(filters.find { it.value.key == "min_power" }!!.value as SliderFilterValue).value
val minConnectors =
(filters.find { it.value.key == "min_connectors" }!!.value as SliderFilterValue).value
return api.getChargepoints(
val response = api.getChargepoints(
bounds.southwest.latitude, bounds.southwest.longitude,
bounds.northeast.latitude, bounds.northeast.longitude,
clustering = zoom < 13, zoom = zoom,
clusterDistance = 70, freecharging = freecharging, minPower = minPower,
freeparking = freeparking
)
if (!response.isSuccessful || response.body()!!.status != "ok") {
return Resource.error(response.message(), chargepoints.value?.data)
} else {
val data = response.body()!!.chargelocations.filter { it ->
// apply filters which GoingElectric does not support natively
if (it is ChargeLocation) {
it.chargepoints
.filter { it.power >= minPower }
.sumBy { it.count } >= minConnectors
} else {
true
}
}
return Resource.success(data)
}
}
private suspend fun loadAvailability(charger: ChargeLocation) {

View File

@@ -57,7 +57,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@{progress + &quot; kW&quot;}"
android:text="@{String.valueOf(progress) + ' ' + ((SliderFilter) item.filter).unit}"
app:layout_constraintBottom_toBottomOf="@+id/seekBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/seekBar"

View File

@@ -47,4 +47,5 @@
<string name="filter_free">Nur kostenlose Ladesäulen</string>
<string name="filter_min_power">Minimale Leistung</string>
<string name="filter_free_parking">Nur Ladesäulen mit kostenlosem Parkplatz</string>
<string name="filter_min_connectors">Mindestzahl Anschlüsse</string>
</resources>

View File

@@ -46,4 +46,5 @@
<string name="filter_free">Only free chargers</string>
<string name="filter_min_power">Minimum power</string>
<string name="filter_free_parking">Only chargers with free parking</string>
<string name="filter_min_connectors">Minimum number of connectors</string>
</resources>