mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 07:55:18 +00:00
chore: initialize boxes in main
This commit is contained in:
parent
b110d83456
commit
dca8bcd7c6
@ -27,9 +27,11 @@ import 'package:spotube/services/audio_player.dart';
|
|||||||
import 'package:spotube/services/pocketbase.dart';
|
import 'package:spotube/services/pocketbase.dart';
|
||||||
import 'package:spotube/services/youtube.dart';
|
import 'package:spotube/services/youtube.dart';
|
||||||
import 'package:spotube/themes/light_theme.dart';
|
import 'package:spotube/themes/light_theme.dart';
|
||||||
|
import 'package:spotube/utils/persisted_state_notifier.dart';
|
||||||
import 'package:spotube/utils/platform.dart';
|
import 'package:spotube/utils/platform.dart';
|
||||||
import 'package:window_manager/window_manager.dart';
|
import 'package:window_manager/window_manager.dart';
|
||||||
import 'package:window_size/window_size.dart';
|
import 'package:window_size/window_size.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
void main(List<String> rawArgs) async {
|
void main(List<String> rawArgs) async {
|
||||||
final parser = ArgParser();
|
final parser = ArgParser();
|
||||||
@ -70,7 +72,11 @@ void main(List<String> rawArgs) async {
|
|||||||
|
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
MetadataGod.initialize();
|
MetadataGod.initialize();
|
||||||
await QueryClient.initialize(cachePrefix: "oss.krtirtho.spotube");
|
await QueryClient.initialize(
|
||||||
|
cachePrefix: "oss.krtirtho.spotube",
|
||||||
|
cacheDir: (await getApplicationSupportDirectory()).path,
|
||||||
|
);
|
||||||
|
await PersistedStateNotifier.initializeBoxes();
|
||||||
Hive.registerAdapter(CacheTrackAdapter());
|
Hive.registerAdapter(CacheTrackAdapter());
|
||||||
Hive.registerAdapter(CacheTrackEngagementAdapter());
|
Hive.registerAdapter(CacheTrackEngagementAdapter());
|
||||||
Hive.registerAdapter(CacheTrackSkipSegmentAdapter());
|
Hive.registerAdapter(CacheTrackSkipSegmentAdapter());
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:typed_data';
|
|
||||||
|
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
import 'package:spotube/utils/primitive_utils.dart';
|
import 'package:spotube/utils/primitive_utils.dart';
|
||||||
|
|
||||||
const secureStorage = FlutterSecureStorage();
|
const secureStorage = FlutterSecureStorage(
|
||||||
|
aOptions: AndroidOptions(
|
||||||
|
encryptedSharedPreferences: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
const kKeyBoxName = "spotube_box_name";
|
||||||
|
String getBoxKey(String boxName) => "spotube_box_$boxName";
|
||||||
|
|
||||||
abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
|
abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
|
||||||
final String cacheKey;
|
final String cacheKey;
|
||||||
@ -23,48 +29,38 @@ abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
|
|||||||
_load().then((_) => onInit());
|
_load().then((_) => onInit());
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _load() async {
|
static late LazyBox _box;
|
||||||
final LazyBox box;
|
static late LazyBox _encryptedBox;
|
||||||
|
|
||||||
if (encrypted) {
|
static Future<void> initializeBoxes() async {
|
||||||
String? boxName =
|
String? boxName = await secureStorage.read(key: kKeyBoxName);
|
||||||
await secureStorage.read(key: "oss.krtirtho.spotube.box_name");
|
|
||||||
|
|
||||||
if (boxName == null) {
|
if (boxName == null) {
|
||||||
await secureStorage.write(
|
boxName = "spotube-${PrimitiveUtils.uuid.v4()}";
|
||||||
key: "oss.krtirtho.spotube.box_name",
|
await secureStorage.write(key: kKeyBoxName, value: boxName);
|
||||||
value: ".spotube-${PrimitiveUtils.uuid.v4()}",
|
|
||||||
);
|
|
||||||
boxName =
|
|
||||||
await secureStorage.read(key: "oss.krtirtho.spotube.box_name");
|
|
||||||
} else {
|
|
||||||
boxName = ".spotube-$boxName";
|
|
||||||
}
|
|
||||||
|
|
||||||
final rawKey =
|
|
||||||
await secureStorage.read(key: "oss.krtirtho.spotube.$boxName");
|
|
||||||
|
|
||||||
Uint8List? encryptionKey =
|
|
||||||
rawKey == null ? null : base64Url.decode(rawKey);
|
|
||||||
|
|
||||||
if (encryptionKey == null) {
|
|
||||||
await secureStorage.write(
|
|
||||||
key: "oss.krtirtho.spotube.$boxName",
|
|
||||||
value: base64UrlEncode(Hive.generateSecureKey()),
|
|
||||||
);
|
|
||||||
encryptionKey = base64Url.decode(
|
|
||||||
(await secureStorage.read(key: "oss.krtirtho.spotube.$boxName"))!,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
box = await Hive.openLazyBox(
|
|
||||||
boxName!,
|
|
||||||
encryptionCipher: HiveAesCipher(encryptionKey),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
box = await Hive.openLazyBox("spotube_cache");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String? encryptionKey = await secureStorage.read(key: getBoxKey(boxName));
|
||||||
|
|
||||||
|
if (encryptionKey == null) {
|
||||||
|
encryptionKey = base64Url.encode(Hive.generateSecureKey());
|
||||||
|
await secureStorage.write(
|
||||||
|
key: getBoxKey(boxName),
|
||||||
|
value: encryptionKey,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
_encryptedBox = await Hive.openLazyBox(
|
||||||
|
boxName,
|
||||||
|
encryptionCipher: HiveAesCipher(base64Url.decode(encryptionKey)),
|
||||||
|
);
|
||||||
|
|
||||||
|
_box = await Hive.openLazyBox("spotube_cache");
|
||||||
|
}
|
||||||
|
|
||||||
|
LazyBox get box => encrypted ? _encryptedBox : _box;
|
||||||
|
|
||||||
|
Future<void> _load() async {
|
||||||
final json = await box.get(cacheKey);
|
final json = await box.get(cacheKey);
|
||||||
|
|
||||||
if (json != null) {
|
if (json != null) {
|
||||||
@ -95,7 +91,6 @@ abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void save() async {
|
void save() async {
|
||||||
final box = await Hive.openLazyBox("spotube_cache");
|
|
||||||
box.put(cacheKey, toJson());
|
box.put(cacheKey, toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ dependencies:
|
|||||||
file_picker: ^5.2.2
|
file_picker: ^5.2.2
|
||||||
fl_query: ^1.0.0-alpha.2
|
fl_query: ^1.0.0-alpha.2
|
||||||
fl_query_hooks: ^1.0.0-alpha.2
|
fl_query_hooks: ^1.0.0-alpha.2
|
||||||
fluent_ui: ^4.3.0
|
fluent_ui: 4.3.0
|
||||||
fluentui_system_icons: ^1.1.189
|
fluentui_system_icons: ^1.1.189
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
Loading…
Reference in New Issue
Block a user