diff --git a/lib/main.dart b/lib/main.dart index ace4c871..3e9348ff 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -96,21 +96,33 @@ Future main(List rawArgs) async { await SystemTheme.accentColor.load(); MetadataGod.initialize(); + + final hiveCacheDir = (await getApplicationSupportDirectory()).path; + await QueryClient.initialize( cachePrefix: "oss.krtirtho.spotube", - cacheDir: (await getApplicationSupportDirectory()).path, + cacheDir: hiveCacheDir, connectivity: FlQueryConnectivityPlusAdapter(), ); Hive.registerAdapter(MatchedTrackAdapter()); Hive.registerAdapter(SkipSegmentAdapter()); Hive.registerAdapter(SearchModeAdapter()); + // Cache versioning entities with Adapter + MatchedTrack.version = 'v1'; + SkipSegment.version = 'v1'; + await Hive.openLazyBox( MatchedTrack.boxName, - path: (await getApplicationSupportDirectory()).path, + path: hiveCacheDir, + ); + await Hive.openLazyBox>( + SkipSegment.boxName, + path: hiveCacheDir, + ); + await PersistedStateNotifier.initializeBoxes( + path: hiveCacheDir, ); - - await PersistedStateNotifier.initializeBoxes(); Catcher( enableLogger: arguments["verbose"], diff --git a/lib/models/matched_track.dart b/lib/models/matched_track.dart index 4c7b1131..b7cc0a3f 100644 --- a/lib/models/matched_track.dart +++ b/lib/models/matched_track.dart @@ -15,7 +15,8 @@ class MatchedTrack { bool get isSynced => id != null; - static const boxName = "oss.krtirtho.spotube.matched_tracks"; + static String version = 'v1'; + static final boxName = "oss.krtirtho.spotube.matched_tracks.$version"; static LazyBox get box => Hive.lazyBox(boxName); diff --git a/lib/models/skip_segment.dart b/lib/models/skip_segment.dart index 889f7ac7..ef4cb889 100644 --- a/lib/models/skip_segment.dart +++ b/lib/models/skip_segment.dart @@ -10,8 +10,10 @@ class SkipSegment { final int end; SkipSegment(this.start, this.end); - static const boxName = "oss.krtirtho.spotube.skip_segments"; - static LazyBox get box => Hive.lazyBox(boxName); + static String version = 'v1'; + static final boxName = "oss.krtirtho.spotube.skip_segments.$version"; + static LazyBox> get box => + Hive.lazyBox>(boxName); SkipSegment.fromJson(Map json) : start = json['start'], diff --git a/lib/provider/proxy_playlist/proxy_playlist_provider.dart b/lib/provider/proxy_playlist/proxy_playlist_provider.dart index 7c1c5c91..cefb3b63 100644 --- a/lib/provider/proxy_playlist/proxy_playlist_provider.dart +++ b/lib/provider/proxy_playlist/proxy_playlist_provider.dart @@ -4,7 +4,6 @@ import 'dart:convert'; import 'package:catcher/catcher.dart'; import 'package:collection/collection.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:hive/hive.dart'; import 'package:http/http.dart'; import 'package:palette_generator/palette_generator.dart'; import 'package:spotify/spotify.dart'; @@ -509,8 +508,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier preferences.searchMode != SearchMode.youtube) return []; try { - final box = await Hive.openLazyBox(SkipSegment.boxName); - final cached = await box.get(id); + final cached = await SkipSegment.box.get(id); if (cached != null && cached.isNotEmpty) { return List.castFrom(cached); } @@ -550,13 +548,13 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier "[SponsorBlock] successfully fetched skip segments for $id", ); - await box.put( + await SkipSegment.box.put( id, segments, ); return List.castFrom(segments); } catch (e, stack) { - await box.put(id, []); + await SkipSegment.box.put(id, []); Catcher.reportCheckedError(e, stack); return List.castFrom([]); } diff --git a/lib/utils/persisted_state_notifier.dart b/lib/utils/persisted_state_notifier.dart index 7cc1652d..551d7438 100644 --- a/lib/utils/persisted_state_notifier.dart +++ b/lib/utils/persisted_state_notifier.dart @@ -53,7 +53,7 @@ abstract class PersistedStateNotifier extends StateNotifier { } } - static Future initializeBoxes() async { + static Future initializeBoxes({required String path}) async { String? boxName = await read(kKeyBoxName); if (boxName == null) { @@ -73,7 +73,10 @@ abstract class PersistedStateNotifier extends StateNotifier { encryptionCipher: HiveAesCipher(base64Url.decode(encryptionKey)), ); - _box = await Hive.openLazyBox("spotube_cache"); + _box = await Hive.openLazyBox( + "spotube_cache", + path: path, + ); } LazyBox get box => encrypted ? _encryptedBox : _box;