spotube/lib/services/kv_store/kv_store.dart
Kingkor Roy Tirtho 1e6d709e04
feat: implement yt-dlp for desktop and NewPipeExtractor for Android (#2316)
* feat: add youtube engine abstraction and yt-dlp integration

* chore: add yt-dlp as optional dependency

* feat: implement custom path support for youtube engines

* chore: check for custom path in setting engine select dropdown

* chore: update yt_dlp_dart

* chore: setting video url instead of video id in fetchSiblings

* feat: implement NewPipe engine

* chore: update local path to git url for flutter_new_pipe_extractor package

* chore: fix android build isn't working

* chore: fix routes not working when initially signing in

* refactor: drop fallback support to different sources
2025-02-11 21:36:07 +06:00

119 lines
3.3 KiB
Dart

import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/models/database/database.dart';
import 'package:spotube/services/wm_tools/wm_tools.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);
static Map<String, dynamic>? get _youtubeEnginePaths {
final jsonRaw = sharedPreferences.getString('ytDlpPath');
if (jsonRaw == null) {
return null;
}
return jsonDecode(jsonRaw);
}
static String? getYoutubeEnginePath(YoutubeClientEngine engine) {
return _youtubeEnginePaths?[engine.name];
}
static Future<void> setYoutubeEnginePath(
YoutubeClientEngine engine,
String path,
) async {
await sharedPreferences.setString(
'ytDlpPath',
jsonEncode({
...?_youtubeEnginePaths,
engine.name: path,
}),
);
}
}