Commit 954c2e47 authored by Mugurell's avatar Mugurell
Browse files

For #7627 - Add hasRegionCached() to LocationService

This method can be used by clients to better estimate if fetchRegion() is
expected to take very little time as the region is already cached or if it can
take a bit longer since there are multiple operations / longer running
operations needed to actually fetch the region.

In the case of MozillaLocationService if the region is not already cached the
process of fetching the region involves a REST call with a connect / read
timeout of 10 seconds.
parent b54541be
......@@ -13,6 +13,11 @@ interface LocationService {
*/
suspend fun fetchRegion(readFromCache: Boolean = true): Region?
/**
* Get if there is already a cached region.
*/
fun hasRegionCached(): Boolean
/**
* A [Region] returned by the location service.
*
......@@ -34,6 +39,7 @@ interface LocationService {
*/
fun dummy() = object : LocationService {
override suspend fun fetchRegion(readFromCache: Boolean): Region? = null
override fun hasRegionCached(): Boolean = false
}
}
}
......@@ -72,6 +72,15 @@ class MozillaLocationService(
client.fetchRegion(regionServiceUrl)?.also { context.cacheRegion(it) }
}
/**
* Get if there is already a cached region.
* This does not guarantee we have the current actual region but only the last value
* which may be obsolete at this time.
*/
override fun hasRegionCached(): Boolean {
return context.hasCachedRegion()
}
}
private fun Context.loadCachedRegion(): LocationService.Region? {
......@@ -87,6 +96,11 @@ private fun Context.loadCachedRegion(): LocationService.Region? {
}
}
private fun Context.hasCachedRegion(): Boolean {
val cache = regionCache()
return cache.contains(KEY_COUNTRY_CODE) && cache.contains(KEY_COUNTRY_NAME)
}
private fun Context.cacheRegion(region: LocationService.Region) {
regionCache()
.edit()
......
......@@ -22,6 +22,9 @@ permalink: /changelog/
* **concept-engine**
* Added `EngineSession.goToHistoryIndex` to jump to a specific index in a session's history.
* **service-location**
* `LocationService.hasRegionCached()` is introduced to query if the region is already cached and a long running operation to fetch the region is not needed.
# 49.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v48.0.0...v49.0.0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment