2024-08-27 07:22:18 +00:00
|
|
|
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 askedForBatteryOptimization =>
|
2024-08-27 15:09:16 +00:00
|
|
|
sharedPreferences.getBool('asked_for_battery_optimization') ?? false;
|
2024-08-27 07:22:18 +00:00
|
|
|
static Future<void> setAskedForBatteryOptimization(bool value) async =>
|
2024-08-27 15:09:16 +00:00
|
|
|
await sharedPreferences.setBool('asked_for_battery_optimization', value);
|
2024-08-27 07:22:18 +00:00
|
|
|
|
|
|
|
static List<String> get recentSearches =>
|
2024-08-27 15:09:16 +00:00
|
|
|
sharedPreferences.getStringList('recent_searches') ?? [];
|
2024-08-27 07:22:18 +00:00
|
|
|
static Future<void> setRecentSearches(List<String> value) async =>
|
2024-08-27 15:09:16 +00:00
|
|
|
await sharedPreferences.setStringList('recent_searches', value);
|
2024-08-27 07:22:18 +00:00
|
|
|
|
|
|
|
static WindowSize? get windowSize {
|
2024-08-27 15:09:16 +00:00
|
|
|
final raw = sharedPreferences.getString('window_size');
|
2024-08-27 07:22:18 +00:00
|
|
|
|
|
|
|
if (raw == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return WindowSize.fromJson(jsonDecode(raw));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> setWindowSize(WindowSize value) async =>
|
|
|
|
await sharedPreferences.setString(
|
2024-08-27 15:09:16 +00:00
|
|
|
'window_size',
|
2024-08-27 07:22:18 +00:00
|
|
|
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);
|
|
|
|
}
|