From 518fc5a657a123a72d3909e65bbb18b6615df6aa Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 6 Jun 2022 12:20:34 +0200 Subject: [PATCH] refactor: Remove legacy store --- lib/utils/famedlysdk_store.dart | 17 ---- lib/utils/legacy_famedlysdk_store.dart | 108 ------------------------- 2 files changed, 125 deletions(-) delete mode 100644 lib/utils/legacy_famedlysdk_store.dart diff --git a/lib/utils/famedlysdk_store.dart b/lib/utils/famedlysdk_store.dart index b42d7f42..2f16b596 100644 --- a/lib/utils/famedlysdk_store.dart +++ b/lib/utils/famedlysdk_store.dart @@ -1,10 +1,7 @@ import 'dart:core'; -import 'package:matrix/matrix.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'legacy_famedlysdk_store.dart' as legacy; - class Store { SharedPreferences? _prefs; @@ -14,25 +11,11 @@ class Store { Future getItem(String key) async { await _setupLocalStorage(); - final legacyVal = await legacy.Store().getItem(key); - if (legacyVal != null) { - Logs().d('Found legacy preference for $key'); - await setItem(key, legacyVal); - legacy.Store().deleteItem(key); - return legacyVal; - } return _prefs!.getString(key); } Future getItemBool(String key, [bool? defaultValue]) async { await _setupLocalStorage(); - final legacyVal = await legacy.Store().getItemBool(key); - if (legacyVal != null) { - Logs().d('Found legacy preference for $key'); - await setItemBool(key, legacyVal); - legacy.Store().deleteItem(key); - return legacyVal; - } return _prefs!.getBool(key) ?? defaultValue ?? true; } diff --git a/lib/utils/legacy_famedlysdk_store.dart b/lib/utils/legacy_famedlysdk_store.dart deleted file mode 100644 index 45b0dba2..00000000 --- a/lib/utils/legacy_famedlysdk_store.dart +++ /dev/null @@ -1,108 +0,0 @@ -import 'dart:async'; -import 'dart:core'; - -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; -import 'package:localstorage/localstorage.dart'; -import 'package:path_provider/path_provider.dart'; - -import 'package:fluffychat/utils/platform_infos.dart'; - -// see https://github.com/mogol/flutter_secure_storage/issues/161#issuecomment-704578453 -class AsyncMutex { - Completer? _completer; - - Future lock() async { - while (_completer != null) { - await _completer!.future; - } - - _completer = Completer(); - } - - void unlock() { - assert(_completer != null); - final completer = _completer!; - _completer = null; - completer.complete(); - } -} - -class Store { - LocalStorage? storage; - final FlutterSecureStorage? secureStorage; - static final _mutex = AsyncMutex(); - - Store() - : secureStorage = - PlatformInfos.isMobile ? const FlutterSecureStorage() : null; - - Future _setupLocalStorage() async { - if (storage == null) { - final directory = PlatformInfos.isBetaDesktop - ? await getApplicationSupportDirectory() - : (PlatformInfos.isWeb - ? null - : await getApplicationDocumentsDirectory()); - storage = LocalStorage('LocalStorage', directory?.path); - await storage!.ready; - } - } - - Future getItem(String key) async { - if (!PlatformInfos.isMobile) { - await _setupLocalStorage(); - try { - return storage!.getItem(key)?.toString(); - } catch (_) { - return null; - } - } - try { - await _mutex.lock(); - return await secureStorage!.read(key: key); - } catch (_) { - return null; - } finally { - _mutex.unlock(); - } - } - - Future getItemBool(String key, [bool? defaultValue]) async { - final value = await getItem(key); - if (value == null) { - return defaultValue; - } - // we also check for '1' for legacy reasons, some booleans were stored that way - return value == '1' || value.toLowerCase() == 'true'; - } - - Future setItem(String key, String? value) async { - if (!PlatformInfos.isMobile) { - await _setupLocalStorage(); - return await storage!.setItem(key, value); - } - try { - await _mutex.lock(); - return await secureStorage!.write(key: key, value: value); - } finally { - _mutex.unlock(); - } - } - - Future setItemBool(String key, bool value) async { - await setItem(key, value.toString()); - } - - Future deleteItem(String key) async { - if (!PlatformInfos.isMobile) { - await _setupLocalStorage(); - return await storage!.deleteItem(key); - } - try { - await _mutex.lock(); - return await secureStorage!.delete(key: key); - } finally { - _mutex.unlock(); - } - } -}