diff --git a/app/src/main/java/net/vonforst/evmap/storage/CacheLiveData.kt b/app/src/main/java/net/vonforst/evmap/storage/CacheLiveData.kt index 338395ca..713bd104 100644 --- a/app/src/main/java/net/vonforst/evmap/storage/CacheLiveData.kt +++ b/app/src/main/java/net/vonforst/evmap/storage/CacheLiveData.kt @@ -16,14 +16,15 @@ import java.time.Instant * successful. */ class CacheLiveData( - cache: LiveData, + cache: LiveData>, api: LiveData>, skipApi: LiveData? = null ) : MediatorLiveData>() { - private var cacheResult: T? = null + private var cacheResult: Resource? = null private var apiResult: Resource? = null private var skipApiResult: Boolean = false + private val apiLiveData = api init { updateValue() @@ -64,9 +65,21 @@ class CacheLiveData( 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( // 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 } } diff --git a/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt b/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt index 42f618e4..dc597f7a 100644 --- a/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt +++ b/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt @@ -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, filters: FilterValues, bounds: LatLngBounds - ): LiveData> { + ): LiveData>> { return queryWithFilters(api, filters, boundsSpatialIndexQuery(bounds)) } @@ -555,7 +559,7 @@ class ChargeLocationsRepository( filters: FilterValues, bounds: LatLngBounds, zoom: Float - ): LiveData> { + ): LiveData>> { return queryWithFiltersClustered(api, filters, boundsSpatialIndexQuery(bounds), zoom) } @@ -564,7 +568,7 @@ class ChargeLocationsRepository( filters: FilterValues, location: LatLng, radius: Double - ): LiveData> { + ): LiveData>> { val region = radiusSpatialIndexQuery(location, radius) val order = @@ -583,7 +587,7 @@ class ChargeLocationsRepository( filters: FilterValues, regionSql: String, orderSql: String? = null - ): LiveData> = referenceData.singleSwitchMap { refData -> + ): LiveData>> = 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> = referenceData.singleSwitchMap { refData -> + ): LiveData>> = 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 } }