Loading...
Searching...
No Matches
location_list_manager_example.dart
Go to the documentation of this file.
1import 'dart:async';
2import 'dart:io';
3import 'package:navigine_sdk/navigine_sdk.dart';
4
9class LocationListManagerExample {
10 NavigineSdk? _sdk;
11 LocationListManager? _locationListManager;
12 LocationListListener? _locationListListener;
13
14 LocationListManagerExample() {
15 _initializeSdk();
16 _setupLocationListListener();
17 }
18
22 void _initializeSdk() {
23 try {
24 // [dart_NavigineSdk_getInstance]
25 // Get SDK instance
26 _sdk = NavigineSdk.getInstance();
27 // [dart_NavigineSdk_getInstance]
28
29 // [dart_NavigineSdk_setUserHash]
30 // Set user hash
31 _sdk?.setUserHash("USER-HASH-HERE");
32 // [dart_NavigineSdk_setUserHash]
33
34 // [dart_NavigineSdk_setServer]
35 // Set server URL (optional)
36 _sdk?.setServer("https://custom.navigine.com");
37 // [dart_NavigineSdk_setServer]
38
39 // [dart_NavigineSdk_getLocationListManager]
40 // Get LocationListManager for working with location lists
41 _locationListManager = _sdk?.getLocationListManager();
42 // [dart_NavigineSdk_getLocationListManager]
43
44 if (_locationListManager != null) {
45 print("LocationListManager successfully initialized");
46 }
47 } catch (e) {
48 print("Error initializing SDK: $e");
49 }
50 }
51
55 void _setupLocationListListener() {
56 _locationListListener = LocationListListener(
57 // [dart_LocationListListener_onLocationListLoaded]
58 onLocationListLoaded: (Map<int, LocationInfo> locationInfos) {
59 print("Location list loaded");
60 _demonstrateLocationList(locationInfos);
61 },
62 // [dart_LocationListListener_onLocationListLoaded]
63
64 // [dart_LocationListListener_onLocationListFailed]
65 onLocationListFailed: (Error error) {
66 print("Location list failed: ${error.message}");
67 _demonstrateErrorHandling(error);
68 },
69 // [dart_LocationListListener_onLocationListFailed]
70 );
71 }
72
76 void demonstrateLocationListManagerMethods() {
77 if (_locationListManager == null) {
78 print("LocationListManager not initialized");
79 return;
80 }
81
82 // [dart_LocationListManager_addLocationListListener]
83 // Add location list listener
84 _locationListManager!.addLocationListListener(_locationListListener!);
85 print("Added location list listener");
86 // [dart_LocationListManager_addLocationListListener]
87
88 // [dart_LocationListManager_updateLocationList]
89 // Force reload location list
90 _locationListManager!.updateLocationList();
91 print("Requested location list update");
92 // [dart_LocationListManager_updateLocationList]
93
94 // [dart_LocationListManager_getLocationList]
95 // Get current location list
96 Map<int, LocationInfo> currentLocationList = _locationListManager!.getLocationList();
97 print("Current location list contains ${currentLocationList.length} locations");
98 _demonstrateLocationList(currentLocationList);
99 // [dart_LocationListManager_getLocationList]
100
101 // Simulate some time passing for location list updates
102 Future.delayed(Duration(seconds: 2), () {
103 // [dart_LocationListManager_removeLocationListListener]
104 // Remove location list listener
105 _locationListManager!.removeLocationListListener(_locationListListener!);
106 print("Removed location list listener");
107 // [dart_LocationListManager_removeLocationListListener]
108 });
109 }
110
114 void _demonstrateLocationList(Map<int, LocationInfo> locationInfos) {
115 if (locationInfos.isEmpty) {
116 print("Location list is empty");
117 return;
118 }
119
120 print("=== Location List ===");
121 locationInfos.forEach((locationId, locationInfo) {
122 _demonstrateLocationInfo(locationInfo);
123 });
124 print("====================");
125 }
126
130 void _demonstrateLocationInfo(LocationInfo locationInfo) {
131 // [dart_LocationInfo_getId]
132 // Get location ID
133 int id = locationInfo.id;
134 print("Location ID: $id");
135 // [dart_LocationInfo_getId]
136
137 // [dart_LocationInfo_getVersion]
138 // Get location version
139 int version = locationInfo.version;
140 print("Location version: $version");
141 // [dart_LocationInfo_getVersion]
142
143 // [dart_LocationInfo_getName]
144 // Get location name
145 String name = locationInfo.name;
146 print("Location name: $name");
147 // [dart_LocationInfo_getName]
148
149 print("---");
150 }
151
155 void _demonstrateErrorHandling(Error error) {
156 print("Handling location list error:");
157 print(" Error message: ${error.message}");
158 print(" Error type: ${error.runtimeType}");
159 }
160
164 Future<void> demonstrateAdvancedLocationListFeatures() async {
165 print("=== Advanced Location List Features ===");
166
167 if (_locationListManager == null) {
168 return;
169 }
170
171 // Add multiple listeners
172 LocationListListener listener1 = LocationListListener(
173 onLocationListLoaded: (Map<int, LocationInfo> locationInfos) {
174 print("Listener 1: Location list loaded - ${locationInfos.length} locations");
175 },
176 onLocationListFailed: (Error error) {
177 print("Listener 1: Location list failed - ${error.message}");
178 },
179 );
180
181 LocationListListener listener2 = LocationListListener(
182 onLocationListLoaded: (Map<int, LocationInfo> locationInfos) {
183 print("Listener 2: Location list loaded - ${locationInfos.length} locations");
184 },
185 onLocationListFailed: (Error error) {
186 print("Listener 2: Location list failed - ${error.message}");
187 },
188 );
189
190 // [dart_LocationListManager_addLocationListListener_1]
191 // Add first location list listener
192 _locationListManager!.addLocationListListener(listener1);
193 print("Added first location list listener");
194 // [dart_LocationListManager_addLocationListListener_1]
195
196 await Future.delayed(Duration(seconds: 1));
197
198 // [dart_LocationListManager_addLocationListListener_2]
199 // Add second location list listener
200 _locationListManager!.addLocationListListener(listener2);
201 print("Added second location list listener");
202 // [dart_LocationListManager_addLocationListListener_2]
203
204 // Force update to trigger listeners
205 _locationListManager!.updateLocationList();
206
207 // Wait for location list updates
208 await Future.delayed(Duration(seconds: 3));
209
210 // Remove listeners
211 // [dart_LocationListManager_removeLocationListListener_1]
212 // Remove first location list listener
213 _locationListManager!.removeLocationListListener(listener1);
214 print("Removed first location list listener");
215 // [dart_LocationListManager_removeLocationListListener_1]
216
217 await Future.delayed(Duration(seconds: 1));
218
219 // [dart_LocationListManager_removeLocationListListener_2]
220 // Remove second location list listener
221 _locationListManager!.removeLocationListListener(listener2);
222 print("Removed second location list listener");
223 // [dart_LocationListManager_removeLocationListListener_2]
224 }
225
229 void demonstrateLocationListSimulation() {
230 print("=== Location List Simulation ===");
231
232 // Simulate different types of locations
233 Map<int, LocationInfo> simulatedLocations = {
234 1001: LocationInfo(1001, 1, "Shopping Mall"),
235 1002: LocationInfo(1002, 2, "Office Building"),
236 1003: LocationInfo(1003, 1, "Airport Terminal"),
237 1004: LocationInfo(1004, 3, "University Campus"),
238 };
239
240 // Simulate location list loading
241 Future.delayed(Duration(seconds: 1), () {
242 print("Simulated location list loaded");
243 _demonstrateLocationList(simulatedLocations);
244 });
245
246 // Simulate location list update
247 Future.delayed(Duration(seconds: 3), () {
248 Map<int, LocationInfo> updatedLocations = {
249 1001: LocationInfo(1001, 2, "Shopping Mall - Updated"),
250 1002: LocationInfo(1002, 3, "Office Building - Updated"),
251 1003: LocationInfo(1003, 2, "Airport Terminal - Updated"),
252 1004: LocationInfo(1004, 4, "University Campus - Updated"),
253 1005: LocationInfo(1005, 1, "New Hospital"),
254 };
255 print("Simulated location list updated");
256 _demonstrateLocationList(updatedLocations);
257 });
258 }
259
263 void demonstrateLocationListListenerManagement() {
264 print("=== Location List Listener Management ===");
265
266 if (_locationListManager == null) {
267 return;
268 }
269
270 // Create a test listener
271 LocationListListener testListener = LocationListListener(
272 onLocationListLoaded: (Map<int, LocationInfo> locationInfos) {
273 print("Test listener received location list: ${locationInfos.length} locations");
274 },
275 onLocationListFailed: (Error error) {
276 print("Test listener received error: ${error.message}");
277 },
278 );
279
280 // Add listener
281 _locationListManager!.addLocationListListener(testListener);
282 print("Added test location list listener");
283
284 // Force update to trigger listener
285 _locationListManager!.updateLocationList();
286
287 // Simulate some time passing
288 Future.delayed(Duration(seconds: 2), () {
289 // Remove listener
290 _locationListManager!.removeLocationListListener(testListener);
291 print("Removed test location list listener");
292 });
293 }
294
298 Future<void> runExample() async {
299 print("=== LocationListManager Example ===");
300
301 demonstrateLocationListManagerMethods();
302 demonstrateLocationListListenerManagement();
303 await demonstrateAdvancedLocationListFeatures();
304 demonstrateLocationListSimulation();
305
306 // Wait a bit for location list operations
307 await Future.delayed(Duration(seconds: 5));
308
309 print("=== Example completed ===");
310 }
311}
312
316Future<void> main() async {
318 await example.runExample();
319}