mirror of
https://github.com/ev-map/EVMap.git
synced 2026-04-29 02:24:20 -04:00
fix endless loading with filters that do not support local SQL queries
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,6 +235,7 @@ class ChargeLocationsRepository(
|
||||
val dbResult = if (filters.isNullOrEmpty()) {
|
||||
liveData {
|
||||
emit(
|
||||
Resource.success(
|
||||
chargeLocationsDao.getChargeLocationsClustered(
|
||||
bounds.southwest.latitude,
|
||||
bounds.northeast.latitude,
|
||||
@@ -244,6 +245,7 @@ class ChargeLocationsRepository(
|
||||
cacheLimitDate(api),
|
||||
zoom
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -251,7 +253,7 @@ class ChargeLocationsRepository(
|
||||
}.map {
|
||||
val t2 = System.currentTimeMillis()
|
||||
Log.d(TAG, "DB loading time: ${t2 - t1}")
|
||||
Log.d(TAG, "number of chargers: ${it.size}")
|
||||
Log.d(TAG, "number of chargers: ${it.data?.size}")
|
||||
it
|
||||
}
|
||||
val filtersSerialized =
|
||||
@@ -321,7 +323,7 @@ class ChargeLocationsRepository(
|
||||
job.join()
|
||||
progressJob.cancelAndJoin()
|
||||
}
|
||||
emit(Resource.success(dbResult.await()))
|
||||
emit(dbResult.await())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -363,6 +365,7 @@ class ChargeLocationsRepository(
|
||||
val dbResult = if (filters.isNullOrEmpty()) {
|
||||
liveData {
|
||||
emit(
|
||||
Resource.success(
|
||||
chargeLocationsDao.getChargeLocationsRadius(
|
||||
location.latitude,
|
||||
location.longitude,
|
||||
@@ -370,6 +373,7 @@ class ChargeLocationsRepository(
|
||||
api.id,
|
||||
cacheLimitDate(api)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -446,7 +450,7 @@ class ChargeLocationsRepository(
|
||||
job.join()
|
||||
progressJob.cancelAndJoin()
|
||||
}
|
||||
emit(Resource.success(dbResult.await()))
|
||||
emit(dbResult.await())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -546,7 +550,7 @@ class ChargeLocationsRepository(
|
||||
api: ChargepointApi<ReferenceData>,
|
||||
filters: FilterValues,
|
||||
bounds: LatLngBounds
|
||||
): LiveData<List<ChargeLocation>> {
|
||||
): LiveData<Resource<List<ChargeLocation>>> {
|
||||
return queryWithFilters(api, filters, boundsSpatialIndexQuery(bounds))
|
||||
}
|
||||
|
||||
@@ -555,7 +559,7 @@ class ChargeLocationsRepository(
|
||||
filters: FilterValues,
|
||||
bounds: LatLngBounds,
|
||||
zoom: Float
|
||||
): LiveData<List<ChargepointListItem>> {
|
||||
): LiveData<Resource<List<ChargepointListItem>>> {
|
||||
return queryWithFiltersClustered(api, filters, boundsSpatialIndexQuery(bounds), zoom)
|
||||
}
|
||||
|
||||
@@ -564,7 +568,7 @@ class ChargeLocationsRepository(
|
||||
filters: FilterValues,
|
||||
location: LatLng,
|
||||
radius: Double
|
||||
): LiveData<List<ChargeLocation>> {
|
||||
): LiveData<Resource<List<ChargeLocation>>> {
|
||||
val region =
|
||||
radiusSpatialIndexQuery(location, radius)
|
||||
val order =
|
||||
@@ -583,7 +587,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)
|
||||
@@ -591,12 +595,14 @@ class ChargeLocationsRepository(
|
||||
|
||||
liveData {
|
||||
emit(
|
||||
Resource.success(
|
||||
chargeLocationsDao.getChargeLocationsCustom(
|
||||
SimpleSQLiteQuery(
|
||||
sql,
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
} catch (e: NotImplementedError) {
|
||||
@@ -610,7 +616,7 @@ class ChargeLocationsRepository(
|
||||
regionSql: String,
|
||||
zoom: Float,
|
||||
orderSql: String? = null
|
||||
): LiveData<List<ChargepointListItem>> = referenceData.singleSwitchMap { refData ->
|
||||
): LiveData<Resource<List<ChargepointListItem>>> = referenceData.singleSwitchMap { refData ->
|
||||
try {
|
||||
if (zoom > CLUSTER_MAX_ZOOM_LEVEL) {
|
||||
queryWithFilters(api, filters, regionSql, orderSql).map { it }
|
||||
@@ -632,13 +638,19 @@ class ChargeLocationsRepository(
|
||||
.map { it.ids }
|
||||
.flatten(), prefs.dataSource, after)
|
||||
emit(
|
||||
Resource.success(
|
||||
clusters.filter { it.clusterCount > 1 }
|
||||
.map { it.convert() } + singleChargers
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user