1import kotlinx.coroutines.delay
2import kotlinx.coroutines.runBlocking
3import kotlinx.coroutines.GlobalScope
4import kotlinx.coroutines.launch
7 * LocationListManager usage example for Kotlin
8 * Demonstrates working with location list management, downloading locations from server, and location information
10class LocationListManagerExample {
11 private var sdk: NavigineSdk? = null
12 private var locationListManager: LocationListManager? = null
13 private var locationListListener: LocationListListener? = null
17 setupLocationListListener()
21 * Initialize SDK and get managers
23 private fun initializeSdk() {
25 // [kotlin_NavigineSdk_getInstance]
27 sdk = NavigineSdk.getInstance()
28 // [kotlin_NavigineSdk_getInstance]
30 // [kotlin_NavigineSdk_setUserHash]
32 sdk?.setUserHash("USER-HASH-HERE")
33 // [kotlin_NavigineSdk_setUserHash]
35 // [kotlin_NavigineSdk_setServer]
36 // Set server URL (optional)
37 sdk?.setServer("https://custom.navigine.com")
38 // [kotlin_NavigineSdk_setServer]
40 // [kotlin_NavigineSdk_getLocationListManager]
41 // Get LocationListManager for working with location lists
42 locationListManager = sdk?.getLocationListManager()
43 // [kotlin_NavigineSdk_getLocationListManager]
45 if (locationListManager != null) {
46 println("LocationListManager successfully initialized")
48 } catch (e: Exception) {
49 System.err.println("Error initializing SDK: ${e.message}")
54 * Setup location list listener
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)
63 // [kotlin_LocationListListener_onLocationListLoaded]
65 // [kotlin_LocationListListener_onLocationListFailed]
66 override fun onLocationListFailed(error: java.lang.Error) {
67 println("Location list failed: ${error.message}")
68 demonstrateErrorHandling(error)
70 // [kotlin_LocationListListener_onLocationListFailed]
75 * Demonstrate LocationListManager methods
77 fun demonstrateLocationListManagerMethods() {
78 val manager = locationListManager ?: run {
79 System.err.println("LocationListManager not initialized")
83 val listener = locationListListener ?: return
85 // [kotlin_LocationListManager_addLocationListListener]
86 // Add location list listener
87 manager.addLocationListListener(listener)
88 println("Added location list listener")
89 // [kotlin_LocationListManager_addLocationListListener]
91 // [kotlin_LocationListManager_updateLocationList]
92 // Force reload location list
93 manager.updateLocationList()
94 println("Requested location list update")
95 // [kotlin_LocationListManager_updateLocationList]
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]
104 // Simulate some time passing for location list updates
108 // [kotlin_LocationListManager_removeLocationListListener]
109 // Remove location list listener
110 manager.removeLocationListListener(listener)
111 println("Removed location list listener")
112 // [kotlin_LocationListManager_removeLocationListListener]
117 * Demonstrate location list
119 fun demonstrateLocationList(locationInfos: Map<Int, LocationInfo>) {
120 if (locationInfos.isEmpty()) {
121 println("Location list is empty")
125 println("=== Location List ===")
126 locationInfos.forEach { (locationId, locationInfo) ->
127 demonstrateLocationInfo(locationInfo)
129 println("====================")
133 * Demonstrate LocationInfo usage
135 fun demonstrateLocationInfo(locationInfo: LocationInfo) {
136 // [kotlin_LocationInfo_getId]
138 val id = locationInfo.id
139 println("Location ID: $id")
140 // [kotlin_LocationInfo_getId]
142 // [kotlin_LocationInfo_getVersion]
143 // Get location version
144 val version = locationInfo.version
145 println("Location version: $version")
146 // [kotlin_LocationInfo_getVersion]
148 // [kotlin_LocationInfo_getName]
150 val name = locationInfo.name
151 println("Location name: $name")
152 // [kotlin_LocationInfo_getName]
158 * Demonstrate error handling
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}")
167 * Demonstrate advanced location list management features
169 suspend fun demonstrateAdvancedLocationListFeatures() {
170 println("=== Advanced Location List Features ===")
172 val manager = locationListManager ?: return
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")
180 override fun onLocationListFailed(error: java.lang.Error) {
181 println("Listener 1: Location list failed - ${error.message}")
185 val listener2 = object : LocationListListener() {
186 override fun onLocationListLoaded(locationInfos: Map<Int, LocationInfo>) {
187 println("Listener 2: Location list loaded - ${locationInfos.size} locations")
190 override fun onLocationListFailed(error: java.lang.Error) {
191 println("Listener 2: Location list failed - ${error.message}")
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]
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]
209 // Force update to trigger listeners
210 manager.updateLocationList()
212 // Wait for location list updates
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]
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]
232 * Demonstrate location list simulation
234 fun demonstrateLocationListSimulation() {
235 println("=== Location List Simulation ===")
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")
245 // Simulate location list loading
248 println("Simulated location list loaded")
249 demonstrateLocationList(simulatedLocations)
252 // Simulate location list update
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")
262 println("Simulated location list updated")
263 demonstrateLocationList(updatedLocations)
268 * Demonstrate location list listener management
270 fun demonstrateLocationListListenerManagement() {
271 println("=== Location List Listener Management ===")
273 val manager = locationListManager ?: return
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")
281 override fun onLocationListFailed(error: java.lang.Error) {
282 println("Test listener received error: ${error.message}")
287 manager.addLocationListListener(testListener)
288 println("Added test location list listener")
290 // Force update to trigger listener
291 manager.updateLocationList()
293 // Simulate some time passing
298 manager.removeLocationListListener(testListener)
299 println("Removed test location list listener")
304 * Main demonstration method
306 suspend fun runExample() {
307 println("=== LocationListManager Example ===")
309 demonstrateLocationListManagerMethods()
310 demonstrateLocationListListenerManagement()
311 demonstrateAdvancedLocationListFeatures()
312 demonstrateLocationListSimulation()
314 // Wait a bit for location list operations
317 println("=== Example completed ===")
322 * Function to run the example
324fun main() = runBlocking {
325 val example = LocationListManagerExample()