Loading...
Searching...
No Matches
LocationListManagerExample.swift
Go to the documentation of this file.
1import Foundation
2
3/**
4 * LocationListManager usage example for Swift
5 * Demonstrates working with location list management, downloading locations from server, and location information
6 */
7class LocationListManagerExample: NSObject {
8 private var sdk: NavigineSdk?
9 private var locationListManager: LocationListManager?
10 private var locationListListener: LocationListListener?
11
12 override init() {
13 super.init()
14 initializeSdk()
15 setupLocationListListener()
16 }
17
18 /**
19 * Initialize SDK and get managers
20 */
21 private func initializeSdk() {
22 do {
23 // [swift_NavigineSdk_getInstance]
24 // Get SDK instance
25 sdk = NavigineSdk.getInstance()
26 // [swift_NavigineSdk_getInstance]
27
28 // [swift_NavigineSdk_setUserHash]
29 // Set user hash
30 sdk?.setUserHash("USER-HASH-HERE")
31 // [swift_NavigineSdk_setUserHash]
32
33 // [swift_NavigineSdk_setServer]
34 // Set server URL (optional)
35 sdk?.setServer("https://custom.navigine.com")
36 // [swift_NavigineSdk_setServer]
37
38 // [swift_NavigineSdk_getLocationListManager]
39 // Get LocationListManager for working with location lists
40 locationListManager = sdk?.getLocationListManager()
41 // [swift_NavigineSdk_getLocationListManager]
42
43 if let locationListManager = locationListManager {
44 print("LocationListManager successfully initialized")
45 }
46 } catch {
47 print("Error initializing SDK: \‍(error)")
48 }
49 }
50
51 /**
52 * Setup location list listener
53 */
54 private func setupLocationListListener() {
55 locationListListener = LocationListListenerImpl(example: self)
56 }
57
58 /**
59 * Demonstrate LocationListManager methods
60 */
61 func demonstrateLocationListManagerMethods() {
62 guard let manager = locationListManager, let listener = locationListListener else {
63 print("LocationListManager not initialized")
64 return
65 }
66
67 // [swift_LocationListManager_addLocationListListener]
68 // Add location list listener
69 manager.addLocationListListener(listener)
70 print("Added location list listener")
71 // [swift_LocationListManager_addLocationListListener]
72
73 // [swift_LocationListManager_updateLocationList]
74 // Force reload location list
75 manager.updateLocationList()
76 print("Requested location list update")
77 // [swift_LocationListManager_updateLocationList]
78
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]
85
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]
93 }
94 }
95
96 /**
97 * Demonstrate location list
98 */
99 func demonstrateLocationList(_ locationInfos: [Int: LocationInfo]) {
100 if locationInfos.isEmpty {
101 print("Location list is empty")
102 return
103 }
104
105 print("=== Location List ===")
106 locationInfos.forEach { (locationId, locationInfo) in
107 demonstrateLocationInfo(locationInfo)
108 }
109 print("====================")
110 }
111
112 /**
113 * Demonstrate LocationInfo usage
114 */
115 func demonstrateLocationInfo(_ locationInfo: LocationInfo) {
116 // [swift_LocationInfo_getId]
117 // Get location ID
118 let id = locationInfo.id
119 print("Location ID: \‍(id)")
120 // [swift_LocationInfo_getId]
121
122 // [swift_LocationInfo_getVersion]
123 // Get location version
124 let version = locationInfo.version
125 print("Location version: \‍(version)")
126 // [swift_LocationInfo_getVersion]
127
128 // [swift_LocationInfo_getName]
129 // Get location name
130 let name = locationInfo.name
131 print("Location name: \‍(name)")
132 // [swift_LocationInfo_getName]
133
134 print("---")
135 }
136
137 /**
138 * Demonstrate error handling
139 */
140 func demonstrateErrorHandling(_ error: Error) {
141 print("Handling location list error:")
142 print(" Error message: \‍(error.localizedDescription)")
143 print(" Error type: \‍(type(of: error))")
144 }
145
146 /**
147 * Demonstrate advanced location list management features
148 */
149 func demonstrateAdvancedLocationListFeatures() {
150 print("=== Advanced Location List Features ===")
151
152 guard let manager = locationListManager else { return }
153
154 // Add multiple listeners
155 let listener1 = LocationListListenerImpl(example: self)
156 let listener2 = LocationListListenerImpl(example: self)
157
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]
163
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]
170
171 // Force update to trigger listeners
172 manager.updateLocationList()
173
174 DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
175 // Remove listeners
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]
181
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]
188 }
189 }
190 }
191 }
192
193 /**
194 * Demonstrate location list simulation
195 */
196 func demonstrateLocationListSimulation() {
197 print("=== Location List Simulation ===")
198
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")
205 ]
206
207 // Simulate location list loading
208 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
209 print("Simulated location list loaded")
210 self.demonstrateLocationList(simulatedLocations)
211 }
212
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")
221 ]
222 print("Simulated location list updated")
223 self.demonstrateLocationList(updatedLocations)
224 }
225 }
226
227 /**
228 * Demonstrate location list listener management
229 */
230 func demonstrateLocationListListenerManagement() {
231 print("=== Location List Listener Management ===")
232
233 guard let manager = locationListManager else { return }
234
235 // Create a test listener
236 let testListener = LocationListListenerImpl(example: self)
237
238 // Add listener
239 manager.addLocationListListener(testListener)
240 print("Added test location list listener")
241
242 // Force update to trigger listener
243 manager.updateLocationList()
244
245 // Simulate some time passing
246 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
247 // Remove listener
248 manager.removeLocationListListener(testListener)
249 print("Removed test location list listener")
250 }
251 }
252
253 /**
254 * Main demonstration method
255 */
256 func runExample() {
257 print("=== LocationListManager Example ===")
258
259 demonstrateLocationListManagerMethods()
260 demonstrateLocationListListenerManagement()
261 demonstrateAdvancedLocationListFeatures()
262 demonstrateLocationListSimulation()
263
264 // Wait a bit for location list operations
265 DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
266 print("=== Example completed ===")
267 }
268 }
269}
270
271/**
272 * LocationListListener implementation
273 */
274class LocationListListenerImpl: NSObject, LocationListListener {
275 private weak var example: LocationListManagerExample?
276
277 init(example: LocationListManagerExample) {
278 self.example = example
279 super.init()
280 }
281
282 // [swift_LocationListListener_onLocationListLoaded]
283 func onLocationListLoaded(_ locationInfos: [Int: LocationInfo]) {
284 print("Location list loaded")
285 example?.demonstrateLocationList(locationInfos)
286 }
287 // [swift_LocationListListener_onLocationListLoaded]
288
289 // [swift_LocationListListener_onLocationListFailed]
290 func onLocationListFailed(_ error: Error) {
291 print("Location list failed")
292 example?.demonstrateErrorHandling(error)
293 }
294 // [swift_LocationListListener_onLocationListFailed]
295}
296
297/**
298 * Function to run the example
299 */
300func main() {
301 let example = LocationListManagerExample()
302 example.runExample()
303
304 // Keep the app running for a while to see the output
305 RunLoop.main.run(until: Date().addingTimeInterval(10.0))
306}
307
308// Run the example
309main()