fluffychat/lib/utils/famedlysdk_store.dart

109 lines
2.7 KiB
Dart
Raw Normal View History

2021-10-26 16:50:34 +00:00
import 'dart:async';
import 'dart:core';
2020-01-26 11:17:54 +00:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:localstorage/localstorage.dart';
2020-10-13 10:20:13 +00:00
import 'package:path_provider/path_provider.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
// see https://github.com/mogol/flutter_secure_storage/issues/161#issuecomment-704578453
class AsyncMutex {
Completer<void> _completer;
Future<void> lock() async {
while (_completer != null) {
await _completer.future;
}
_completer = Completer<void>();
}
void unlock() {
assert(_completer != null);
final completer = _completer;
_completer = null;
completer.complete();
}
}
2020-05-13 13:58:59 +00:00
class Store {
2020-10-25 15:59:55 +00:00
LocalStorage storage;
2020-01-26 11:17:54 +00:00
final FlutterSecureStorage secureStorage;
static final _mutex = AsyncMutex();
2020-01-01 18:10:13 +00:00
2020-05-13 13:58:59 +00:00
Store()
2021-10-17 12:15:29 +00:00
: secureStorage =
PlatformInfos.isMobile ? const FlutterSecureStorage() : null;
2020-01-01 18:10:13 +00:00
2020-10-25 15:59:55 +00:00
Future<void> _setupLocalStorage() async {
if (storage == null) {
final directory = PlatformInfos.isBetaDesktop
? await getApplicationSupportDirectory()
: (PlatformInfos.isWeb
? null
: await getApplicationDocumentsDirectory());
storage = LocalStorage('LocalStorage', directory?.path);
2020-01-26 11:17:54 +00:00
await storage.ready;
2020-10-25 15:59:55 +00:00
}
}
Future<String> getItem(String key) async {
if (!PlatformInfos.isMobile) {
await _setupLocalStorage();
try {
2021-03-04 11:28:06 +00:00
return storage.getItem(key)?.toString();
} catch (_) {
return null;
}
2020-01-26 11:17:54 +00:00
}
2020-03-29 10:06:25 +00:00
try {
await _mutex.lock();
2020-03-29 10:06:25 +00:00
return await secureStorage.read(key: key);
} catch (_) {
return null;
2021-02-03 15:30:23 +00:00
} finally {
_mutex.unlock();
2020-03-29 10:06:25 +00:00
}
2020-01-26 11:17:54 +00:00
}
Future<bool> getItemBool(String key, [bool defaultValue]) async {
final value = await getItem(key);
if (value == null) {
return defaultValue ?? false;
}
// we also check for '1' for legacy reasons, some booleans were stored that way
return value == '1' || value.toLowerCase() == 'true';
}
2020-01-26 11:17:54 +00:00
Future<void> setItem(String key, String value) async {
2020-09-26 18:27:15 +00:00
if (!PlatformInfos.isMobile) {
2020-10-25 15:59:55 +00:00
await _setupLocalStorage();
2020-01-26 11:17:54 +00:00
return await storage.setItem(key, value);
}
2021-02-03 15:30:23 +00:00
try {
await _mutex.lock();
return await secureStorage.write(key: key, value: value);
} finally {
_mutex.unlock();
}
2020-01-26 11:17:54 +00:00
}
Future<void> setItemBool(String key, bool value) async {
await setItem(key, value.toString());
}
2020-10-25 15:59:55 +00:00
Future<void> deleteItem(String key) async {
2020-09-26 18:27:15 +00:00
if (!PlatformInfos.isMobile) {
2020-10-25 15:59:55 +00:00
await _setupLocalStorage();
return await storage.deleteItem(key);
2020-01-02 11:27:02 +00:00
}
2021-02-03 15:30:23 +00:00
try {
await _mutex.lock();
return await secureStorage.delete(key: key);
} finally {
_mutex.unlock();
}
2020-03-29 10:06:25 +00:00
}
2020-01-01 18:10:13 +00:00
}