Loading...
Searching...
No Matches
LocationListManagerExample.kt
Go to the documentation of this file.
1import kotlinx.coroutines.delay
2import kotlinx.coroutines.runBlocking
3import kotlinx.coroutines.GlobalScope
4import kotlinx.coroutines.launch
5
6/**
7 * LocationListManager usage example for Kotlin
8 * Demonstrates working with location list management, downloading locations from server, and location information
9 */
10class LocationListManagerExample {
11 private var sdk: NavigineSdk? = null
12 private var locationListManager: LocationListManager? = null
13 private var locationListListener: LocationListListener? = null
14
15 init {
16 initializeSdk()
17 setupLocationListListener()
18 }
19
20 /**
21 * Initialize SDK and get managers
22 */
23 private fun initializeSdk() {
24 try {
25 // [kotlin_NavigineSdk_getInstance]
26 // Get SDK instance
27 sdk = NavigineSdk.getInstance()
28 // [kotlin_NavigineSdk_getInstance]
29
30 // [kotlin_NavigineSdk_setUserHash]
31 // Set user hash
32 sdk?.setUserHash("USER-HASH-HERE")
33 // [kotlin_NavigineSdk_setUserHash]
34
35 // [kotlin_NavigineSdk_setServer]
36 // Set server URL (optional)
37 sdk?.setServer("https://custom.navigine.com")
38 // [kotlin_NavigineSdk_setServer]
39
40 // [kotlin_NavigineSdk_getLocationListManager]
41 // Get LocationListManager for working with location lists
42 locationListManager = sdk?.getLocationListManager()
43 // [kotlin_NavigineSdk_getLocationListManager]
44
45 if (locationListManager != null) {
46 println("LocationListManager successfully initialized")
47 }
48 } catch (e: Exception) {
49 System.err.println("Error initializing SDK: ${e.message}")
50 }
51 }
52
53 /**
54 * Setup location list listener
55 */
56 private fun setupLocationListListener() {
57 locationListListener = object : LocationListListener() {
58 // [kotlin_LocationListListener_onLocationListLoaded]
59 override fun onLocationListLoaded(locationInfos: Map<Int, LocationInfo>) {
60 println("Location list loaded")
61 demonstrateLocationList(locationInfos)
62 }
63 // [kotlin_LocationListListener_onLocationListLoaded]
64
65 // [kotlin_LocationListListener_onLocationListFailed]
66 override fun onLocationListFailed(error: java.lang.Error) {
67 println("Location list failed: ${error.message}")
68 demonstrateErrorHandling(error)
69 }
70 // [kotlin_LocationListListener_onLocationListFailed]
71 }
72 }
73
74 /**
75 * Demonstrate LocationListManager methods
76 */
77 fun demonstrateLocationListManagerMethods() {
78 val manager = locationListManager ?: run {
79 System.err.println("LocationListManager not initialized")
80 return
81 }
82
83 val listener = locationListListener ?: return
84
85 // [kotlin_LocationListManager_addLocationListListener]
86 // Add location list listener
87 manager.addLocationListListener(listener)
88 println("Added location list listener")
89 // [kotlin_LocationListManager_addLocationListListener]
90
91 // [kotlin_LocationListManager_updateLocationList]
92 // Force reload location list
93 manager.updateLocationList()
94 println("Requested location list update")
95 // [kotlin_LocationListManager_updateLocationList]
96
97 // [kotlin_LocationListManager_getLocationList]
98 // Get current location list
99 val currentLocationList = manager.getLocationList()
100 println("Current location list contains ${currentLocationList.size} locations")
101 demonstrateLocationList(currentLocationList)
102 // [kotlin_LocationListManager_getLocationList]
103
104 // Simulate some time passing for location list updates
105 GlobalScope.launch {
106 delay(2000)
107
108 // [kotlin_LocationListManager_removeLocationListListener]
109 // Remove location list listener
110 manager.removeLocationListListener(listener)
111 println("Removed location list listener")
112 // [kotlin_LocationListManager_removeLocationListListener]
113 }
114 }
115
116 /**
117 * Demonstrate location list
118 */
119 fun demonstrateLocationList(locationInfos: Map<Int, LocationInfo>) {
120 if (locationInfos.isEmpty()) {
121 println("Location list is empty")
122 return
123 }
124
125 println("=== Location List ===")
126 locationInfos.forEach { (locationId, locationInfo) ->
127 demonstrateLocationInfo(locationInfo)
128 }
129 println("====================")
130 }
131
132 /**
133 * Demonstrate LocationInfo usage
134 */
135 fun demonstrateLocationInfo(locationInfo: LocationInfo) {
136 // [kotlin_LocationInfo_getId]
137 // Get location ID
138 val id = locationInfo.id
139 println("Location ID: $id")
140 // [kotlin_LocationInfo_getId]
141
142 // [kotlin_LocationInfo_getVersion]
143 // Get location version
144 val version = locationInfo.version
145 println("Location version: $version")
146 // [kotlin_LocationInfo_getVersion]
147
148 // [kotlin_LocationInfo_getName]
149 // Get location name
150 val name = locationInfo.name
151 println("Location name: $name")
152 // [kotlin_LocationInfo_getName]
153
154 println("---")
155 }
156
157 /**
158 * Demonstrate error handling
159 */
160 fun demonstrateErrorHandling(error: java.lang.Error) {
161 println("Handling location list error:")
162 println(" Error message: ${error.message}")
163 println(" Error type: ${error.javaClass.simpleName}")
164 }
165
166 /**
167 * Demonstrate advanced location list management features
168 */
169 suspend fun demonstrateAdvancedLocationListFeatures() {
170 println("=== Advanced Location List Features ===")
171
172 val manager = locationListManager ?: return
173
174 // Add multiple listeners
175 val listener1 = object : LocationListListener() {
176 override fun onLocationListLoaded(locationInfos: Map<Int, LocationInfo>) {
177 println("Listener 1: Location list loaded - ${locationInfos.size} locations")
178 }
179
180 override fun onLocationListFailed(error: java.lang.Error) {
181 println("Listener 1: Location list failed - ${error.message}")
182 }
183 }
184
185 val listener2 = object : LocationListListener() {
186 override fun onLocationListLoaded(locationInfos: Map<Int, LocationInfo>) {
187 println("Listener 2: Location list loaded - ${locationInfos.size} locations")
188 }
189
190 override fun onLocationListFailed(error: java.lang.Error) {
191 println("Listener 2: Location list failed - ${error.message}")
192 }
193 }
194
195 // [kotlin_LocationListManager_addLocationListListener_1]
196 // Add first location list listener
197 manager.addLocationListListener(listener1)
198 println("Added first location list listener")
199 // [kotlin_LocationListManager_addLocationListListener_1]
200
201 delay(1000)
202
203 // [kotlin_LocationListManager_addLocationListListener_2]
204 // Add second location list listener
205 manager.addLocationListListener(listener2)
206 println("Added second location list listener")
207 // [kotlin_LocationListManager_addLocationListListener_2]
208
209 // Force update to trigger listeners
210 manager.updateLocationList()
211
212 // Wait for location list updates
213 delay(3000)
214
215 // Remove listeners
216 // [kotlin_LocationListManager_removeLocationListListener_1]
217 // Remove first location list listener
218 manager.removeLocationListListener(listener1)
219 println("Removed first location list listener")
220 // [kotlin_LocationListManager_removeLocationListListener_1]
221
222 delay(1000)
223
224 // [kotlin_LocationListManager_removeLocationListListener_2]
225 // Remove second location list listener
226 manager.removeLocationListListener(listener2)
227 println("Removed second location list listener")
228 // [kotlin_LocationListManager_removeLocationListListener_2]
229 }
230
231 /**
232 * Demonstrate location list simulation
233 */
234 fun demonstrateLocationListSimulation() {
235 println("=== Location List Simulation ===")
236
237 // Simulate different types of locations
238 val simulatedLocations = mapOf(
239 1001 to LocationInfo(1001, 1, "Shopping Mall"),
240 1002 to LocationInfo(1002, 2, "Office Building"),
241 1003 to LocationInfo(1003, 1, "Airport Terminal"),
242 1004 to LocationInfo(1004, 3, "University Campus")
243 )
244
245 // Simulate location list loading
246 GlobalScope.launch {
247 delay(1000)
248 println("Simulated location list loaded")
249 demonstrateLocationList(simulatedLocations)
250 }
251
252 // Simulate location list update
253 GlobalScope.launch {
254 delay(3000)
255 val updatedLocations = mapOf(
256 1001 to LocationInfo(1001, 2, "Shopping Mall - Updated"),
257 1002 to LocationInfo(1002, 3, "Office Building - Updated"),
258 1003 to LocationInfo(1003, 2, "Airport Terminal - Updated"),
259 1004 to LocationInfo(1004, 4, "University Campus - Updated"),
260 1005 to LocationInfo(1005, 1, "New Hospital")
261 )
262 println("Simulated location list updated")
263 demonstrateLocationList(updatedLocations)
264 }
265 }
266
267 /**
268 * Demonstrate location list listener management
269 */
270 fun demonstrateLocationListListenerManagement() {
271 println("=== Location List Listener Management ===")
272
273 val manager = locationListManager ?: return
274
275 // Create a test listener
276 val testListener = object : LocationListListener() {
277 override fun onLocationListLoaded(locationInfos: Map<Int, LocationInfo>) {
278 println("Test listener received location list: ${locationInfos.size} locations")
279 }
280
281 override fun onLocationListFailed(error: java.lang.Error) {
282 println("Test listener received error: ${error.message}")
283 }
284 }
285
286 // Add listener
287 manager.addLocationListListener(testListener)
288 println("Added test location list listener")
289
290 // Force update to trigger listener
291 manager.updateLocationList()
292
293 // Simulate some time passing
294 GlobalScope.launch {
295 delay(2000)
296
297 // Remove listener
298 manager.removeLocationListListener(testListener)
299 println("Removed test location list listener")
300 }
301 }
302
303 /**
304 * Main demonstration method
305 */
306 suspend fun runExample() {
307 println("=== LocationListManager Example ===")
308
309 demonstrateLocationListManagerMethods()
310 demonstrateLocationListListenerManagement()
311 demonstrateAdvancedLocationListFeatures()
312 demonstrateLocationListSimulation()
313
314 // Wait a bit for location list operations
315 delay(5000)
316
317 println("=== Example completed ===")
318 }
319}
320
321/**
322 * Function to run the example
323 */
324fun main() = runBlocking {
325 val example = LocationListManagerExample()
326 example.runExample()
327}