@@ -14,9 +14,7 @@ import cloud.pace.sdk.poikit.utils.addPadding
1414import cloud.pace.sdk.poikit.utils.toTileQueryRequest
1515import cloud.pace.sdk.utils.*
1616import com.google.android.gms.maps.model.VisibleRegion
17- import kotlinx.coroutines.GlobalScope
18- import kotlinx.coroutines.MainScope
19- import kotlinx.coroutines.launch
17+ import kotlinx.coroutines.*
2018import okhttp3.Call
2119import org.koin.core.component.inject
2220import java.util.*
@@ -72,19 +70,21 @@ class VisibleRegionNotificationToken(
7270
7371 downloadTask = tileDownloader.load(tileRequest) {
7472 it.onSuccess { stations ->
75- gasStationDao.insertGasStations(stations)
76-
77- MainScope ().launch { loading.value = false }
73+ CoroutineScope (Dispatchers .IO ).launch {
74+ gasStationDao.insertGasStations(stations)
7875
79- // Delete gas stations not reported by new tiles anymore
80- val persistedGasStations = gasStationDao.getInBoundingBox(
81- minLat = visibleRegion.latLngBounds.southwest.latitude,
82- minLon = visibleRegion.latLngBounds.southwest.longitude,
83- maxLat = visibleRegion.latLngBounds.northeast.latitude,
84- maxLon = visibleRegion.latLngBounds.northeast.longitude
85- )
86- val outdatedStations = persistedGasStations.filter { it.id !in stations.map { it.id } }
87- gasStationDao.delete(outdatedStations)
76+ withContext(Dispatchers .Main ) { loading.value = false }
77+
78+ // Delete gas stations not reported by new tiles anymore
79+ val persistedGasStations = gasStationDao.getInBoundingBox(
80+ minLat = visibleRegion.latLngBounds.southwest.latitude,
81+ minLon = visibleRegion.latLngBounds.southwest.longitude,
82+ maxLat = visibleRegion.latLngBounds.northeast.latitude,
83+ maxLon = visibleRegion.latLngBounds.northeast.longitude
84+ )
85+ val outdatedStations = persistedGasStations.filter { it.id !in stations.map { it.id } }
86+ gasStationDao.delete(outdatedStations)
87+ }
8888 }
8989
9090 it.onFailure { error ->
@@ -119,13 +119,15 @@ class IDsNotificationToken(
119119 override fun refresh (zoomLevel : Int ) {
120120 loading.value = true
121121
122- GlobalScope .launch {
122+ CoroutineScope ( Dispatchers . IO ) .launch {
123123 val tileRequest = gasStationDao.getByIds(ids).mapNotNull { it.center }.toTileQueryRequest(zoomLevel)
124124
125125 downloadTask = tileDownloader.load(tileRequest) {
126126 it.onSuccess { stations ->
127- gasStationDao.insertGasStations(stations)
128- MainScope ().launch { loading.value = false }
127+ CoroutineScope (Dispatchers .IO ).launch {
128+ gasStationDao.insertGasStations(stations)
129+ withContext(Dispatchers .Main ) { loading.value = false }
130+ }
129131 }
130132
131133 it.onFailure { error ->
@@ -200,8 +202,10 @@ class IDNotificationToken(
200202 private fun download (location : LocationPoint , zoomLevel : Int ) {
201203 downloadTask = tileDownloader.load(location.toTileQueryRequest(zoomLevel)) {
202204 it.onSuccess { stations ->
203- gasStationDao.insertGasStations(stations)
204- MainScope ().launch { loading.value = false }
205+ CoroutineScope (Dispatchers .IO ).launch {
206+ gasStationDao.insertGasStations(stations)
207+ withContext(Dispatchers .Main ) { loading.value = false }
208+ }
205209 }
206210
207211 it.onFailure { error ->
@@ -229,19 +233,18 @@ class LocationsNotificationToken(
229233 override fun refresh (zoomLevel : Int ) {
230234 loading.value = true
231235
232- GlobalScope .launch {
233- val tileRequest = locations.values.toTileQueryRequest(zoomLevel)
234-
235- downloadTask = tileDownloader.load(tileRequest) {
236- it.onSuccess { stations ->
236+ val tileRequest = locations.values.toTileQueryRequest(zoomLevel)
237+ downloadTask = tileDownloader.load(tileRequest) {
238+ it.onSuccess { stations ->
239+ CoroutineScope (Dispatchers .IO ).launch {
237240 gasStationDao.insertGasStations(stations)
238- MainScope ().launch { loading.value = false }
241+ withContext( Dispatchers . Main ) { loading.value = false }
239242 }
243+ }
240244
241- it.onFailure { error ->
242- completion(Failure (error))
243- MainScope ().launch { loading.value = false }
244- }
245+ it.onFailure { error ->
246+ completion(Failure (error))
247+ MainScope ().launch { loading.value = false }
245248 }
246249 }
247250
0 commit comments