From cf68a7d9435ce2b8ed73e11d0d16755c9cdb0723 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:50:36 +0700 Subject: [PATCH] fix(home): do not offer a scanned song to continue while Local Library is off Continue listening showed the saved session whatever it held, so a song on a NAS stayed on Home, and played from the NAS, after Local Library was switched off. The saved session is now left out while it is on a song of the scanned Library and that Library is off, as Recently Played and the mixes already do. Downloads and songs the Library does not know stay. --- lib/providers/music_player_provider.dart | 21 +++++- lib/services/playback_session_restore.dart | 16 +++++ test/playback_session_restore_test.dart | 84 ++++++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/lib/providers/music_player_provider.dart b/lib/providers/music_player_provider.dart index 1cdcba524..627512e7c 100644 --- a/lib/providers/music_player_provider.dart +++ b/lib/providers/music_player_provider.dart @@ -91,10 +91,17 @@ class LocalMediaItemNotifier extends CurrentMediaItemNotifier { } /// A dismissed session stays available on Home without restoring the player. -final playbackResumeProvider = FutureProvider((ref) { +/// With the scanned Library switched off, a session on one of its songs is +/// not offered. +final playbackResumeProvider = FutureProvider(( + ref, +) async { if (!ref.watch(settingsProvider.select((s) => s.playerMode == 'internal'))) { return null; } + final localLibraryEnabled = ref.watch( + settingsProvider.select((s) => s.localLibraryEnabled), + ); final hasCurrent = ref.watch( currentMediaItemProvider.select((state) => state.value != null), ); @@ -107,7 +114,17 @@ final playbackResumeProvider = FutureProvider((ref) { ref.invalidateSelf(); }), ); - return loadPlaybackResumePreview(runtime); + return resumePreviewForLibrary( + await loadPlaybackResumePreview(runtime), + localLibraryEnabled: localLibraryEnabled, + libraryItem: (media) => _libraryItemForMedia( + ref, + id: media.id, + source: media.source, + title: media.title, + artist: media.artist, + ), + ); }); /// [libraryItemForMedia] with the download History the app has loaded. diff --git a/lib/services/playback_session_restore.dart b/lib/services/playback_session_restore.dart index 5d9ef1b6a..98b36b60b 100644 --- a/lib/services/playback_session_restore.dart +++ b/lib/services/playback_session_restore.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:audio_service/audio_service.dart' show AudioServiceRepeatMode; import 'package:path_provider/path_provider.dart'; +import 'package:mornye/models/unified_library_item.dart'; import 'package:mornye/services/cover_store.dart'; import 'package:mornye/services/music_player_runtime.dart'; import 'package:mornye/services/playable_media.dart'; @@ -63,6 +64,21 @@ Future loadPlaybackResumePreview( } } +/// [preview], or null when its song belongs to the scanned Library while +/// that Library is switched off: Home then offers nothing from it, such as +/// a song on a NAS. [libraryItem] finds the song's Library record; a +/// download and a song the Library does not know stay. +Future resumePreviewForLibrary( + PlaybackResumePreview? preview, { + required bool localLibraryEnabled, + required Future Function(PlayableMedia media) + libraryItem, +}) async { + if (preview == null || localLibraryEnabled) return preview; + final item = await libraryItem(preview.media); + return item?.source == LibraryItemSource.local ? null : preview; +} + /// Resolve a saved queue in bounded groups, retaining its original order even /// when later file checks finish first. File existence is shared across tracks /// with the same artwork for this restore only, with a bounded LRU cache. diff --git a/test/playback_session_restore_test.dart b/test/playback_session_restore_test.dart index 8e197046f..0624e592d 100644 --- a/test/playback_session_restore_test.dart +++ b/test/playback_session_restore_test.dart @@ -1,6 +1,9 @@ import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/models/download_history.dart'; +import 'package:mornye/models/library_database_models.dart'; +import 'package:mornye/models/unified_library_item.dart'; import 'package:mornye/services/playable_media.dart'; import 'package:mornye/services/playback_session_restore.dart'; @@ -268,4 +271,85 @@ void main() { expect(result.artworkRelocated, isTrue); }, ); + + group('the session Home offers to continue', () { + const nas = 'network://nas/Album/Song.flac'; + final preview = ( + media: PlayableMedia.fromJson(_media(0, source: nas))!, + position: const Duration(seconds: 30), + ); + final scanned = UnifiedLibraryItem.fromLocalLibrary( + LocalLibraryItem( + id: '0', + trackName: 'Track 0', + artistName: 'Fixture artist', + albumName: 'Album', + filePath: nas, + scannedAt: DateTime(2026, 11, 17), + ), + ); + final downloaded = UnifiedLibraryItem.fromDownloadHistory( + DownloadHistoryItem( + id: '0', + trackName: 'Track 0', + artistName: 'Fixture artist', + albumName: 'Album', + filePath: '/music/0.flac', + service: 'example-provider', + downloadedAt: DateTime(2026, 11, 17), + ), + ); + + test( + 'is hidden for a scanned song while the scanned Library is off', + () async { + expect( + await resumePreviewForLibrary( + preview, + localLibraryEnabled: false, + libraryItem: (media) async { + expect(media.source, nas); + return scanned; + }, + ), + isNull, + ); + }, + ); + + test( + 'stays for a download and for a song the Library does not know', + () async { + for (final item in [downloaded, null]) { + expect( + await resumePreviewForLibrary( + preview, + localLibraryEnabled: false, + libraryItem: (_) async => item, + ), + same(preview), + ); + } + }, + ); + + test('stays for a scanned song while the scanned Library is on', () async { + expect( + await resumePreviewForLibrary( + preview, + localLibraryEnabled: true, + libraryItem: (_) => throw StateError('No lookup is needed'), + ), + same(preview), + ); + expect( + await resumePreviewForLibrary( + null, + localLibraryEnabled: false, + libraryItem: (_) => throw StateError('No lookup is needed'), + ), + isNull, + ); + }); + }); }