fluffychat/lib/utils/famedlysdk_store.dart

106 lines
2.7 KiB
Dart
Raw Normal View History

2020-09-26 20:27:15 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
2020-01-26 12:17:54 +01:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:localstorage/localstorage.dart';
2020-10-13 12:20:13 +02:00
import 'package:path_provider/path_provider.dart';
2020-01-26 12:17:54 +01:00
import 'dart:async';
import 'dart:core';
// 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 15:58:59 +02:00
class Store {
2020-10-25 16:59:55 +01:00
LocalStorage storage;
2020-01-26 12:17:54 +01:00
final FlutterSecureStorage secureStorage;
static final _mutex = AsyncMutex();
2020-01-01 19:10:13 +01:00
2020-05-13 15:58:59 +02:00
Store()
2020-10-25 16:59:55 +01:00
: secureStorage = PlatformInfos.isMobile ? FlutterSecureStorage() : null;
2020-01-01 19:10:13 +01:00
2020-10-25 16:59:55 +01: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 12:17:54 +01:00
await storage.ready;
2020-10-25 16:59:55 +01:00
}
}
Future<String> getItem(String key) async {
if (!PlatformInfos.isMobile) {
await _setupLocalStorage();
try {
2021-03-04 12:28:06 +01:00
return storage.getItem(key)?.toString();
} catch (_) {
return null;
}
2020-01-26 12:17:54 +01:00
}
2020-03-29 12:06:25 +02:00
try {
await _mutex.lock();
2020-03-29 12:06:25 +02:00
return await secureStorage.read(key: key);
} catch (_) {
return null;
2021-02-03 16:30:23 +01:00
} finally {
_mutex.unlock();
2020-03-29 12:06:25 +02:00
}
2020-01-26 12:17:54 +01: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 12:17:54 +01:00
Future<void> setItem(String key, String value) async {
2020-09-26 20:27:15 +02:00
if (!PlatformInfos.isMobile) {
2020-10-25 16:59:55 +01:00
await _setupLocalStorage();
2020-01-26 12:17:54 +01:00
return await storage.setItem(key, value);
}
2021-02-03 16:30:23 +01:00
try {
await _mutex.lock();
return await secureStorage.write(key: key, value: value);
} finally {
_mutex.unlock();
}
2020-01-26 12:17:54 +01:00
}
Future<void> setItemBool(String key, bool value) async {
await setItem(key, value.toString());
}
2020-10-25 16:59:55 +01:00
Future<void> deleteItem(String key) async {
2020-09-26 20:27:15 +02:00
if (!PlatformInfos.isMobile) {
2020-10-25 16:59:55 +01:00
await _setupLocalStorage();
return await storage.deleteItem(key);
2020-01-02 12:27:02 +01:00
}
2021-02-03 16:30:23 +01:00
try {
await _mutex.lock();
return await secureStorage.delete(key: key);
} finally {
_mutex.unlock();
}
2020-03-29 12:06:25 +02:00
}
2020-01-01 19:10:13 +01:00
}