Loading...
Searching...
No Matches
storage_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 StorageManagerExample {
10 NavigineSdk? _sdk;
11 StorageManager? _storageManager;
12 KeyValueStorage? _userStorage;
13 KeyValueStorage? _appStorage;
14 KeyValueStorage? _cacheStorage;
15
16 StorageManagerExample() {
17 _initializeSdk();
18 _setupStorages();
19 }
20
24 void _initializeSdk() {
25 try {
26 // [dart_NavigineSdk_getInstance]
27 // Get SDK instance
28 _sdk = NavigineSdk.getInstance();
29 // [dart_NavigineSdk_getInstance]
30
31 // [dart_NavigineSdk_setUserHash]
32 // Set user hash
33 _sdk?.setUserHash("USER-HASH-HERE");
34 // [dart_NavigineSdk_setUserHash]
35
36 // [dart_NavigineSdk_setServer]
37 // Set server URL (optional)
38 _sdk?.setServer("https://custom.navigine.com");
39 // [dart_NavigineSdk_setServer]
40
41 // [dart_NavigineSdk_getStorageManager]
42 // Get StorageManager for working with storages
43 _storageManager = _sdk?.getStorageManager();
44 // [dart_NavigineSdk_getStorageManager]
45
46 if (_storageManager != null) {
47 print("StorageManager successfully initialized");
48 }
49 } catch (e) {
50 print("Error initializing SDK: $e");
51 }
52 }
53
57 void _setupStorages() {
58 if (_storageManager == null) {
59 print("StorageManager not initialized");
60 return;
61 }
62
63 // [dart_StorageManager_getStorage]
64 // Get or create different storage instances
65 _userStorage = _storageManager!.getStorage("user_preferences");
66 _appStorage = _storageManager!.getStorage("app_settings");
67 _cacheStorage = _storageManager!.getStorage("cache");
68 // [dart_StorageManager_getStorage]
69
70 print("Storage instances created");
71 }
72
76 void demonstrateStorageManagerMethods() {
77 if (_storageManager == null) {
78 print("StorageManager not initialized");
79 return;
80 }
81
82 // [dart_StorageManager_getStorageList]
83 // Get list of all existing storages
84 List<String> storageList = _storageManager!.getStorageList();
85 print("Existing storages: $storageList");
86 // [dart_StorageManager_getStorageList]
87
88 // [dart_StorageManager_getStorage1]
89 // Get or create storage by name
90 KeyValueStorage testStorage = _storageManager!.getStorage("test_storage");
91 print("Created test storage");
92 // [dart_StorageManager_getStorage1]
93
94 // Demonstrate storage creation and listing
95 _storageManager!.getStorage("another_storage");
96 List<String> updatedList = _storageManager!.getStorageList();
97 print("Updated storage list: $updatedList");
98
99 // [dart_StorageManager_removeStorage]
100 // Remove storage and all its data
101 _storageManager!.removeStorage("test_storage");
102 print("Removed test storage");
103 // [dart_StorageManager_removeStorage]
104
105 List<String> finalList = _storageManager!.getStorageList();
106 print("Final storage list: $finalList");
107 }
108
112 void demonstrateKeyValueStorageMethods() {
113 if (_userStorage == null) {
114 print("User storage not initialized");
115 return;
116 }
117
118 print("\n=== KeyValueStorage Methods Demonstration ===");
119
120 // [dart_KeyValueStorage_putString]
121 // Store string values
122 _userStorage!.putString("user_name", "John Doe");
123 _userStorage!.putString("user_email", "john.doe@example.com");
124 print("Stored string values");
125 // [dart_KeyValueStorage_putString]
126
127 // [dart_KeyValueStorage_putInt]
128 // Store integer values
129 _userStorage!.putInt("user_age", 25);
130 _userStorage!.putInt("login_count", 42);
131 print("Stored integer values");
132 // [dart_KeyValueStorage_putInt]
133
134 // [dart_KeyValueStorage_putLong]
135 // Store long values
136 _userStorage!.putLong("registration_timestamp", 1640995200000);
137 _userStorage!.putLong(
138 "last_login_timestamp",
139 DateTime.now().millisecondsSinceEpoch,
140 );
141 print("Stored long values");
142 // [dart_KeyValueStorage_putLong]
143
144 // [dart_KeyValueStorage_putBool]
145 // Store boolean values
146 _userStorage!.putBool("is_premium", true);
147 _userStorage!.putBool("notifications_enabled", false);
148 print("Stored boolean values");
149 // [dart_KeyValueStorage_putBool]
150
151 // [dart_KeyValueStorage_putFloat]
152 // Store float values
153 _userStorage!.putFloat("user_rating", 4.5);
154 _userStorage!.putFloat("temperature", 23.5);
155 print("Stored float values");
156 // [dart_KeyValueStorage_putFloat]
157
158 // [dart_KeyValueStorage_putDouble]
159 // Store double values
160 _userStorage!.putDouble("user_location_lat", 55.7558);
161 _userStorage!.putDouble("user_location_lng", 37.6176);
162 print("Stored double values");
163 // [dart_KeyValueStorage_putDouble]
164
165 // [dart_KeyValueStorage_contains]
166 // Check if keys exist
167 bool hasUserName = _userStorage!.contains("user_name");
168 bool hasNonExistent = _userStorage!.contains("non_existent_key");
169 print("Contains 'user_name': $hasUserName");
170 print("Contains 'non_existent_key': $hasNonExistent");
171 // [dart_KeyValueStorage_contains]
172
173 // [dart_KeyValueStorage_getKeys]
174 // Get all stored keys
175 List<String> allKeys = _userStorage!.getKeys();
176 print("All stored keys: $allKeys");
177 // [dart_KeyValueStorage_getKeys]
178
179 // [dart_KeyValueStorage_getString]
180 // Retrieve string values with defaults
181 String userName = _userStorage!.getString("user_name", "Unknown");
182 String userEmail = _userStorage!.getString("user_email", "");
183 String nonExistent = _userStorage!.getString(
184 "non_existent_key",
185 "default_value",
186 );
187 print("User name: $userName");
188 print("User email: $userEmail");
189 print("Non-existent key: $nonExistent");
190 // [dart_KeyValueStorage_getString]
191
192 // [dart_KeyValueStorage_getInt]
193 // Retrieve integer values with defaults
194 int userAge = _userStorage!.getInt("user_age", 0);
195 int loginCount = _userStorage!.getInt("login_count", 0);
196 int nonExistentInt = _userStorage!.getInt("non_existent_int", -1);
197 print("User age: $userAge");
198 print("Login count: $loginCount");
199 print("Non-existent int: $nonExistentInt");
200 // [dart_KeyValueStorage_getInt]
201
202 // [dart_KeyValueStorage_getLong]
203 // Retrieve long values with defaults
204 long regTimestamp = _userStorage!.getLong("registration_timestamp", 0);
205 long lastLogin = _userStorage!.getLong("last_login_timestamp", 0);
206 long nonExistentLong = _userStorage!.getLong("non_existent_long", -1);
207 print("Registration timestamp: $regTimestamp");
208 print("Last login timestamp: $lastLogin");
209 print("Non-existent long: $nonExistentLong");
210 // [dart_KeyValueStorage_getLong]
211
212 // [dart_KeyValueStorage_getBool]
213 // Retrieve boolean values with defaults
214 bool isPremium = _userStorage!.getBool("is_premium", false);
215 bool notificationsEnabled = _userStorage!.getBool(
216 "notifications_enabled",
217 true,
218 );
219 bool nonExistentBool = _userStorage!.getBool("non_existent_bool", false);
220 print("Is premium: $isPremium");
221 print("Notifications enabled: $notificationsEnabled");
222 print("Non-existent bool: $nonExistentBool");
223 // [dart_KeyValueStorage_getBool]
224
225 // [dart_KeyValueStorage_getFloat]
226 // Retrieve float values with defaults
227 float userRating = _userStorage!.getFloat("user_rating", 0.0);
228 float temperature = _userStorage!.getFloat("temperature", 0.0);
229 float nonExistentFloat = _userStorage!.getFloat("non_existent_float", -1.0);
230 print("User rating: $userRating");
231 print("Temperature: $temperature");
232 print("Non-existent float: $nonExistentFloat");
233 // [dart_KeyValueStorage_getFloat]
234
235 // [dart_KeyValueStorage_getDouble]
236 // Retrieve double values with defaults
237 double userLat = _userStorage!.getDouble("user_location_lat", 0.0);
238 double userLng = _userStorage!.getDouble("user_location_lng", 0.0);
239 double nonExistentDouble = _userStorage!.getDouble(
240 "non_existent_double",
241 -1.0,
242 );
243 print("User location lat: $userLat");
244 print("User location lng: $userLng");
245 print("Non-existent double: $nonExistentDouble");
246 // [dart_KeyValueStorage_getDouble]
247
248 // [dart_KeyValueStorage_remove]
249 // Remove specific keys
250 _userStorage!.remove("user_age");
251 _userStorage!.remove("non_existent_key"); // No-op
252 print("Removed 'user_age' key");
253 // [dart_KeyValueStorage_remove]
254
255 // Verify removal
256 bool stillHasAge = _userStorage!.contains("user_age");
257 print("Still contains 'user_age': $stillHasAge");
258
259 // [dart_KeyValueStorage_clear]
260 // Clear all data
261 _userStorage!.clear();
262 print("Cleared all data from user storage");
263 // [dart_KeyValueStorage_clear]
264
265 // Verify clear
266 List<String> keysAfterClear = _userStorage!.getKeys();
267 print("Keys after clear: $keysAfterClear");
268 }
269
273 void demonstrateMultipleStorages() {
274 if (_userStorage == null || _appStorage == null || _cacheStorage == null) {
275 print("Storages not initialized");
276 return;
277 }
278
279 print("\n=== Multiple Storages Demonstration ===");
280
281 // Store different data in different storages
282 _userStorage!.putString("name", "John Doe");
283 _userStorage!.putInt("age", 25);
284 _userStorage!.putBool("is_premium", true);
285
286 _appStorage!.putString("theme", "dark");
287 _appStorage!.putInt("api_version", 2);
288 _appStorage!.putBool("debug_mode", false);
289
290 _cacheStorage!.putLong(
291 "last_update",
292 DateTime.now().millisecondsSinceEpoch,
293 );
294 _cacheStorage!.putString("api_token", "abc123");
295 _cacheStorage!.putDouble("cached_rating", 4.2);
296
297 // Retrieve data from different storages
298 print("User storage data:");
299 _demonstrateStorageData(_userStorage!);
300
301 print("\nApp storage data:");
302 _demonstrateStorageData(_appStorage!);
303
304 print("\nCache storage data:");
305 _demonstrateStorageData(_cacheStorage!);
306 }
307
311 void _demonstrateStorageData(KeyValueStorage storage) {
312 List<String> keys = storage.getKeys();
313 for (String key in keys) {
314 if (storage.contains(key)) {
315 // Try to get value as string first
316 String stringValue = storage.getString(key, "");
317 if (stringValue.isNotEmpty) {
318 print(" $key: $stringValue (string)");
319 continue;
320 }
321
322 // Try integer
323 int intValue = storage.getInt(key, -999999);
324 if (intValue != -999999) {
325 print(" $key: $intValue (int)");
326 continue;
327 }
328
329 // Try long
330 long longValue = storage.getLong(key, -999999);
331 if (longValue != -999999) {
332 print(" $key: $longValue (long)");
333 continue;
334 }
335
336 // Try boolean
337 bool boolValue = storage.getBool(key, false);
338 if (storage.contains(key)) {
339 print(" $key: $boolValue (bool)");
340 continue;
341 }
342
343 // Try float
344 float floatValue = storage.getFloat(key, -999999.0);
345 if (floatValue != -999999.0) {
346 print(" $key: $floatValue (float)");
347 continue;
348 }
349
350 // Try double
351 double doubleValue = storage.getDouble(key, -999999.0);
352 if (doubleValue != -999999.0) {
353 print(" $key: $doubleValue (double)");
354 }
355 }
356 }
357 }
358
362 void demonstrateStorageAnalytics() {
363 if (_storageManager == null) {
364 print("StorageManager not initialized");
365 return;
366 }
367
368 print("\n=== Storage Analytics ===");
369
370 // Analyze all storages
371 List<String> allStorages = _storageManager!.getStorageList();
372 print("Total storages: ${allStorages.length}");
373
374 for (String storageName in allStorages) {
375 KeyValueStorage storage = _storageManager!.getStorage(storageName);
376 List<String> keys = storage.getKeys();
377 print("Storage '$storageName' contains ${keys.length} items");
378
379 // Analyze data types
380 int stringCount = 0;
381 int intCount = 0;
382 int longCount = 0;
383 int boolCount = 0;
384 int floatCount = 0;
385 int doubleCount = 0;
386
387 for (String key in keys) {
388 // Count by trying to get each type
389 String stringValue = storage.getString(key, "");
390 if (stringValue.isNotEmpty) {
391 stringCount++;
392 continue;
393 }
394
395 int intValue = storage.getInt(key, -999999);
396 if (intValue != -999999) {
397 intCount++;
398 continue;
399 }
400
401 long longValue = storage.getLong(key, -999999);
402 if (longValue != -999999) {
403 longCount++;
404 continue;
405 }
406
407 bool boolValue = storage.getBool(key, false);
408 if (storage.contains(key)) {
409 boolCount++;
410 continue;
411 }
412
413 float floatValue = storage.getFloat(key, -999999.0);
414 if (floatValue != -999999.0) {
415 floatCount++;
416 continue;
417 }
418
419 double doubleValue = storage.getDouble(key, -999999.0);
420 if (doubleValue != -999999.0) {
421 doubleCount++;
422 }
423 }
424
425 print(" - Strings: $stringCount");
426 print(" - Integers: $intCount");
427 print(" - Longs: $longCount");
428 print(" - Booleans: $boolCount");
429 print(" - Floats: $floatCount");
430 print(" - Doubles: $doubleCount");
431 }
432 }
433
437 void demonstrateDataMigration() {
438 if (_storageManager == null) {
439 print("StorageManager not initialized");
440 return;
441 }
442
443 print("\n=== Data Migration Demonstration ===");
444
445 // Create old and new storages
446 KeyValueStorage oldStorage = _storageManager!.getStorage("old_storage");
447 KeyValueStorage newStorage = _storageManager!.getStorage("new_storage");
448
449 // Populate old storage with data
450 oldStorage.putString("name", "John Doe");
451 oldStorage.putInt("age", 25);
452 oldStorage.putBool("is_premium", true);
453 oldStorage.putDouble("rating", 4.5);
454
455 print("Old storage before migration:");
456 _demonstrateStorageData(oldStorage);
457
458 // Migrate data from old to new storage
459 List<String> keys = oldStorage.getKeys();
460 for (String key in keys) {
461 // Migrate string data
462 String stringValue = oldStorage.getString(key, "");
463 if (stringValue.isNotEmpty) {
464 newStorage.putString(key, stringValue);
465 continue;
466 }
467
468 // Migrate integer data
469 int intValue = oldStorage.getInt(key, -999999);
470 if (intValue != -999999) {
471 newStorage.putInt(key, intValue);
472 continue;
473 }
474
475 // Migrate long data
476 long longValue = oldStorage.getLong(key, -999999);
477 if (longValue != -999999) {
478 newStorage.putLong(key, longValue);
479 continue;
480 }
481
482 // Migrate boolean data
483 bool boolValue = oldStorage.getBool(key, false);
484 if (oldStorage.contains(key)) {
485 newStorage.putBool(key, boolValue);
486 continue;
487 }
488
489 // Migrate float data
490 float floatValue = oldStorage.getFloat(key, -999999.0);
491 if (floatValue != -999999.0) {
492 newStorage.putFloat(key, floatValue);
493 continue;
494 }
495
496 // Migrate double data
497 double doubleValue = oldStorage.getDouble(key, -999999.0);
498 if (doubleValue != -999999.0) {
499 newStorage.putDouble(key, doubleValue);
500 }
501 }
502
503 print("\nNew storage after migration:");
504 _demonstrateStorageData(newStorage);
505
506 // Remove old storage
507 _storageManager!.removeStorage("old_storage");
508 print("\nRemoved old storage");
509
510 // Verify migration
511 List<String> finalStorages = _storageManager!.getStorageList();
512 print("Final storage list: $finalStorages");
513 }
514
518 void demonstrateStorageCleanup() {
519 if (_storageManager == null) {
520 print("StorageManager not initialized");
521 return;
522 }
523
524 print("\n=== Storage Cleanup Demonstration ===");
525
526 // Create some temporary storages
527 _storageManager!.getStorage("temp_cache_1");
528 _storageManager!.getStorage("temp_cache_2");
529 _storageManager!.getStorage("permanent_storage");
530
531 List<String> beforeCleanup = _storageManager!.getStorageList();
532 print("Storages before cleanup: $beforeCleanup");
533
534 // Clean up temporary storages
535 List<String> storages = _storageManager!.getStorageList();
536 for (String storageName in storages) {
537 if (storageName.startsWith("temp_")) {
538 _storageManager!.removeStorage(storageName);
539 print("Removed temporary storage: $storageName");
540 }
541 }
542
543 List<String> afterCleanup = _storageManager!.getStorageList();
544 print("Storages after cleanup: $afterCleanup");
545 }
546
550 void runAllDemonstrations() {
551 print("=== StorageManager and KeyValueStorage Examples ===\n");
552
553 demonstrateStorageManagerMethods();
554 demonstrateKeyValueStorageMethods();
555 demonstrateMultipleStorages();
556 demonstrateStorageAnalytics();
557 demonstrateDataMigration();
558 demonstrateStorageCleanup();
559
560 print("\n=== All demonstrations completed ===");
561 }
562
566 void cleanup() {
567 if (_storageManager != null) {
568 // Remove test storages
569 List<String> storages = _storageManager!.getStorageList();
570 for (String storageName in storages) {
571 if (storageName.startsWith("test_") ||
572 storageName.startsWith("temp_") ||
573 storageName.startsWith("old_") ||
574 storageName.startsWith("new_")) {
575 _storageManager!.removeStorage(storageName);
576 }
577 }
578 }
579 }
580}
581
585void main() {
587
588 // Run all demonstrations
589 example.runAllDemonstrations();
590
591 // Cleanup
592 example.cleanup();
593
594 print("Example completed successfully!");
595}