Loading...
Searching...
No Matches
LocationListManagerExample.m
Go to the documentation of this file.
2
7@implementation LocationListManagerExample
8
9- (instancetype)init {
10 self = [super init];
11 if (self) {
12 [self initializeSdk];
13 [self setupLocationListListener];
14 }
15 return self;
16}
17
21- (void)initializeSdk {
22 @try {
23 // [objc_NavigineSdk_getInstance]
24 // Get SDK instance
25 self.sdk = [NCNavigineSdk getInstance];
26 // [objc_NavigineSdk_getInstance]
27
28 // [objc_NavigineSdk_setUserHash]
29 // Set user hash
30 [self.sdk setUserHash:@"USER-HASH-HERE"];
31 // [objc_NavigineSdk_setUserHash]
32
33 // [objc_NavigineSdk_setServer]
34 // Set server URL (optional)
35 [self.sdk setServer:@"https://custom.navigine.com"];
36 // [objc_NavigineSdk_setServer]
37
38 // [objc_NavigineSdk_getLocationListManager]
39 // Get LocationListManager for working with location lists
40 self.locationListManager = [self.sdk getLocationListManager];
41 // [objc_NavigineSdk_getLocationListManager]
42
43 if (self.locationListManager != nil) {
44 NSLog(@"LocationListManager successfully initialized");
45 }
46 } @catch (NSException *exception) {
47 NSLog(@"Error initializing SDK: %@", exception.reason);
48 }
49}
50
54- (void)setupLocationListListener {
55 self.locationListListener = [[LocationListListenerImpl alloc] initWithExample:self];
56}
57
61- (void)demonstrateLocationListManagerMethods {
62 if (self.locationListManager == nil) {
63 NSLog(@"LocationListManager not initialized");
64 return;
65 }
66
67 // [objc_LocationListManager_addLocationListListener]
68 // Add location list listener
69 [self.locationListManager addLocationListListener:self.locationListListener];
70 NSLog(@"Added location list listener");
71 // [objc_LocationListManager_addLocationListListener]
72
73 // [objc_LocationListManager_updateLocationList]
74 // Force reload location list
75 [self.locationListManager updateLocationList];
76 NSLog(@"Requested location list update");
77 // [objc_LocationListManager_updateLocationList]
78
79 // [objc_LocationListManager_getLocationList]
80 // Get current location list
81 NSDictionary<NSNumber *, NCLocationInfo *> *currentLocationList = [self.locationListManager getLocationList];
82 NSLog(@"Current location list contains %lu locations", (unsigned long)currentLocationList.count);
83 [self demonstrateLocationList:currentLocationList];
84 // [objc_LocationListManager_getLocationList]
85
86 // Simulate some time passing for location list updates
87 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
88 // [objc_LocationListManager_removeLocationListListener]
89 // Remove location list listener
90 [self.locationListManager removeLocationListListener:self.locationListListener];
91 NSLog(@"Removed location list listener");
92 // [objc_LocationListManager_removeLocationListListener]
93 });
94}
95
99- (void)demonstrateLocationList:(NSDictionary<NSNumber *, NCLocationInfo *> *)locationInfos {
100 if (locationInfos.count == 0) {
101 NSLog(@"Location list is empty");
102 return;
103 }
104
105 NSLog(@"=== Location List ===");
106 [locationInfos enumerateKeysAndObjectsUsingBlock:^(NSNumber *locationId, NCLocationInfo *locationInfo, BOOL *stop) {
107 [self demonstrateLocationInfo:locationInfo];
108 }];
109 NSLog(@"====================");
110}
111
115- (void)demonstrateLocationInfo:(NCLocationInfo *)locationInfo {
116 // [objc_LocationInfo_getId]
117 // Get location ID
118 int32_t id = locationInfo.id;
119 NSLog(@"Location ID: %d", id);
120 // [objc_LocationInfo_getId]
121
122 // [objc_LocationInfo_getVersion]
123 // Get location version
124 int32_t version = locationInfo.version;
125 NSLog(@"Location version: %d", version);
126 // [objc_LocationInfo_getVersion]
127
128 // [objc_LocationInfo_getName]
129 // Get location name
130 NSString *name = locationInfo.name;
131 NSLog(@"Location name: %@", name);
132 // [objc_LocationInfo_getName]
133
134 NSLog(@"---");
135}
136
140- (void)demonstrateErrorHandling:(NSError *)error {
141 NSLog(@"Handling location list error:");
142 NSLog(@" Error message: %@", error.localizedDescription);
143 NSLog(@" Error domain: %@", error.domain);
144 NSLog(@" Error code: %ld", (long)error.code);
145}
146
150- (void)demonstrateAdvancedLocationListFeatures {
151 NSLog(@"=== Advanced Location List Features ===");
152
153 if (self.locationListManager == nil) {
154 return;
155 }
156
157 // Add multiple listeners
158 LocationListListenerImpl *listener1 = [[LocationListListenerImpl alloc] initWithExample:self];
159 LocationListListenerImpl *listener2 = [[LocationListListenerImpl alloc] initWithExample:self];
160
161 // [objc_LocationListManager_addLocationListListener_1]
162 // Add first location list listener
163 [self.locationListManager addLocationListListener:listener1];
164 NSLog(@"Added first location list listener");
165 // [objc_LocationListManager_addLocationListListener_1]
166
167 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
168 // [objc_LocationListManager_addLocationListListener_2]
169 // Add second location list listener
170 [self.locationListManager addLocationListListener:listener2];
171 NSLog(@"Added second location list listener");
172 // [objc_LocationListManager_addLocationListListener_2]
173
174 // Force update to trigger listeners
175 [self.locationListManager updateLocationList];
176
177 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
178 // Remove listeners
179 // [objc_LocationListManager_removeLocationListListener_1]
180 // Remove first location list listener
181 [self.locationListManager removeLocationListListener:listener1];
182 NSLog(@"Removed first location list listener");
183 // [objc_LocationListManager_removeLocationListListener_1]
184
185 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
186 // [objc_LocationListManager_removeLocationListListener_2]
187 // Remove second location list listener
188 [self.locationListManager removeLocationListListener:listener2];
189 NSLog(@"Removed second location list listener");
190 // [objc_LocationListManager_removeLocationListListener_2]
191 });
192 });
193 });
194}
195
199- (void)demonstrateLocationListSimulation {
200 NSLog(@"=== Location List Simulation ===");
201
202 // Simulate different types of locations
203 NSDictionary<NSNumber *, NCLocationInfo *> *simulatedLocations = @{
204 @1001: [[NCLocationInfo alloc] initWithId:1001 version:1 name:@"Shopping Mall"],
205 @1002: [[NCLocationInfo alloc] initWithId:1002 version:2 name:@"Office Building"],
206 @1003: [[NCLocationInfo alloc] initWithId:1003 version:1 name:@"Airport Terminal"],
207 @1004: [[NCLocationInfo alloc] initWithId:1004 version:3 name:@"University Campus"],
208 };
209
210 // Simulate location list loading
211 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
212 NSLog(@"Simulated location list loaded");
213 [self demonstrateLocationList:simulatedLocations];
214 });
215
216 // Simulate location list update
217 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
218 NSDictionary<NSNumber *, NCLocationInfo *> *updatedLocations = @{
219 @1001: [[NCLocationInfo alloc] initWithId:1001 version:2 name:@"Shopping Mall - Updated"],
220 @1002: [[NCLocationInfo alloc] initWithId:1002 version:3 name:@"Office Building - Updated"],
221 @1003: [[NCLocationInfo alloc] initWithId:1003 version:2 name:@"Airport Terminal - Updated"],
222 @1004: [[NCLocationInfo alloc] initWithId:1004 version:4 name:@"University Campus - Updated"],
223 @1005: [[NCLocationInfo alloc] initWithId:1005 version:1 name:@"New Hospital"],
224 };
225 NSLog(@"Simulated location list updated");
226 [self demonstrateLocationList:updatedLocations];
227 });
228}
229
233- (void)demonstrateLocationListListenerManagement {
234 NSLog(@"=== Location List Listener Management ===");
235
236 if (self.locationListManager == nil) {
237 return;
238 }
239
240 // Create a test listener
241 LocationListListenerImpl *testListener = [[LocationListListenerImpl alloc] initWithExample:self];
242
243 // Add listener
244 [self.locationListManager addLocationListListener:testListener];
245 NSLog(@"Added test location list listener");
246
247 // Force update to trigger listener
248 [self.locationListManager updateLocationList];
249
250 // Simulate some time passing
251 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
252 // Remove listener
253 [self.locationListManager removeLocationListListener:testListener];
254 NSLog(@"Removed test location list listener");
255 });
256}
257
261- (void)runExample {
262 NSLog(@"=== LocationListManager Example ===");
263
264 [self demonstrateLocationListManagerMethods];
265 [self demonstrateLocationListListenerManagement];
266 [self demonstrateAdvancedLocationListFeatures];
267 [self demonstrateLocationListSimulation];
268
269 // Wait a bit for location list operations
270 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
271 NSLog(@"=== Example completed ===");
272 });
273}
274
275@end
276
280@interface LocationListListenerImpl : NSObject <NCLocationListListener>
281
282@property (nonatomic, weak) LocationListManagerExample *example;
283
284- (instancetype)initWithExample:(LocationListManagerExample *)example;
285
286@end
287
288@implementation LocationListListenerImpl
289
290- (instancetype)initWithExample:(LocationListManagerExample *)example {
291 self = [super init];
292 if (self) {
293 self.example = example;
294 }
295 return self;
296}
297
298// [objc_LocationListListener_onLocationListLoaded]
299- (void)onLocationListLoaded:(NSDictionary<NSNumber *, NCLocationInfo *> *)locationInfos {
300 NSLog(@"Location list loaded");
301 [self.example demonstrateLocationList:locationInfos];
302}
303// [objc_LocationListListener_onLocationListLoaded]
304
305// [objc_LocationListListener_onLocationListFailed]
306- (void)onLocationListFailed:(NSError *)error {
307 NSLog(@"Location list failed");
308 [self.example demonstrateErrorHandling:error];
309}
310// [objc_LocationListListener_onLocationListFailed]
311
312@end
313
317int main(int argc, const char * argv[]) {
318 @autoreleasepool {
320 [example runExample];
321
322 // Keep the app running for a while to see the output
323 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];
324 }
325 return 0;
326}