🔀 Merge database system from spotube

This commit is contained in:
2024-08-27 15:22:18 +08:00
parent 95b04adede
commit a162619a88
32 changed files with 5978 additions and 14 deletions

View File

@ -0,0 +1,61 @@
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:rhythm_box/platform.dart';
import 'package:rhythm_box/services/kv_store/kv_store.dart';
import 'package:uuid/uuid.dart';
abstract class EncryptedKvStoreService {
static const _storage = FlutterSecureStorage(
aOptions: AndroidOptions(
encryptedSharedPreferences: true,
),
);
static FlutterSecureStorage get storage => _storage;
static String? _encryptionKeySync;
static Future<void> initialize() async {
_encryptionKeySync = await encryptionKey;
}
static String get encryptionKeySync => _encryptionKeySync!;
static bool get isUnsupportedPlatform =>
PlatformInfo.isMacOS ||
PlatformInfo.isIOS ||
(PlatformInfo.isLinux && !PlatformInfo.isInFlatpak);
static Future<String> get encryptionKey async {
if (isUnsupportedPlatform) {
return KVStoreService.encryptionKey;
}
try {
final value = await _storage.read(key: 'encryption');
final key = const Uuid().v4();
if (value == null) {
await setEncryptionKey(key);
return key;
}
return value;
} catch (e) {
return KVStoreService.encryptionKey;
}
}
static Future<void> setEncryptionKey(String key) async {
if (isUnsupportedPlatform) {
await KVStoreService.setEncryptionKey(key);
return;
}
try {
await _storage.write(key: 'encryption', value: key);
} catch (e) {
await KVStoreService.setEncryptionKey(key);
} finally {
_encryptionKeySync = key;
}
}
}

View File

@ -0,0 +1,90 @@
import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:rhythm_box/services/wm_tools.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
abstract class KVStoreService {
static SharedPreferences? _sharedPreferences;
static SharedPreferences get sharedPreferences => _sharedPreferences!;
static Future<void> initialize() async {
_sharedPreferences = await SharedPreferences.getInstance();
}
static bool get doneGettingStarted =>
sharedPreferences.getBool('doneGettingStarted') ?? false;
static Future<void> setDoneGettingStarted(bool value) async =>
await sharedPreferences.setBool('doneGettingStarted', value);
static bool get askedForBatteryOptimization =>
sharedPreferences.getBool('askedForBatteryOptimization') ?? false;
static Future<void> setAskedForBatteryOptimization(bool value) async =>
await sharedPreferences.setBool('askedForBatteryOptimization', value);
static List<String> get recentSearches =>
sharedPreferences.getStringList('recentSearches') ?? [];
static Future<void> setRecentSearches(List<String> value) async =>
await sharedPreferences.setStringList('recentSearches', value);
static WindowSize? get windowSize {
final raw = sharedPreferences.getString('windowSize');
if (raw == null) {
return null;
}
return WindowSize.fromJson(jsonDecode(raw));
}
static Future<void> setWindowSize(WindowSize value) async =>
await sharedPreferences.setString(
'windowSize',
jsonEncode(
value.toJson(),
),
);
static String get encryptionKey {
final value = sharedPreferences.getString('encryption');
final key = const Uuid().v4();
if (value == null) {
setEncryptionKey(key);
return key;
}
return value;
}
static Future<void> setEncryptionKey(String key) async {
await sharedPreferences.setString('encryption', key);
}
static IV get ivKey {
final iv = sharedPreferences.getString('iv');
final value = IV.fromSecureRandom(8);
if (iv == null) {
setIVKey(value);
return value;
}
return IV.fromBase64(iv);
}
static Future<void> setIVKey(IV iv) async {
await sharedPreferences.setString('iv', iv.base64);
}
static double get volume => sharedPreferences.getDouble('volume') ?? 1.0;
static Future<void> setVolume(double value) async =>
await sharedPreferences.setDouble('volume', value);
static bool get hasMigratedToDrift =>
sharedPreferences.getBool('hasMigratedToDrift') ?? false;
static Future<void> setHasMigratedToDrift(bool value) async =>
await sharedPreferences.setBool('hasMigratedToDrift', value);
}