fluffychat/lib/utils/famedlysdk_store.dart

123 lines
3.1 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'dart:async';
import 'dart:core';
import 'package:device_info_plus/device_info_plus.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';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
// import 'package:flutter_secure_storage/flutter_secure_storage.dart';
// see https://github.com/mogol/flutter_secure_storage/issues/161#issuecomment-704578453
class AsyncMutex {
2022-01-29 12:35:03 +01:00
Completer<void>? _completer;
Future<void> lock() async {
while (_completer != null) {
2022-01-29 12:35:03 +01:00
await _completer!.future;
}
_completer = Completer<void>();
}
void unlock() {
assert(_completer != null);
2022-01-29 12:35:03 +01:00
final completer = _completer!;
_completer = null;
completer.complete();
}
}
2020-05-13 15:58:59 +02:00
class Store {
static FlutterSecureStorage? secureStorage;
static FutureOr<void> init() {
if (PlatformInfos.isMobile) {
if (PlatformInfos.isAndroid) {
return DeviceInfoPlugin().androidInfo.then((info) {
if ((info.version.sdkInt ?? 16) >= 19) {
secureStorage = const FlutterSecureStorage();
}
});
} else {
secureStorage = const FlutterSecureStorage();
}
}
}
2022-01-29 12:35:03 +01:00
LocalStorage? storage;
static final _mutex = AsyncMutex();
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);
2022-01-29 12:35:03 +01:00
await storage!.ready;
2020-10-25 16:59:55 +01:00
}
}
2022-01-29 12:35:03 +01:00
Future<String?> getItem(String key) async {
2020-10-25 16:59:55 +01:00
if (!PlatformInfos.isMobile) {
await _setupLocalStorage();
try {
2022-01-29 12:35:03 +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();
2022-01-29 12:35:03 +01:00
return await secureStorage!.read(key: key);
2020-03-29 12:06:25 +02:00
} 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
}
2022-01-29 12:35:03 +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';
}
2022-01-29 12:35:03 +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();
2022-01-29 12:35:03 +01:00
return await storage!.setItem(key, value);
2020-01-26 12:17:54 +01:00
}
2021-02-03 16:30:23 +01:00
try {
await _mutex.lock();
2022-01-29 12:35:03 +01:00
return await secureStorage!.write(key: key, value: value);
2021-02-03 16:30:23 +01:00
} 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();
2022-01-29 12:35:03 +01:00
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();
2022-01-29 12:35:03 +01:00
return await secureStorage!.delete(key: key);
2021-02-03 16:30:23 +01:00
} finally {
_mutex.unlock();
}
2020-03-29 12:06:25 +02:00
}
2020-01-01 19:10:13 +01:00
}