@@ -3,14 +3,11 @@ package cloud.pace.sdk.poikit.poi
33import androidx.lifecycle.LiveData
44import androidx.lifecycle.MutableLiveData
55import androidx.lifecycle.Observer
6- import cloud.pace.sdk.poikit.POIKit
76import cloud.pace.sdk.poikit.database.GasStationDAO
87import cloud.pace.sdk.poikit.poi.download.TileDownloader
98import cloud.pace.sdk.poikit.poi.download.TileQueryRequestOuterClass.TileQueryRequest.*
109import cloud.pace.sdk.poikit.utils.POIKitConfig
11- import cloud.pace.sdk.poikit.utils.ZoomException
12- import cloud.pace.sdk.poikit.utils.diameter
13- import cloud.pace.sdk.poikit.utils.zoomToDiameter
10+ import cloud.pace.sdk.poikit.utils.addPadding
1411import cloud.pace.sdk.utils.CloudSDKKoinComponent
1512import cloud.pace.sdk.utils.Completion
1613import cloud.pace.sdk.utils.Failure
@@ -29,7 +26,7 @@ open class PoiKitObserverToken : CloudSDKKoinComponent {
2926 val loading = MutableLiveData <Boolean >()
3027 var lastRefreshTime: Date ? = null
3128
32- open fun refresh () {
29+ open fun refresh (zoomLevel : Int = POIKitConfig . ZOOMLEVEL ) {
3330 lastRefreshTime = Date ()
3431 }
3532
@@ -38,7 +35,7 @@ open class PoiKitObserverToken : CloudSDKKoinComponent {
3835
3936class VisibleRegionNotificationToken (
4037 val visibleRegion : VisibleRegion ,
41- withMaxPoiSearchBoxSize : Boolean ,
38+ padding : Double ,
4239 private val gasStationDao : GasStationDAO ,
4340 private val completion : (Completion <List <PointOfInterest >>) -> Unit
4441) : PoiKitObserverToken() {
@@ -48,37 +45,28 @@ class VisibleRegionNotificationToken(
4845 private var downloadTask: Call ? = null
4946
5047 init {
51- if (withMaxPoiSearchBoxSize && visibleRegion.diameter() > POIKit .maxPoiSearchBoxSize) {
52- completion(Failure (ZoomException ()))
53- } else {
54- // load all the points that are around a certain radius of the visible center
55- val regionToLoad = if (withMaxPoiSearchBoxSize) {
56- visibleRegion.zoomToDiameter(POIKit .maxPoiSearchBoxSize)
57- } else {
58- visibleRegion
59- }
60- gasStations = gasStationDao.getInBoundingBoxLive(
61- minLat = regionToLoad.latLngBounds.southwest.latitude,
62- minLon = regionToLoad.latLngBounds.southwest.longitude,
63- maxLat = regionToLoad.latLngBounds.northeast.latitude,
64- maxLon = regionToLoad.latLngBounds.northeast.longitude
65- )
66-
67- gasStationsObserver = Observer {
68- completion(Success (it))
69- }
70-
71- gasStationsObserver?.let { gasStations?.observeForever(it) }
48+ // load all the points that are around a certain radius of the visible center
49+ val regionToLoad = visibleRegion.addPadding(padding)
50+
51+ gasStations = gasStationDao.getInBoundingBoxLive(
52+ minLat = regionToLoad.latLngBounds.southwest.latitude,
53+ minLon = regionToLoad.latLngBounds.southwest.longitude,
54+ maxLat = regionToLoad.latLngBounds.northeast.latitude,
55+ maxLon = regionToLoad.latLngBounds.northeast.longitude
56+ )
57+
58+ gasStationsObserver = Observer {
59+ completion(Success (it))
7260 }
61+
62+ gasStationsObserver?.let { gasStations?.observeForever(it) }
7363 }
7464
75- override fun refresh () {
76- // TODO: Calculate best zoom level based on the diameter of the request + meta data which discribes what is available at what zoom level
65+ override fun refresh (zoomLevel : Int ) {
7766 if (gasStationsObserver == null ) return
7867
7968 loading.value = true
8069
81- val zoomLevel = POIKitConfig .ZOOMLEVEL
8270 val northEast = visibleRegion.latLngBounds.northeast.toLocationPoint().tileInfo(zoom = zoomLevel)
8371 val southWest = visibleRegion.latLngBounds.southwest.toLocationPoint().tileInfo(zoom = zoomLevel)
8472
@@ -116,7 +104,7 @@ class VisibleRegionNotificationToken(
116104 }
117105 }
118106
119- super .refresh()
107+ super .refresh(zoomLevel )
120108 }
121109
122110 override fun invalidate () {
@@ -139,12 +127,9 @@ class IDsNotificationToken(
139127 gasStations.observeForever(gasStationsObserver)
140128 }
141129
142- override fun refresh () {
130+ override fun refresh (zoomLevel : Int ) {
143131 loading.value = true
144132
145- // TODO: Calculate best zoom level based on the diameter of the request + meta data which discribes what is available at what zoom level
146- val zoomLevel = POIKitConfig .ZOOMLEVEL
147-
148133 GlobalScope .launch {
149134 // Build request from bounding box
150135 val tiles = gasStationDao
@@ -177,7 +162,7 @@ class IDsNotificationToken(
177162 }
178163 }
179164
180- super .refresh()
165+ super .refresh(zoomLevel )
181166 }
182167
183168 override fun invalidate () {
0 commit comments