From dca8bcd7c673764714723a0e6b83117e834806d5 Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Wed, 26 Apr 2023 00:35:32 +0600 Subject: [PATCH] chore: initialize boxes in main --- lib/main.dart | 8 ++- lib/utils/persisted_state_notifier.dart | 77 ++++++++++++------------- pubspec.yaml | 2 +- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 81c0ec94..a9d18b57 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -27,9 +27,11 @@ import 'package:spotube/services/audio_player.dart'; import 'package:spotube/services/pocketbase.dart'; import 'package:spotube/services/youtube.dart'; import 'package:spotube/themes/light_theme.dart'; +import 'package:spotube/utils/persisted_state_notifier.dart'; import 'package:spotube/utils/platform.dart'; import 'package:window_manager/window_manager.dart'; import 'package:window_size/window_size.dart'; +import 'package:path_provider/path_provider.dart'; void main(List rawArgs) async { final parser = ArgParser(); @@ -70,7 +72,11 @@ void main(List rawArgs) async { WidgetsFlutterBinding.ensureInitialized(); 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(CacheTrackEngagementAdapter()); Hive.registerAdapter(CacheTrackSkipSegmentAdapter()); diff --git a/lib/utils/persisted_state_notifier.dart b/lib/utils/persisted_state_notifier.dart index f509f439..60b8d2fd 100644 --- a/lib/utils/persisted_state_notifier.dart +++ b/lib/utils/persisted_state_notifier.dart @@ -1,13 +1,19 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:typed_data'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:hive/hive.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 extends StateNotifier { final String cacheKey; @@ -23,48 +29,38 @@ abstract class PersistedStateNotifier extends StateNotifier { _load().then((_) => onInit()); } - Future _load() async { - final LazyBox box; + static late LazyBox _box; + static late LazyBox _encryptedBox; - if (encrypted) { - String? boxName = - await secureStorage.read(key: "oss.krtirtho.spotube.box_name"); + static Future initializeBoxes() async { + String? boxName = await secureStorage.read(key: kKeyBoxName); - if (boxName == null) { - await secureStorage.write( - key: "oss.krtirtho.spotube.box_name", - 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"); + if (boxName == null) { + boxName = "spotube-${PrimitiveUtils.uuid.v4()}"; + await secureStorage.write(key: kKeyBoxName, value: boxName); } + 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 _load() async { final json = await box.get(cacheKey); if (json != null) { @@ -95,7 +91,6 @@ abstract class PersistedStateNotifier extends StateNotifier { } void save() async { - final box = await Hive.openLazyBox("spotube_cache"); box.put(cacheKey, toJson()); } diff --git a/pubspec.yaml b/pubspec.yaml index 0287943c..f17e8ca1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: file_picker: ^5.2.2 fl_query: ^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 flutter: sdk: flutter