From 423c5b7d842f39f985a2d89e202a74e891539cf3 Mon Sep 17 00:00:00 2001 From: James Harvey <44349936+jmshrv@users.noreply.github.com> Date: Fri, 11 Aug 2023 20:46:49 +0100 Subject: [PATCH] Fix incorrect IDs in blurhash migration --- .flutter | 2 +- lib/main.dart | 13 ++++++++- lib/models/finamp_models.dart | 8 +++++ lib/models/finamp_models.g.dart | 8 +++-- lib/services/downloads_helper.dart | 37 ++++++++++++++++++++++++ lib/services/finamp_settings_helper.dart | 9 ++++++ 6 files changed, 73 insertions(+), 4 deletions(-) diff --git a/.flutter b/.flutter index f468f3366..4d9e56e69 160000 --- a/.flutter +++ b/.flutter @@ -1 +1 @@ -Subproject commit f468f3366c26a5092eb964a230ce7892fda8f2f8 +Subproject commit 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf diff --git a/lib/main.dart b/lib/main.dart index 6d7c8a8d5..33aa5c349 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -93,11 +93,22 @@ void _setupJellyfinApiData() { Future _setupDownloadsHelper() async { GetIt.instance.registerSingleton(DownloadsHelper()); + final downloadsHelper = GetIt.instance(); + + // We awkwardly cache this value since going from 0.6.14 -> 0.6.16 will switch + // hasCompletedBlurhashImageMigration despite doing a fixed migration + final shouldRunBlurhashImageMigrationIdFix = + FinampSettingsHelper.finampSettings.shouldRunBlurhashImageMigrationIdFix; if (!FinampSettingsHelper.finampSettings.hasCompletedBlurhashImageMigration) { - await GetIt.instance().migrateBlurhashImages(); + await downloadsHelper.migrateBlurhashImages(); FinampSettingsHelper.setHasCompletedBlurhashImageMigration(true); } + + if (shouldRunBlurhashImageMigrationIdFix) { + await downloadsHelper.fixBlurhashMigrationIds(); + FinampSettingsHelper.setHasCompletedBlurhashImageMigrationIdFix(true); + } } Future _setupDownloader() async { diff --git a/lib/models/finamp_models.dart b/lib/models/finamp_models.dart index ef1596632..e4d037a28 100644 --- a/lib/models/finamp_models.dart +++ b/lib/models/finamp_models.dart @@ -87,6 +87,7 @@ class FinampSettings { required this.tabSortOrder, this.tabOrder = _tabOrder, this.hasCompletedBlurhashImageMigration = true, + this.hasCompletedBlurhashImageMigrationIdFix = true, }); @HiveField(0) @@ -176,6 +177,9 @@ class FinampSettings { @HiveField(23, defaultValue: false) bool hasCompletedBlurhashImageMigration; + @HiveField(24, defaultValue: false) + bool hasCompletedBlurhashImageMigrationIdFix; + static Future create() async { final internalSongDir = await getInternalSongDir(); final downloadLocation = DownloadLocation.create( @@ -216,6 +220,10 @@ class FinampSettings { SortOrder getSortOrder(TabContentType tabType) { return tabSortOrder[tabType] ?? SortOrder.ascending; } + + bool get shouldRunBlurhashImageMigrationIdFix => + hasCompletedBlurhashImageMigration && + !hasCompletedBlurhashImageMigrationIdFix; } /// Custom storage locations for storing music. diff --git a/lib/models/finamp_models.g.dart b/lib/models/finamp_models.g.dart index c7a16ea6b..e8e2fe943 100644 --- a/lib/models/finamp_models.g.dart +++ b/lib/models/finamp_models.g.dart @@ -110,13 +110,15 @@ class FinampSettingsAdapter extends TypeAdapter { : (fields[22] as List).cast(), hasCompletedBlurhashImageMigration: fields[23] == null ? false : fields[23] as bool, + hasCompletedBlurhashImageMigrationIdFix: + fields[24] == null ? false : fields[24] as bool, )..disableGesture = fields[19] == null ? false : fields[19] as bool; } @override void write(BinaryWriter writer, FinampSettings obj) { writer - ..writeByte(24) + ..writeByte(25) ..writeByte(0) ..write(obj.isOffline) ..writeByte(1) @@ -164,7 +166,9 @@ class FinampSettingsAdapter extends TypeAdapter { ..writeByte(22) ..write(obj.tabOrder) ..writeByte(23) - ..write(obj.hasCompletedBlurhashImageMigration); + ..write(obj.hasCompletedBlurhashImageMigration) + ..writeByte(24) + ..write(obj.hasCompletedBlurhashImageMigrationIdFix); } @override diff --git a/lib/services/downloads_helper.dart b/lib/services/downloads_helper.dart index 7ee67d4ef..cdb0edc7e 100644 --- a/lib/services/downloads_helper.dart +++ b/lib/services/downloads_helper.dart @@ -936,6 +936,43 @@ class DownloadsHelper { _downloadsLogger.info("${imagesToDelete.length} duplicate images deleted."); } + Future fixBlurhashMigrationIds() async { + _downloadsLogger.info("Fixing blurhash migration IDs from 0.6.15"); + + final List images = []; + + for (final image in downloadedImages) { + final item = getDownloadedSong(image.requiredBy.first) ?? + getDownloadedParent(image.requiredBy.first); + + if (item == null) { + // I should really use error enums when I rip this whole system out + throw "Failed to get item from image during blurhash migration fix!"; + } + + switch (item.runtimeType) { + case DownloadedSong: + image.id = (item as DownloadedSong).song.blurHash!; + break; + case DownloadedParent: + image.id = (item as DownloadedParent).item.blurHash!; + break; + default: + throw "Item was unexpected type! got ${item.runtimeType}. This really shouldn't happen..."; + } + + images.add(image); + } + + await _downloadedImagesBox.clear(); + await _downloadedImagesBox + .putAll(Map.fromEntries(images.map((e) => MapEntry(e.id, e)))); + + await _downloadedImageIdsBox.clear(); + await _downloadedImageIdsBox.putAll( + Map.fromEntries(images.map((e) => MapEntry(e.downloadId, e.id)))); + } + Iterable get downloadedItems => _downloadedItemsBox.values; Iterable get downloadedImages => _downloadedImagesBox.values; diff --git a/lib/services/finamp_settings_helper.dart b/lib/services/finamp_settings_helper.dart index 6e73e298f..b8722c6f3 100644 --- a/lib/services/finamp_settings_helper.dart +++ b/lib/services/finamp_settings_helper.dart @@ -199,6 +199,15 @@ class FinampSettingsHelper { .put("FinampSettings", finampSettingsTemp); } + static void setHasCompletedBlurhashImageMigrationIdFix( + bool hasCompletedBlurhashImageMigrationIdFix) { + FinampSettings finampSettingsTemp = finampSettings; + finampSettingsTemp.hasCompletedBlurhashImageMigrationIdFix = + hasCompletedBlurhashImageMigrationIdFix; + Hive.box("FinampSettings") + .put("FinampSettings", finampSettingsTemp); + } + static void setTabOrder(int index, TabContentType tabContentType) { FinampSettings finampSettingsTemp = finampSettings; finampSettingsTemp.tabOrder[index] = tabContentType;