1
1
package com.mapbox.search.sample.api
2
2
3
3
import android.os.Bundle
4
- import android.os.Handler
5
- import android.os.Looper
6
- import android.util.Log
7
4
import com.mapbox.annotation.MapboxExperimental
8
5
import com.mapbox.common.Cancelable
9
6
import com.mapbox.common.TileRegionLoadOptions
@@ -12,6 +9,9 @@ import com.mapbox.geojson.Point
12
9
import com.mapbox.search.common.AsyncOperationTask
13
10
import com.mapbox.search.common.ev.EvConnectorType
14
11
import com.mapbox.search.offline.OfflineEvSearchOptions
12
+ import com.mapbox.search.offline.OfflineIndexChangeEvent
13
+ import com.mapbox.search.offline.OfflineIndexChangeEvent.EventType
14
+ import com.mapbox.search.offline.OfflineIndexErrorEvent
15
15
import com.mapbox.search.offline.OfflineResponseInfo
16
16
import com.mapbox.search.offline.OfflineSearchCallback
17
17
import com.mapbox.search.offline.OfflineSearchEngine
@@ -20,7 +20,6 @@ import com.mapbox.search.offline.OfflineSearchOptions
20
20
import com.mapbox.search.offline.OfflineSearchResult
21
21
import com.mapbox.search.sample.R
22
22
import java.net.URI
23
- import java.util.concurrent.TimeUnit
24
23
25
24
@OptIn(MapboxExperimental ::class )
26
25
class OfflineEvSearchKotlinExampleActivity : BaseKotlinExampleActivity () {
@@ -34,13 +33,26 @@ class OfflineEvSearchKotlinExampleActivity : BaseKotlinExampleActivity() {
34
33
private val searchCallback = object : OfflineSearchCallback {
35
34
36
35
override fun onResults (results : List <OfflineSearchResult >, responseInfo : OfflineResponseInfo ) {
37
- logI(" SearchApiExample" , " Results:" , results)
38
- Log .d(" Test." , " Test. results: $results " )
36
+ printMessage(" Results:\n ${results.joinToString(separator = " \n " ) { it.toPrettyString() }} " )
39
37
onFinished()
40
38
}
41
39
42
40
override fun onError (e : Exception ) {
43
- logI(" SearchApiExample" , " Search error" , e)
41
+ printMessage(" Search error: $e " )
42
+ onFinished()
43
+ }
44
+ }
45
+
46
+ private val onIndexChangeListener = object : OfflineSearchEngine .OnIndexChangeListener {
47
+ override fun onIndexChange (event : OfflineIndexChangeEvent ) {
48
+ if (event.regionId == tileRegionId && (event.type == EventType .ADD || event.type == EventType .UPDATE )) {
49
+ printMessage(" $tileRegionId was successfully added or updated" )
50
+ startSearch()
51
+ }
52
+ }
53
+
54
+ override fun onError (event : OfflineIndexErrorEvent ) {
55
+ printMessage(" Offline index error: $event " )
44
56
onFinished()
45
57
}
46
58
}
@@ -70,18 +82,8 @@ class OfflineEvSearchKotlinExampleActivity : BaseKotlinExampleActivity() {
70
82
}
71
83
72
84
override fun startExample () {
73
- tileStore.getAllTileRegions { regions ->
74
- regions.onValue { value ->
75
- logI(" SearchApiExample" , " Available regions: $value " )
76
- downloadTiles()
77
- }.onError { error ->
78
- logI(" SearchApiExample" , " Unable to get tile regions: $error " )
79
- downloadTiles()
80
- }
81
- }
82
- }
85
+ searchEngine.addOnIndexChangeListener(onIndexChangeListener)
83
86
84
- private fun downloadTiles () {
85
87
val tileRegionLoadOptions = TileRegionLoadOptions
86
88
.Builder ()
87
89
.descriptors(descriptors)
@@ -93,14 +95,13 @@ class OfflineEvSearchKotlinExampleActivity : BaseKotlinExampleActivity() {
93
95
tileRegionId,
94
96
tileRegionLoadOptions,
95
97
{ progress ->
96
- logI( " SearchApiExample " , " Loading progress: $progress " )
98
+ printMessage( " Loading progress: $progress " )
97
99
},
98
100
{ result ->
99
101
if (result.isValue) {
100
- logI(" SearchApiExample" , " Tiles successfully loaded: ${result.value} " )
101
- startSearchDelayed()
102
+ printMessage(" Tiles successfully loaded: ${result.value} " )
102
103
} else {
103
- logI( " SearchApiExample " , " Tiles loading error: ${result.error} " )
104
+ printMessage( " Tiles loading error: ${result.error} " )
104
105
}
105
106
}
106
107
)
@@ -133,16 +134,8 @@ class OfflineEvSearchKotlinExampleActivity : BaseKotlinExampleActivity() {
133
134
)
134
135
}
135
136
136
- private fun startSearchDelayed () {
137
- // Workaround for an issue with OnIndexChangeListener
138
- // Allow time for the OfflineSearchEngine to initialize the downloaded tiles
139
- Handler (Looper .getMainLooper()).postDelayed(
140
- ::startSearch,
141
- TimeUnit .SECONDS .toMillis(1 )
142
- )
143
- }
144
-
145
137
override fun onDestroy () {
138
+ searchEngine.removeOnIndexChangeListener(onIndexChangeListener)
146
139
tilesLoadingTask?.cancel()
147
140
searchRequestTask?.cancel()
148
141
super .onDestroy()
0 commit comments