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.
This commit is contained in:
zarzet committed 2026-11-17 08:50:36 +07:00
1 parent e18efec8f5
commit cf68a7d943
3 files changed
+119 -2

No files matched your search

+19 -2
View File
@@ -91,10 +91,17 @@ class LocalMediaItemNotifier extends CurrentMediaItemNotifier {
} }
/// A dismissed session stays available on Home without restoring the player. /// A dismissed session stays available on Home without restoring the player.
final playbackResumeProvider = FutureProvider<PlaybackResumePreview?>((ref) { /// With the scanned Library switched off, a session on one of its songs is
/// not offered.
final playbackResumeProvider = FutureProvider<PlaybackResumePreview?>((
ref,
) async {
if (!ref.watch(settingsProvider.select((s) => s.playerMode == 'internal'))) { if (!ref.watch(settingsProvider.select((s) => s.playerMode == 'internal'))) {
return null; return null;
} }
final localLibraryEnabled = ref.watch(
settingsProvider.select((s) => s.localLibraryEnabled),
);
final hasCurrent = ref.watch( final hasCurrent = ref.watch(
currentMediaItemProvider.select((state) => state.value != null), currentMediaItemProvider.select((state) => state.value != null),
); );
@@ -107,7 +114,17 @@ final playbackResumeProvider = FutureProvider<PlaybackResumePreview?>((ref) {
ref.invalidateSelf(); 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. /// [libraryItemForMedia] with the download History the app has loaded.
@@ -2,6 +2,7 @@ import 'dart:io';
import 'package:audio_service/audio_service.dart' show AudioServiceRepeatMode; import 'package:audio_service/audio_service.dart' show AudioServiceRepeatMode;
import 'package:path_provider/path_provider.dart'; 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/cover_store.dart';
import 'package:mornye/services/music_player_runtime.dart'; import 'package:mornye/services/music_player_runtime.dart';
import 'package:mornye/services/playable_media.dart'; import 'package:mornye/services/playable_media.dart';
@@ -63,6 +64,21 @@ Future<PlaybackResumePreview?> 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<PlaybackResumePreview?> resumePreviewForLibrary(
PlaybackResumePreview? preview, {
required bool localLibraryEnabled,
required Future<UnifiedLibraryItem?> 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 /// Resolve a saved queue in bounded groups, retaining its original order even
/// when later file checks finish first. File existence is shared across tracks /// 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. /// with the same artwork for this restore only, with a bounded LRU cache.
+84
View File
@@ -1,6 +1,9 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter_test/flutter_test.dart'; 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/playable_media.dart';
import 'package:mornye/services/playback_session_restore.dart'; import 'package:mornye/services/playback_session_restore.dart';
@@ -268,4 +271,85 @@ void main() {
expect(result.artworkRelocated, isTrue); 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,
);
});
});
} }