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';
|
2020-05-20 09:10:13 +02:00
|
|
|
|
2021-02-03 14:50:03 +01:00
|
|
|
// 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;
|
2021-02-03 14:50:03 +01:00
|
|
|
static final _mutex = AsyncMutex();
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2020-05-13 15:58:59 +02:00
|
|
|
Store()
|
2021-10-17 14:15:29 +02:00
|
|
|
: secureStorage =
|
|
|
|
PlatformInfos.isMobile ? const 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();
|
2020-03-06 13:05:52 +01:00
|
|
|
try {
|
2021-03-04 12:28:06 +01:00
|
|
|
return storage.getItem(key)?.toString();
|
2020-03-06 13:05:52 +01:00
|
|
|
} catch (_) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-01-26 12:17:54 +01:00
|
|
|
}
|
2020-03-29 12:06:25 +02:00
|
|
|
try {
|
2021-02-03 14:50:03 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-07 12:00:41 +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
|
|
|
}
|
|
|
|
|
2021-01-23 14:06:00 +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
|
|
|
}
|