Skip to content

Commit

Permalink
Fix incorrect IDs in blurhash migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jmshrv committed Aug 11, 2023
1 parent 73d35c1 commit 423c5b7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .flutter
Submodule .flutter updated 3305 files
13 changes: 12 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,22 @@ void _setupJellyfinApiData() {

Future<void> _setupDownloadsHelper() async {
GetIt.instance.registerSingleton(DownloadsHelper());
final downloadsHelper = GetIt.instance<DownloadsHelper>();

// 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<DownloadsHelper>().migrateBlurhashImages();
await downloadsHelper.migrateBlurhashImages();
FinampSettingsHelper.setHasCompletedBlurhashImageMigration(true);
}

if (shouldRunBlurhashImageMigrationIdFix) {
await downloadsHelper.fixBlurhashMigrationIds();
FinampSettingsHelper.setHasCompletedBlurhashImageMigrationIdFix(true);
}
}

Future<void> _setupDownloader() async {
Expand Down
8 changes: 8 additions & 0 deletions lib/models/finamp_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class FinampSettings {
required this.tabSortOrder,
this.tabOrder = _tabOrder,
this.hasCompletedBlurhashImageMigration = true,
this.hasCompletedBlurhashImageMigrationIdFix = true,
});

@HiveField(0)
Expand Down Expand Up @@ -176,6 +177,9 @@ class FinampSettings {
@HiveField(23, defaultValue: false)
bool hasCompletedBlurhashImageMigration;

@HiveField(24, defaultValue: false)
bool hasCompletedBlurhashImageMigrationIdFix;

static Future<FinampSettings> create() async {
final internalSongDir = await getInternalSongDir();
final downloadLocation = DownloadLocation.create(
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions lib/models/finamp_models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions lib/services/downloads_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,43 @@ class DownloadsHelper {
_downloadsLogger.info("${imagesToDelete.length} duplicate images deleted.");
}

Future<void> fixBlurhashMigrationIds() async {
_downloadsLogger.info("Fixing blurhash migration IDs from 0.6.15");

final List<DownloadedImage> 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<DownloadedSong> get downloadedItems => _downloadedItemsBox.values;

Iterable<DownloadedImage> get downloadedImages => _downloadedImagesBox.values;
Expand Down
9 changes: 9 additions & 0 deletions lib/services/finamp_settings_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ class FinampSettingsHelper {
.put("FinampSettings", finampSettingsTemp);
}

static void setHasCompletedBlurhashImageMigrationIdFix(
bool hasCompletedBlurhashImageMigrationIdFix) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.hasCompletedBlurhashImageMigrationIdFix =
hasCompletedBlurhashImageMigrationIdFix;
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}

static void setTabOrder(int index, TabContentType tabContentType) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.tabOrder[index] = tabContentType;
Expand Down

0 comments on commit 423c5b7

Please sign in to comment.