fluffychat/lib/utils/famedlysdk_store.dart

107 lines
3.0 KiB
Dart
Raw Normal View History

2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
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-13 15:58:59 +02:00
import './database/shared.dart';
import '../config/setting_keys.dart';
2020-05-13 15:58:59 +02:00
import 'package:random_string/random_string.dart';
Future<Database> getDatabase(Client client) async {
2020-07-20 17:33:52 +02:00
while (_generateDatabaseLock) {
await Future.delayed(Duration(milliseconds: 50));
2020-05-13 15:58:59 +02:00
}
2020-07-20 17:33:52 +02:00
_generateDatabaseLock = true;
try {
if (_db != null) return _db;
final store = Store();
var password = await store.getItem(SettingKeys.databasePassword);
2020-10-25 16:59:55 +01:00
var newPassword = false;
2020-07-20 17:33:52 +02:00
if (password == null || password.isEmpty) {
2020-10-25 16:59:55 +01:00
newPassword = true;
2020-07-20 17:33:52 +02:00
password = randomString(255);
}
_db = await constructDb(
logStatements: false,
filename: 'moor.sqlite',
password: password,
);
2020-10-25 16:59:55 +01:00
if (newPassword) {
await store.setItem(SettingKeys.databasePassword, password);
2020-07-20 17:33:52 +02:00
}
return _db;
} finally {
_generateDatabaseLock = false;
2020-05-13 15:58:59 +02:00
}
}
2020-01-01 19:10:13 +01:00
Database _db;
2020-07-20 17:33:52 +02:00
bool _generateDatabaseLock = false;
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;
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 {
2020-10-28 10:15:06 +01:00
return await storage.getItem(key)?.toString();
} catch (_) {
return null;
}
2020-01-26 12:17:54 +01:00
}
2020-03-29 12:06:25 +02:00
try {
return await secureStorage.read(key: key);
} catch (_) {
return null;
}
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);
}
2020-10-25 16:59:55 +01:00
return await secureStorage.write(key: key, value: value);
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
}
2020-10-25 16:59:55 +01:00
return await secureStorage.delete(key: key);
2020-03-29 12:06:25 +02:00
}
2020-01-01 19:10:13 +01:00
}