9class StorageManagerExample {
11 StorageManager? _storageManager;
12 KeyValueStorage? _userStorage;
13 KeyValueStorage? _appStorage;
14 KeyValueStorage? _cacheStorage;
16 StorageManagerExample() {
24 void _initializeSdk() {
28 _sdk = NavigineSdk.getInstance();
33 _sdk?.setUserHash(
"USER-HASH-HERE");
38 _sdk?.setServer(
"https://custom.navigine.com");
43 _storageManager = _sdk?.getStorageManager();
46 if (_storageManager !=
null) {
47 print(
"StorageManager successfully initialized");
50 print(
"Error initializing SDK: $e");
57 void _setupStorages() {
58 if (_storageManager ==
null) {
59 print(
"StorageManager not initialized");
65 _userStorage = _storageManager!.getStorage(
"user_preferences");
66 _appStorage = _storageManager!.getStorage(
"app_settings");
67 _cacheStorage = _storageManager!.getStorage(
"cache");
70 print(
"Storage instances created");
76 void demonstrateStorageManagerMethods() {
77 if (_storageManager ==
null) {
78 print(
"StorageManager not initialized");
84 List<String> storageList = _storageManager!.getStorageList();
85 print(
"Existing storages: $storageList");
90 KeyValueStorage testStorage = _storageManager!.getStorage(
"test_storage");
91 print(
"Created test storage");
95 _storageManager!.getStorage(
"another_storage");
96 List<String> updatedList = _storageManager!.getStorageList();
97 print(
"Updated storage list: $updatedList");
101 _storageManager!.removeStorage(
"test_storage");
102 print(
"Removed test storage");
105 List<String> finalList = _storageManager!.getStorageList();
106 print(
"Final storage list: $finalList");
112 void demonstrateKeyValueStorageMethods() {
113 if (_userStorage ==
null) {
114 print(
"User storage not initialized");
118 print(
"\n=== KeyValueStorage Methods Demonstration ===");
122 _userStorage!.putString(
"user_name",
"John Doe");
123 _userStorage!.putString(
"user_email",
"john.doe@example.com");
124 print(
"Stored string values");
129 _userStorage!.putInt(
"user_age", 25);
130 _userStorage!.putInt(
"login_count", 42);
131 print(
"Stored integer values");
136 _userStorage!.putLong(
"registration_timestamp", 1640995200000);
137 _userStorage!.putLong(
138 "last_login_timestamp",
139 DateTime.now().millisecondsSinceEpoch,
141 print(
"Stored long values");
146 _userStorage!.putBool(
"is_premium",
true);
147 _userStorage!.putBool(
"notifications_enabled",
false);
148 print(
"Stored boolean values");
153 _userStorage!.putFloat(
"user_rating", 4.5);
154 _userStorage!.putFloat(
"temperature", 23.5);
155 print(
"Stored float values");
160 _userStorage!.putDouble(
"user_location_lat", 55.7558);
161 _userStorage!.putDouble(
"user_location_lng", 37.6176);
162 print(
"Stored double values");
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");
175 List<String> allKeys = _userStorage!.getKeys();
176 print(
"All stored keys: $allKeys");
181 String userName = _userStorage!.getString(
"user_name",
"Unknown");
182 String userEmail = _userStorage!.getString(
"user_email",
"");
183 String nonExistent = _userStorage!.getString(
187 print(
"User name: $userName");
188 print(
"User email: $userEmail");
189 print(
"Non-existent key: $nonExistent");
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");
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");
214 bool isPremium = _userStorage!.getBool(
"is_premium",
false);
215 bool notificationsEnabled = _userStorage!.getBool(
216 "notifications_enabled",
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");
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");
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",
243 print(
"User location lat: $userLat");
244 print(
"User location lng: $userLng");
245 print(
"Non-existent double: $nonExistentDouble");
250 _userStorage!.remove(
"user_age");
251 _userStorage!.remove(
"non_existent_key");
252 print(
"Removed 'user_age' key");
256 bool stillHasAge = _userStorage!.contains(
"user_age");
257 print(
"Still contains 'user_age': $stillHasAge");
261 _userStorage!.clear();
262 print(
"Cleared all data from user storage");
266 List<String> keysAfterClear = _userStorage!.getKeys();
267 print(
"Keys after clear: $keysAfterClear");
273 void demonstrateMultipleStorages() {
274 if (_userStorage ==
null || _appStorage ==
null || _cacheStorage ==
null) {
275 print(
"Storages not initialized");
279 print(
"\n=== Multiple Storages Demonstration ===");
282 _userStorage!.putString(
"name",
"John Doe");
283 _userStorage!.putInt(
"age", 25);
284 _userStorage!.putBool(
"is_premium",
true);
286 _appStorage!.putString(
"theme",
"dark");
287 _appStorage!.putInt(
"api_version", 2);
288 _appStorage!.putBool(
"debug_mode",
false);
290 _cacheStorage!.putLong(
292 DateTime.now().millisecondsSinceEpoch,
294 _cacheStorage!.putString(
"api_token",
"abc123");
295 _cacheStorage!.putDouble(
"cached_rating", 4.2);
298 print(
"User storage data:");
299 _demonstrateStorageData(_userStorage!);
301 print(
"\nApp storage data:");
302 _demonstrateStorageData(_appStorage!);
304 print(
"\nCache storage data:");
305 _demonstrateStorageData(_cacheStorage!);
311 void _demonstrateStorageData(KeyValueStorage storage) {
312 List<String> keys = storage.getKeys();
313 for (String key in keys) {
314 if (storage.contains(key)) {
316 String stringValue = storage.getString(key,
"");
317 if (stringValue.isNotEmpty) {
318 print(
" $key: $stringValue (string)");
323 int intValue = storage.getInt(key, -999999);
324 if (intValue != -999999) {
325 print(
" $key: $intValue (int)");
330 long longValue = storage.getLong(key, -999999);
331 if (longValue != -999999) {
332 print(
" $key: $longValue (long)");
337 bool boolValue = storage.getBool(key,
false);
338 if (storage.contains(key)) {
339 print(
" $key: $boolValue (bool)");
344 float floatValue = storage.getFloat(key, -999999.0);
345 if (floatValue != -999999.0) {
346 print(
" $key: $floatValue (float)");
351 double doubleValue = storage.getDouble(key, -999999.0);
352 if (doubleValue != -999999.0) {
353 print(
" $key: $doubleValue (double)");
362 void demonstrateStorageAnalytics() {
363 if (_storageManager ==
null) {
364 print(
"StorageManager not initialized");
368 print(
"\n=== Storage Analytics ===");
371 List<String> allStorages = _storageManager!.getStorageList();
372 print(
"Total storages: ${allStorages.length}");
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");
387 for (String key in keys) {
389 String stringValue = storage.getString(key,
"");
390 if (stringValue.isNotEmpty) {
395 int intValue = storage.getInt(key, -999999);
396 if (intValue != -999999) {
401 long longValue = storage.getLong(key, -999999);
402 if (longValue != -999999) {
407 bool boolValue = storage.getBool(key,
false);
408 if (storage.contains(key)) {
413 float floatValue = storage.getFloat(key, -999999.0);
414 if (floatValue != -999999.0) {
419 double doubleValue = storage.getDouble(key, -999999.0);
420 if (doubleValue != -999999.0) {
425 print(
" - Strings: $stringCount");
426 print(
" - Integers: $intCount");
427 print(
" - Longs: $longCount");
428 print(
" - Booleans: $boolCount");
429 print(
" - Floats: $floatCount");
430 print(
" - Doubles: $doubleCount");
437 void demonstrateDataMigration() {
438 if (_storageManager ==
null) {
439 print(
"StorageManager not initialized");
443 print(
"\n=== Data Migration Demonstration ===");
446 KeyValueStorage oldStorage = _storageManager!.getStorage(
"old_storage");
447 KeyValueStorage newStorage = _storageManager!.getStorage(
"new_storage");
450 oldStorage.putString(
"name",
"John Doe");
451 oldStorage.putInt(
"age", 25);
452 oldStorage.putBool(
"is_premium",
true);
453 oldStorage.putDouble(
"rating", 4.5);
455 print(
"Old storage before migration:");
456 _demonstrateStorageData(oldStorage);
459 List<String> keys = oldStorage.getKeys();
460 for (String key in keys) {
462 String stringValue = oldStorage.getString(key,
"");
463 if (stringValue.isNotEmpty) {
464 newStorage.putString(key, stringValue);
469 int intValue = oldStorage.getInt(key, -999999);
470 if (intValue != -999999) {
471 newStorage.putInt(key, intValue);
476 long longValue = oldStorage.getLong(key, -999999);
477 if (longValue != -999999) {
478 newStorage.putLong(key, longValue);
483 bool boolValue = oldStorage.getBool(key,
false);
484 if (oldStorage.contains(key)) {
485 newStorage.putBool(key, boolValue);
490 float floatValue = oldStorage.getFloat(key, -999999.0);
491 if (floatValue != -999999.0) {
492 newStorage.putFloat(key, floatValue);
497 double doubleValue = oldStorage.getDouble(key, -999999.0);
498 if (doubleValue != -999999.0) {
499 newStorage.putDouble(key, doubleValue);
503 print(
"\nNew storage after migration:");
504 _demonstrateStorageData(newStorage);
507 _storageManager!.removeStorage(
"old_storage");
508 print(
"\nRemoved old storage");
511 List<String> finalStorages = _storageManager!.getStorageList();
512 print(
"Final storage list: $finalStorages");
518 void demonstrateStorageCleanup() {
519 if (_storageManager ==
null) {
520 print(
"StorageManager not initialized");
524 print(
"\n=== Storage Cleanup Demonstration ===");
527 _storageManager!.getStorage(
"temp_cache_1");
528 _storageManager!.getStorage(
"temp_cache_2");
529 _storageManager!.getStorage(
"permanent_storage");
531 List<String> beforeCleanup = _storageManager!.getStorageList();
532 print(
"Storages before cleanup: $beforeCleanup");
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");
543 List<String> afterCleanup = _storageManager!.getStorageList();
544 print(
"Storages after cleanup: $afterCleanup");
550 void runAllDemonstrations() {
551 print(
"=== StorageManager and KeyValueStorage Examples ===\n");
553 demonstrateStorageManagerMethods();
554 demonstrateKeyValueStorageMethods();
555 demonstrateMultipleStorages();
556 demonstrateStorageAnalytics();
557 demonstrateDataMigration();
558 demonstrateStorageCleanup();
560 print(
"\n=== All demonstrations completed ===");
567 if (_storageManager !=
null) {
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);