fix endless loading with filters that do not support local SQL queries

This commit is contained in:
johan12345
2025-07-27 17:36:52 +02:00
parent 88bb527972
commit 99e7ae45a8
2 changed files with 44 additions and 14 deletions

View File

@@ -16,14 +16,15 @@ import java.time.Instant
* successful.
*/
class CacheLiveData<T>(
cache: LiveData<T>,
cache: LiveData<Resource<T>>,
api: LiveData<Resource<T>>,
skipApi: LiveData<Boolean>? = null
) :
MediatorLiveData<Resource<T>>() {
private var cacheResult: T? = null
private var cacheResult: Resource<T>? = null
private var apiResult: Resource<T>? = null
private var skipApiResult: Boolean = false
private val apiLiveData = api
init {
updateValue()
@@ -64,9 +65,21 @@ class CacheLiveData<T>(
Log.d("CacheLiveData", "cache has finished loading before API")
// cache has finished loading before API
if (skipApiResult) {
value = Resource.success(cache)
value = when (cache.status) {
Status.SUCCESS -> cache
Status.ERROR -> {
Log.d("CacheLiveData", "Cache returned an error, querying API")
addSource(apiLiveData) {
apiResult = it
updateValue()
}
Resource.loading(null)
}
Status.LOADING -> cache
}
} else {
value = Resource.loading(cache)
value = Resource.loading(cache.data)
}
} else if (cache == null && api != null) {
Log.d("CacheLiveData", "API has finished loading before cache")
@@ -81,7 +94,7 @@ class CacheLiveData<T>(
// Both cache and API have finished loading
value = when (api.status) {
Status.SUCCESS -> api
Status.ERROR -> Resource.error(api.message, cache)
Status.ERROR -> Resource.error(api.message, cache.data)
Status.LOADING -> api // should not occur
}
}

View File

@@ -165,10 +165,16 @@ class ChargeLocationsRepository(
bounds.northeast.longitude,
api.id,
cacheLimitDate(api)
)
).map { Resource.success(it) }
} else {
queryWithFilters(api, filters, bounds)
}.map { ChargepointList(applyLocalClustering(it, zoom), true) }
}.map {
Resource(
it.status,
it.data?.let { ChargepointList(applyLocalClustering(it, zoom), true) },
it.message
)
}
val filtersSerialized =
filters?.filter { it.value != it.filter.defaultValue() }?.takeIf { it.isNotEmpty() }
?.serialize()
@@ -263,10 +269,16 @@ class ChargeLocationsRepository(
radiusMeters,
api.id,
cacheLimitDate(api)
)
).map { Resource.success(it) }
} else {
queryWithFilters(api, filters, location, radiusMeters)
}.map { ChargepointList(applyLocalClustering(it, zoom), true) }
}.map {
Resource(
it.status,
it.data?.let { ChargepointList(applyLocalClustering(it, zoom), true) },
it.message
)
}
val filtersSerialized =
filters?.filter { it.value != it.filter.defaultValue() }?.takeIf { it.isNotEmpty() }
?.serialize()
@@ -400,7 +412,7 @@ class ChargeLocationsRepository(
api: ChargepointApi<ReferenceData>,
filters: FilterValues,
bounds: LatLngBounds
): LiveData<List<ChargeLocation>> {
): LiveData<Resource<List<ChargeLocation>>> {
val region =
"Within(coordinates, BuildMbr(${bounds.southwest.longitude}, ${bounds.southwest.latitude}, ${bounds.northeast.longitude}, ${bounds.northeast.latitude}))"
return queryWithFilters(api, filters, region)
@@ -411,7 +423,7 @@ class ChargeLocationsRepository(
filters: FilterValues,
location: LatLng,
radius: Double
): LiveData<List<ChargeLocation>> {
): LiveData<Resource<List<ChargeLocation>>> {
val region =
"PtDistWithin(coordinates, MakePoint(${location.longitude}, ${location.latitude}, 4326), ${radius})"
val order =
@@ -424,7 +436,7 @@ class ChargeLocationsRepository(
filters: FilterValues,
regionSql: String,
orderSql: String? = null
): LiveData<List<ChargeLocation>> = referenceData.singleSwitchMap { refData ->
): LiveData<Resource<List<ChargeLocation>>> = referenceData.singleSwitchMap { refData ->
try {
val query = api.convertFiltersToSQL(filters, refData)
val after = cacheLimitDate(api)
@@ -454,9 +466,14 @@ class ChargeLocationsRepository(
sql,
null
)
)
).map { Resource.success(it) }
} catch (e: NotImplementedError) {
MutableLiveData() // in this case we cannot get a DB result
MutableLiveData(
Resource.error(
e.message,
null
)
) // in this case we cannot get a DB result
}
}