chore: versioning of Hive boxes

This commit is contained in:
Kingkor Roy Tirtho 2023-06-30 11:21:02 +06:00
parent b54ee96233
commit c429e6f48d
5 changed files with 30 additions and 14 deletions

View File

@ -96,21 +96,33 @@ Future<void> main(List<String> 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>(
MatchedTrack.boxName,
path: (await getApplicationSupportDirectory()).path,
path: hiveCacheDir,
);
await Hive.openLazyBox<List<SkipSegment>>(
SkipSegment.boxName,
path: hiveCacheDir,
);
await PersistedStateNotifier.initializeBoxes(
path: hiveCacheDir,
);
await PersistedStateNotifier.initializeBoxes();
Catcher(
enableLogger: arguments["verbose"],

View File

@ -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<MatchedTrack> get box => Hive.lazyBox<MatchedTrack>(boxName);

View File

@ -10,8 +10,10 @@ class SkipSegment {
final int end;
SkipSegment(this.start, this.end);
static const boxName = "oss.krtirtho.spotube.skip_segments";
static LazyBox<SkipSegment> get box => Hive.lazyBox<SkipSegment>(boxName);
static String version = 'v1';
static final boxName = "oss.krtirtho.spotube.skip_segments.$version";
static LazyBox<List<SkipSegment>> get box =>
Hive.lazyBox<List<SkipSegment>>(boxName);
SkipSegment.fromJson(Map<String, dynamic> json)
: start = json['start'],

View File

@ -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<ProxyPlaylist>
preferences.searchMode != SearchMode.youtube) return [];
try {
final box = await Hive.openLazyBox<List>(SkipSegment.boxName);
final cached = await box.get(id);
final cached = await SkipSegment.box.get(id);
if (cached != null && cached.isNotEmpty) {
return List.castFrom<dynamic, SkipSegment>(cached);
}
@ -550,13 +548,13 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
"[SponsorBlock] successfully fetched skip segments for $id",
);
await box.put(
await SkipSegment.box.put(
id,
segments,
);
return List.castFrom<dynamic, SkipSegment>(segments);
} catch (e, stack) {
await box.put(id, []);
await SkipSegment.box.put(id, []);
Catcher.reportCheckedError(e, stack);
return List.castFrom<dynamic, SkipSegment>([]);
}

View File

@ -53,7 +53,7 @@ abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
}
}
static Future<void> initializeBoxes() async {
static Future<void> initializeBoxes({required String path}) async {
String? boxName = await read(kKeyBoxName);
if (boxName == null) {
@ -73,7 +73,10 @@ abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
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;