FurryChat/lib/utils/famedlysdk_store.dart

93 lines
2.5 KiB
Dart
Raw Normal View History

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