4 * LocationListManager usage example for Swift
5 * Demonstrates working with location list management, downloading locations from server, and location information
7class LocationListManagerExample: NSObject {
8 private var sdk: NavigineSdk?
9 private var locationListManager: LocationListManager?
10 private var locationListListener: LocationListListener?
15 setupLocationListListener()
19 * Initialize SDK and get managers
21 private func initializeSdk() {
23 // [swift_NavigineSdk_getInstance]
25 sdk = NavigineSdk.getInstance()
26 // [swift_NavigineSdk_getInstance]
28 // [swift_NavigineSdk_setUserHash]
30 sdk?.setUserHash("USER-HASH-HERE")
31 // [swift_NavigineSdk_setUserHash]
33 // [swift_NavigineSdk_setServer]
34 // Set server URL (optional)
35 sdk?.setServer("https://custom.navigine.com")
36 // [swift_NavigineSdk_setServer]
38 // [swift_NavigineSdk_getLocationListManager]
39 // Get LocationListManager for working with location lists
40 locationListManager = sdk?.getLocationListManager()
41 // [swift_NavigineSdk_getLocationListManager]
43 if let locationListManager = locationListManager {
44 print("LocationListManager successfully initialized")
47 print("Error initializing SDK: \(error)")
52 * Setup location list listener
54 private func setupLocationListListener() {
55 locationListListener = LocationListListenerImpl(example: self)
59 * Demonstrate LocationListManager methods
61 func demonstrateLocationListManagerMethods() {
62 guard let manager = locationListManager, let listener = locationListListener else {
63 print("LocationListManager not initialized")
67 // [swift_LocationListManager_addLocationListListener]
68 // Add location list listener
69 manager.addLocationListListener(listener)
70 print("Added location list listener")
71 // [swift_LocationListManager_addLocationListListener]
73 // [swift_LocationListManager_updateLocationList]
74 // Force reload location list
75 manager.updateLocationList()
76 print("Requested location list update")
77 // [swift_LocationListManager_updateLocationList]
79 // [swift_LocationListManager_getLocationList]
80 // Get current location list
81 let currentLocationList = manager.getLocationList()
82 print("Current location list contains \(currentLocationList.count) locations")
83 demonstrateLocationList(currentLocationList)
84 // [swift_LocationListManager_getLocationList]
86 // Simulate some time passing for location list updates
87 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
88 // [swift_LocationListManager_removeLocationListListener]
89 // Remove location list listener
90 manager.removeLocationListListener(listener)
91 print("Removed location list listener")
92 // [swift_LocationListManager_removeLocationListListener]
97 * Demonstrate location list
99 func demonstrateLocationList(_ locationInfos: [Int: LocationInfo]) {
100 if locationInfos.isEmpty {
101 print("Location list is empty")
105 print("=== Location List ===")
106 locationInfos.forEach { (locationId, locationInfo) in
107 demonstrateLocationInfo(locationInfo)
109 print("====================")
113 * Demonstrate LocationInfo usage
115 func demonstrateLocationInfo(_ locationInfo: LocationInfo) {
116 // [swift_LocationInfo_getId]
118 let id = locationInfo.id
119 print("Location ID: \(id)")
120 // [swift_LocationInfo_getId]
122 // [swift_LocationInfo_getVersion]
123 // Get location version
124 let version = locationInfo.version
125 print("Location version: \(version)")
126 // [swift_LocationInfo_getVersion]
128 // [swift_LocationInfo_getName]
130 let name = locationInfo.name
131 print("Location name: \(name)")
132 // [swift_LocationInfo_getName]
138 * Demonstrate error handling
140 func demonstrateErrorHandling(_ error: Error) {
141 print("Handling location list error:")
142 print(" Error message: \(error.localizedDescription)")
143 print(" Error type: \(type(of: error))")
147 * Demonstrate advanced location list management features
149 func demonstrateAdvancedLocationListFeatures() {
150 print("=== Advanced Location List Features ===")
152 guard let manager = locationListManager else { return }
154 // Add multiple listeners
155 let listener1 = LocationListListenerImpl(example: self)
156 let listener2 = LocationListListenerImpl(example: self)
158 // [swift_LocationListManager_addLocationListListener_1]
159 // Add first location list listener
160 manager.addLocationListListener(listener1)
161 print("Added first location list listener")
162 // [swift_LocationListManager_addLocationListListener_1]
164 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
165 // [swift_LocationListManager_addLocationListListener_2]
166 // Add second location list listener
167 manager.addLocationListListener(listener2)
168 print("Added second location list listener")
169 // [swift_LocationListManager_addLocationListListener_2]
171 // Force update to trigger listeners
172 manager.updateLocationList()
174 DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
176 // [swift_LocationListManager_removeLocationListListener_1]
177 // Remove first location list listener
178 manager.removeLocationListListener(listener1)
179 print("Removed first location list listener")
180 // [swift_LocationListManager_removeLocationListListener_1]
182 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
183 // [swift_LocationListManager_removeLocationListListener_2]
184 // Remove second location list listener
185 manager.removeLocationListListener(listener2)
186 print("Removed second location list listener")
187 // [swift_LocationListManager_removeLocationListListener_2]
194 * Demonstrate location list simulation
196 func demonstrateLocationListSimulation() {
197 print("=== Location List Simulation ===")
199 // Simulate different types of locations
200 let simulatedLocations: [Int: LocationInfo] = [
201 1001: LocationInfo(id: 1001, version: 1, name: "Shopping Mall"),
202 1002: LocationInfo(id: 1002, version: 2, name: "Office Building"),
203 1003: LocationInfo(id: 1003, version: 1, name: "Airport Terminal"),
204 1004: LocationInfo(id: 1004, version: 3, name: "University Campus")
207 // Simulate location list loading
208 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
209 print("Simulated location list loaded")
210 self.demonstrateLocationList(simulatedLocations)
213 // Simulate location list update
214 DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
215 let updatedLocations: [Int: LocationInfo] = [
216 1001: LocationInfo(id: 1001, version: 2, name: "Shopping Mall - Updated"),
217 1002: LocationInfo(id: 1002, version: 3, name: "Office Building - Updated"),
218 1003: LocationInfo(id: 1003, version: 2, name: "Airport Terminal - Updated"),
219 1004: LocationInfo(id: 1004, version: 4, name: "University Campus - Updated"),
220 1005: LocationInfo(id: 1005, version: 1, name: "New Hospital")
222 print("Simulated location list updated")
223 self.demonstrateLocationList(updatedLocations)
228 * Demonstrate location list listener management
230 func demonstrateLocationListListenerManagement() {
231 print("=== Location List Listener Management ===")
233 guard let manager = locationListManager else { return }
235 // Create a test listener
236 let testListener = LocationListListenerImpl(example: self)
239 manager.addLocationListListener(testListener)
240 print("Added test location list listener")
242 // Force update to trigger listener
243 manager.updateLocationList()
245 // Simulate some time passing
246 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
248 manager.removeLocationListListener(testListener)
249 print("Removed test location list listener")
254 * Main demonstration method
257 print("=== LocationListManager Example ===")
259 demonstrateLocationListManagerMethods()
260 demonstrateLocationListListenerManagement()
261 demonstrateAdvancedLocationListFeatures()
262 demonstrateLocationListSimulation()
264 // Wait a bit for location list operations
265 DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
266 print("=== Example completed ===")
272 * LocationListListener implementation
274class LocationListListenerImpl: NSObject, LocationListListener {
275 private weak var example: LocationListManagerExample?
277 init(example: LocationListManagerExample) {
278 self.example = example
282 // [swift_LocationListListener_onLocationListLoaded]
283 func onLocationListLoaded(_ locationInfos: [Int: LocationInfo]) {
284 print("Location list loaded")
285 example?.demonstrateLocationList(locationInfos)
287 // [swift_LocationListListener_onLocationListLoaded]
289 // [swift_LocationListListener_onLocationListFailed]
290 func onLocationListFailed(_ error: Error) {
291 print("Location list failed")
292 example?.demonstrateErrorHandling(error)
294 // [swift_LocationListListener_onLocationListFailed]
298 * Function to run the example
301 let example = LocationListManagerExample()
304 // Keep the app running for a while to see the output
305 RunLoop.main.run(until: Date().addingTimeInterval(10.0))