Skip to content

Commit

Permalink
Fix requiring song metadata on downloaded collections. Fix offline de…
Browse files Browse the repository at this point in the history
…lete refresh on music screen.
  • Loading branch information
Komodo5197 committed May 18, 2024
1 parent d2cfb04 commit ad5554b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
7 changes: 3 additions & 4 deletions lib/components/MusicScreen/music_screen_tab_view.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'dart:async';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:Finamp/services/finamp_user_helper.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/rendering/sliver.dart';
import 'package:flutter/src/rendering/sliver_grid.dart';
import 'package:flutter/rendering.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
Expand Down Expand Up @@ -182,7 +181,7 @@ class _MusicScreenTabViewState extends State<MusicScreenTabView>
Rect.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
axis: Axis.vertical);
_refreshStream = _isarDownloader.offlineDeletesStream.listen((event) {
_pagingController.refresh();
_refresh();
});
super.initState();
}
Expand Down
42 changes: 27 additions & 15 deletions lib/services/downloads_service_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -920,28 +920,14 @@ class DownloadsSyncService {
//
// Fetch latest metadata from server, if needed or not quicksyncing
//
// If modifying requiredAttributes with another field, the requested fields to be downloaded
// in _getCollectionInfo and _getCollectionChildren must be updated. Additionally,
// DownloadItem.copyWith must be updated to preserve the field, and BaseItemDto.mostlyEqual
// must be updated to check the field when determining equality.
final requiredAttributes = [
parent.baseItem?.sortName,
parent.baseItem?.mediaSources,
parent.baseItem?.mediaStreams
];
// newBaseItem must be calculated before children are determined so that the latest
// metadata can be used, especially imageId and blurhash.
BaseItemDto? newBaseItem;
//If we aren't quicksyncing, fetch the latest BaseItemDto to copy into Isar.
// childCount is expected to change frequently for playlists, so we
// always fetch a fresh copy from the server to check if the metadata
// needs updating, even when quickSyncing.
if (parent.type.requiresItem &&
(!FinampSettingsHelper.finampSettings.preferQuickSyncs ||
_downloadsService.forceFullSync ||
(parent.type == DownloadItemType.collection &&
parent.baseItemType == BaseItemDtoType.playlist) ||
requiredAttributes.any((element) => element == null))) {
_needsMetadataUpdate(parent))) {
newBaseItem =
(await _getCollectionInfo(parent.baseItem!.id, parent.type, true))
?.baseItem;
Expand Down Expand Up @@ -1447,6 +1433,32 @@ class DownloadsSyncService {
return null;
}

/// This returns whether the given item needs its metadata refreshed from the server.
/// If modifying to add another required field, the requested fields to be downloaded
/// in _getCollectionInfo and _getCollectionChildren must be updated. Additionally,
/// DownloadItem.copyWith must be updated to preserve the field, and BaseItemDto.mostlyEqual
/// must be updated to check the field when determining equality.
bool _needsMetadataUpdate(DownloadStub stub) {
assert(stub.type.requiresItem);

// childCount is expected to change frequently for playlists, so we
// always fetch a fresh copy from the server to check if the metadata
// needs updating, even when quickSyncing.
if (stub.type == DownloadItemType.collection &&
stub.baseItemType == BaseItemDtoType.playlist) {
return true;
}
if (stub.baseItem?.sortName == null) {
return true;
}
if (stub.type == DownloadItemType.song &&
(stub.baseItem?.mediaSources == null ||
stub.baseItem?.mediaStreams == null)) {
return true;
}
return false;
}

/// Ensures the given node is downloaded. Called on all required nodes with files
/// by [_syncDownload]. Items enqueued/downloading/failed are validated and cleaned
/// up before re-initiating download if needed.
Expand Down

0 comments on commit ad5554b

Please sign in to comment.