diff --git a/lib/providers/library_counts_provider.dart b/lib/providers/library_counts_provider.dart new file mode 100644 index 000000000..d9f6f132f --- /dev/null +++ b/lib/providers/library_counts_provider.dart @@ -0,0 +1,84 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:mornye/providers/download_history_provider.dart'; +import 'package:mornye/providers/local_library_provider.dart'; +import 'package:mornye/services/library_database.dart'; + +/// The filters a Library count is taken under. Equal requests share one +/// count, so the Library badges, Home and the Home mixes read the unfiltered +/// Library once per change. +class LibraryCountsRequest { + final String searchQuery; + final String? filterSource; + final String? filterQuality; + final String? filterFormat; + final String? filterMetadata; + final bool localLibraryEnabled; + + const LibraryCountsRequest({ + required this.searchQuery, + required this.filterSource, + required this.filterQuality, + required this.filterFormat, + required this.filterMetadata, + required this.localLibraryEnabled, + }); + + /// The whole Library, as the unfiltered Library tab counts it. + const LibraryCountsRequest.all({required this.localLibraryEnabled}) + : searchQuery = '', + filterSource = null, + filterQuality = null, + filterFormat = null, + filterMetadata = null; + + QueueLibraryDbQuery toDbQuery() => QueueLibraryDbQuery( + searchQuery: searchQuery, + source: filterSource, + quality: filterQuality, + format: filterFormat, + metadata: filterMetadata, + includeLocal: localLibraryEnabled, + ); + + bool get allowsInMemoryHistoryFallback => + searchQuery.trim().isEmpty && + filterSource != 'local' && + filterQuality == null && + filterFormat == null && + filterMetadata == null; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is LibraryCountsRequest && + searchQuery == other.searchQuery && + filterSource == other.filterSource && + filterQuality == other.filterQuality && + filterFormat == other.filterFormat && + filterMetadata == other.filterMetadata && + localLibraryEnabled == other.localLibraryEnabled; + + @override + int get hashCode => Object.hash( + searchQuery, + filterSource, + filterQuality, + filterFormat, + filterMetadata, + localLibraryEnabled, + ); +} + +/// Track, album and single counts, taken again when downloads or the scanned +/// Library change. The scanned side follows the index revision, so a count +/// does not load the Library index ahead of its startup warmup. +final libraryCountsProvider = FutureProvider.autoDispose + .family((ref, request) async { + ref.watch( + downloadHistoryProvider.select((state) => state.loadedIndexVersion), + ); + if (request.localLibraryEnabled) { + ref.watch(localLibraryIndexRevisionProvider); + } + return LibraryDatabase.instance.getQueueCounts(request.toDbQuery()); + }); diff --git a/lib/providers/library_mixes_provider.dart b/lib/providers/library_mixes_provider.dart index eb3e7f951..e49b835ce 100644 --- a/lib/providers/library_mixes_provider.dart +++ b/lib/providers/library_mixes_provider.dart @@ -1,12 +1,14 @@ import 'dart:async'; import 'dart:convert'; -import 'package:flutter/foundation.dart' show compute, visibleForTesting; +import 'package:flutter/foundation.dart' + show compute, mapEquals, visibleForTesting; import 'package:flutter/scheduler.dart' show SchedulerBinding; import 'package:flutter/widgets.dart' show WidgetsBinding; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:mornye/models/unified_library_item.dart'; import 'package:mornye/providers/download_history_provider.dart'; +import 'package:mornye/providers/library_counts_provider.dart'; import 'package:mornye/providers/local_library_provider.dart'; import 'package:mornye/providers/music_player_provider.dart'; import 'package:mornye/providers/recent_access_provider.dart' @@ -148,14 +150,21 @@ final libraryMixesProvider = FutureProvider>((ref) async { } // A download or a rescan may change what the mixes can hold. The Local // Library is only read once startup has settled, so it keeps its own - // deferred warmup. - ref.watch(downloadHistoryProvider.select((s) => s.items.length)); + // deferred warmup. Downloads in a batch arrive coalesced, at most one + // revision a second. + ref.watch(downloadHistoryProvider.select((s) => s.loadedIndexVersion)); if (includeLocal) { ref.watch(localLibraryProvider.select((s) => s.loadedIndexVersion)); if (ref.watch(localLibraryProvider.select((s) => s.isScanning))) { return _cachedViews(includeLocal); } } + // The Library tab and Home count the same Library; one count serves all. + final counts = ref.watch( + libraryCountsProvider( + LibraryCountsRequest.all(localLibraryEnabled: includeLocal), + ).future, + ); final now = DateTime.now(); // New mixes at midnight while Home stays open. final midnight = Timer( @@ -169,6 +178,7 @@ final libraryMixesProvider = FutureProvider>((ref) async { includeLocal: includeLocal, now: now, runtime: ref.read(musicPlayerRuntimeProvider), + count: (await counts).allTrackCount, ); return await _resolve(mixes, includeLocal); } on Object catch (error, stack) { @@ -211,15 +221,21 @@ class _LibraryMixes { required bool includeLocal, required DateTime now, required MusicPlayerRuntime runtime, + required int count, }) async { final pending = _building; if (pending != null) { if (_buildingIncludeLocal == includeLocal) return pending; // Local Library was switched: the next load starts after this one. await pending.then((_) {}, onError: (_) {}); - return load(includeLocal: includeLocal, now: now, runtime: runtime); + return load( + includeLocal: includeLocal, + now: now, + runtime: runtime, + count: count, + ); } - final operation = _load(includeLocal, now, runtime); + final operation = _load(includeLocal, now, runtime, count); _building = operation; _buildingIncludeLocal = includeLocal; try { @@ -233,11 +249,9 @@ class _LibraryMixes { bool includeLocal, DateTime now, MusicPlayerRuntime runtime, + int count, ) async { final prefs = await SharedPreferences.getInstance(); - final count = (await LibraryDatabase.instance.getQueueCounts( - QueueLibraryDbQuery(includeLocal: includeLocal), - )).allTrackCount; final day = listeningDay(now); final cached = _decode(prefs.getString(_cacheKey)); if (cached != null && @@ -267,9 +281,24 @@ class _LibraryMixes { return mixes; } + /// The cache as last decoded, so an unchanged cache is not decoded again + /// and keeps the same mixes. + static String? _decodedValue; + static ({String day, bool includeLocal, int count, List mixes})? + _decoded; + static ({String day, bool includeLocal, int count, List mixes})? _decode(String? value) { if (value == null) return null; + if (value == _decodedValue) return _decoded; + final decoded = _decodeJson(value); + _decodedValue = value; + _decoded = decoded; + return decoded; + } + + static ({String day, bool includeLocal, int count, List mixes})? + _decodeJson(String value) { try { final json = jsonDecode(value); if (json is! Map) return null; @@ -349,35 +378,38 @@ class _LibraryMixes { final songs = []; final years = {}; final added = {}; - for (var offset = 0; ; offset += _pageSize) { - final rows = await LibraryDatabase.instance.getMixSongPage( - includeLocal: includeLocal, - limit: _pageSize, - offset: offset, - ); - for (final row in rows) { - final seconds = readPositiveInt(row['duration']); - final id = '${row['source'] == 'local' ? 'local' : 'dl'}_${row['id']}'; - if (libraryMixReleaseYear(row['release_date']) case final year?) { - years[id] = year; - } - if (libraryMixAddedAt(row) case final at?) added[id] = at; - songs.add( - PlayableMedia( - id: id, - source: row['file_path'] as String? ?? '', - title: row['track_name'] as String? ?? '', - artist: row['artist_name'] as String? ?? '', - album: row['album_name'] as String? ?? '', - genre: row['genre'] as String?, - duration: seconds == null ? null : Duration(seconds: seconds), - ), + for (final source in ['downloaded', if (includeLocal) 'local']) { + String? afterId; + while (true) { + final rows = await LibraryDatabase.instance.getMixSongPage( + source: source, + afterId: afterId, + limit: _pageSize, ); - } - if (rows.length < _pageSize) { - return (songs: songs, years: years, added: added); + for (final row in rows) { + final seconds = readPositiveInt(row['duration']); + final id = '${source == 'local' ? 'local' : 'dl'}_${row['id']}'; + if (libraryMixReleaseYear(row['release_date']) case final year?) { + years[id] = year; + } + if (libraryMixAddedAt(row) case final at?) added[id] = at; + songs.add( + PlayableMedia( + id: id, + source: row['file_path'] as String? ?? '', + title: row['track_name'] as String? ?? '', + artist: row['artist_name'] as String? ?? '', + album: row['album_name'] as String? ?? '', + genre: row['genre'] as String?, + duration: seconds == null ? null : Duration(seconds: seconds), + ), + ); + } + afterId = rows.lastOrNull?['id'] as String?; + if (rows.length < _pageSize || afterId == null) break; } } + return (songs: songs, years: years, added: added); } /// Loved songs, Autoplay's learning log while learning is on, the saved @@ -513,6 +545,16 @@ Future> _resolve( HistoryDatabase.instance.getByIds(downloaded), LibraryDatabase.instance.getByIds(local.toList()), ).wait; + // Most Library changes leave the mixes' songs as they were: keep the + // views, so Home neither decodes them again nor rebuilds the shelf. + final last = _lastResolved; + if (last != null && + identical(last.mixes, mixes) && + last.includeLocal == includeLocal && + _sameRowsById(last.history, history) && + _sameRows(last.scanned, scanned)) { + return last.views; + } final items = { for (final MapEntry(:key, :value) in history.entries) 'dl_$key': UnifiedLibraryItem.fromDownloadHistory( @@ -523,10 +565,46 @@ Future> _resolve( LocalLibraryItem.fromJson(row), ), }; - return [ + final views = [ for (final mix in mixes) if ([for (final id in mix.songIds) ?items[id]] case final songs when songs.length >= _minVisibleSongs) LibraryMixView(mix: mix, items: songs), ]; + _lastResolved = ( + mixes: mixes, + includeLocal: includeLocal, + history: history, + scanned: scanned, + views: views, + ); + return views; } + +/// The last [_resolve] and the rows it read. +({ + List mixes, + bool includeLocal, + Map> history, + List> scanned, + List views, +})? +_lastResolved; + +bool _sameRows( + List> first, + List> second, +) { + if (first.length != second.length) return false; + for (var index = 0; index < first.length; index++) { + if (!mapEquals(first[index], second[index])) return false; + } + return true; +} + +bool _sameRowsById( + Map> first, + Map> second, +) => + first.length == second.length && + first.entries.every((entry) => mapEquals(entry.value, second[entry.key])); diff --git a/lib/providers/local_library_provider.dart b/lib/providers/local_library_provider.dart index fdfece1a9..0d1cd4e9e 100644 --- a/lib/providers/local_library_provider.dart +++ b/lib/providers/local_library_provider.dart @@ -334,6 +334,7 @@ class LocalLibraryNotifier extends Notifier { trackKeySet: lookupIndex.matchKeys, isrcSet: lookupIndex.isrcs, ); + ref.read(localLibraryIndexRevisionProvider.notifier)._bump(); _log.i( 'Loaded local library summary: $count items, lastScannedAt: ' '$lastScannedAt, excludedDownloadedCount: $excludedDownloadedCount', @@ -764,3 +765,18 @@ final localLibraryProvider = NotifierProvider( LocalLibraryNotifier.new, ); + +/// Moves with [LocalLibraryState.loadedIndexVersion] without creating the +/// Library notifier, so a view can refresh when the index changes and leave +/// loading it to the startup warmup. +final localLibraryIndexRevisionProvider = + NotifierProvider( + LocalLibraryIndexRevision.new, + ); + +class LocalLibraryIndexRevision extends Notifier { + @override + int build() => 0; + + void _bump() => state++; +} diff --git a/lib/providers/local_library_provider_sources.dart b/lib/providers/local_library_provider_sources.dart index dc81a8743..b014716e4 100644 --- a/lib/providers/local_library_provider_sources.dart +++ b/lib/providers/local_library_provider_sources.dart @@ -116,6 +116,7 @@ extension _LocalLibrarySources on LocalLibraryNotifier { trackKeySet: index.matchKeys, isrcSet: index.isrcs, ); + ref.read(localLibraryIndexRevisionProvider.notifier)._bump(); _hasLoadedFromDatabase = true; _isLoaded = true; } diff --git a/lib/screens/home_tab.dart b/lib/screens/home_tab.dart index a82870263..be55503f6 100644 --- a/lib/screens/home_tab.dart +++ b/lib/screens/home_tab.dart @@ -46,6 +46,7 @@ import 'package:mornye/providers/explore_provider.dart'; import 'package:mornye/providers/user_profile_provider.dart'; import 'package:mornye/widgets/desktop/desktop_scroll_arrows.dart'; import 'package:mornye/widgets/explore_featured_section.dart'; +import 'package:mornye/providers/library_counts_provider.dart'; import 'package:mornye/providers/library_mixes_provider.dart'; import 'package:mornye/providers/local_library_provider.dart'; import 'package:mornye/providers/music_player_provider.dart'; diff --git a/lib/screens/home_tab_intro.dart b/lib/screens/home_tab_intro.dart index a5b1036be..07d874a8c 100644 --- a/lib/screens/home_tab_intro.dart +++ b/lib/screens/home_tab_intro.dart @@ -4,105 +4,276 @@ const _homeShelfLimit = 10; const _homeShelfCoverSize = 120.0; const _homeMixCoverSize = 152.0; -/// The newest downloaded and scanned tracks, with the Library's track count -/// for the shortcut at the end of the shelf. -final _homeRecentlyAddedProvider = - FutureProvider.autoDispose< - ({List items, int trackCount}) - >((ref) async { - final includeLocal = ref.watch( - settingsProvider.select((s) => s.localLibraryEnabled), - ); - // Reload when a download lands or a scan rebuilds the index. - ref.watch(downloadHistoryProvider.select((s) => s.items.length)); - ref.watch(localLibraryProvider.select((s) => s.loadedIndexVersion)); - final database = LibraryDatabase.instance; - final rows = await database.getQueueTrackPage( - QueueLibraryDbQuery(includeLocal: includeLocal, limit: _homeShelfLimit), - ); - final counts = await database.getQueueCounts( - QueueLibraryDbQuery(includeLocal: includeLocal), - ); - final items = []; - for (final row in rows) { - final json = Map.from(row['item'] as Map); - switch (row['source']) { - case 'local': - items.add( - UnifiedLibraryItem.fromLocalLibrary( - LocalLibraryItem.fromJson(json), - ), - ); - case 'downloaded': - items.add( - UnifiedLibraryItem.fromDownloadHistory( - DownloadHistoryItem.fromJson(json), - ), - ); - } - } - return (items: items, trackCount: counts.allTrackCount); - }); +typedef HomeRecentlyAdded = ({List items, int trackCount}); -typedef _HomeRepeatTrack = ({ListeningTrack track, UnifiedLibraryItem? item}); +/// The newest downloaded and scanned tracks, with the Library's track count +/// for the shortcut at the end of the shelf. A reload that finds the same +/// songs keeps the same list, so Home does not rebuild for it. +final homeRecentlyAddedProvider = FutureProvider.autoDispose( + (ref) async { + final includeLocal = ref.watch( + settingsProvider.select((s) => s.localLibraryEnabled), + ); + // Reload when a download lands or a scan rebuilds the index. Downloads + // in a batch arrive coalesced, at most one revision a second. + ref.watch(downloadHistoryProvider.select((s) => s.loadedIndexVersion)); + if (includeLocal) ref.watch(localLibraryIndexRevisionProvider); + final memo = ref.read(_homeShelfMemoProvider); + final (rows, counts) = await ( + LibraryDatabase.instance.getQueueTrackPage( + QueueLibraryDbQuery(includeLocal: includeLocal, limit: _homeShelfLimit), + ), + // The Library tab's badges count the same Library; one count serves + // both. + ref.watch( + libraryCountsProvider( + LibraryCountsRequest.all(localLibraryEnabled: includeLocal), + ).future, + ), + ).wait; + final last = memo.recentlyAdded; + if (last != null && _sameQueueRows(last.rows, rows)) { + return (items: last.items, trackCount: counts.allTrackCount); + } + final items = []; + for (final row in rows) { + final json = Map.from(row['item'] as Map); + switch (row['source']) { + case 'local': + items.add( + UnifiedLibraryItem.fromLocalLibrary( + LocalLibraryItem.fromJson(json), + ), + ); + case 'downloaded': + items.add( + UnifiedLibraryItem.fromDownloadHistory( + DownloadHistoryItem.fromJson(json), + ), + ); + } + } + memo.recentlyAdded = (rows: rows, items: items); + return (items: items, trackCount: counts.allTrackCount); + }, +); + +bool _sameQueueRows( + List> first, + List> second, +) { + if (first.length != second.length) return false; + for (var index = 0; index < first.length; index++) { + final (a, b) = (first[index], second[index]); + if (a['source'] != b['source'] || + !mapEquals(a['item'] as Map?, b['item'] as Map?)) { + return false; + } + } + return true; +} + +typedef HomeRepeatTrack = ({ListeningTrack track, UnifiedLibraryItem? item}); + +/// Candidates read from the statistics at a time; most shelves fill from +/// the first page. +const _homeRepeatPage = 40; /// The most played files of the last four weeks, matched to their current /// Library entry. Statistics keep the artwork seen at play time, which may /// since have been evicted from a cache or rescanned away. -final _homeOnRepeatProvider = - FutureProvider.autoDispose>((ref) async { - // A new track means the previous one may have just earned a play. - ref.watch(currentMediaItemProvider.select((s) => s.value?.id)); - // A deleted download or a rescan can remove a track from the shelf. - ref.watch(downloadHistoryProvider.select((s) => s.items.length)); - ref.watch(localLibraryProvider.select((s) => s.loadedIndexVersion)); - final includeLocal = ref.watch( - settingsProvider.select((s) => s.localLibraryEnabled), +final homeOnRepeatProvider = FutureProvider.autoDispose>(( + ref, +) async { + // A track that ends may have just earned a play. One that starts after + // none, such as a restored session's, follows no play. + ref.listen(currentMediaItemProvider.select((s) => s.value?.id), ( + previous, + _, + ) { + if (previous != null) ref.invalidateSelf(); + }); + // A deleted download or a rescan can remove a track from the shelf. + final historyVersion = ref.watch( + downloadHistoryProvider.select((s) => s.loadedIndexVersion), + ); + final localRevision = ref.watch(localLibraryIndexRevisionProvider); + final includeLocal = ref.watch( + settingsProvider.select((s) => s.localLibraryEnabled), + ); + final history = ref.read(downloadHistoryProvider.notifier); + final memo = ref.read(_homeShelfMemoProvider); + // Library matches hold until the Library changes, so a new track only + // reads the statistics again. + final library = (historyVersion, localRevision, includeLocal); + if (memo.repeatLibrary != library) { + // A new map, so a load still running for the old Library keeps its own. + memo + ..repeatLibrary = library + ..repeatMatches = {}; + } + final matches = memo.repeatMatches; + await listeningRecorder.flush(); + final now = DateTime.now(); + final start = DateTime(now.year, now.month, now.day - 27); + final end = DateTime(now.year, now.month, now.day + 1); + final tracks = []; + for (var offset = 0; ; offset += _homeRepeatPage) { + // Played files and network songs only: remote sources cannot be + // replayed from their recorded key. + final totals = await ListeningStatisticsStore.instance.loadReplayableTracks( + start, + end, + limit: _homeRepeatPage, + offset: offset, + ); + // Matched in rounds of the songs still missing, so no more songs are + // looked up than one at a time would. + var next = 0; + while (tracks.length < _homeShelfLimit && next < totals.length) { + final round = totals + .skip(next) + .take(_homeShelfLimit - tracks.length) + .toList(); + next += round.length; + tracks.addAll( + await _homeRepeatTracks( + round, + history: history, + includeLocal: includeLocal, + matches: matches, + ), ); - await listeningRecorder.flush(); - final now = DateTime.now(); - final summary = await ListeningStatisticsStore.instance.load( - DateTime(now.year, now.month, now.day - 27), - DateTime(now.year, now.month, now.day + 1), - ); - final history = ref.read(downloadHistoryProvider.notifier); - final database = LibraryDatabase.instance; - final tracks = <_HomeRepeatTrack>[]; - for (final total in summary.tracks) { - if (tracks.length == _homeShelfLimit) break; - final key = total.track.key; - // Remote sources cannot be replayed from their recorded key. - if (total.plays <= 0 || - key.startsWith('http://') || - key.startsWith('https://')) { - continue; - } - final downloaded = await history.getByFilePathAsync(key); - if (downloaded != null) { - tracks.add(( - track: total.track, - item: UnifiedLibraryItem.fromDownloadHistory(downloaded), - )); - continue; - } - final ids = await database.getPhysicalFileIds([key]); - // With Local Library off its songs stay off Home as well. - if (ids.isNotEmpty && !includeLocal) continue; - final row = ids.isEmpty ? null : await database.getById(ids.first); - if (row != null) { - tracks.add(( - track: total.track, - item: UnifiedLibraryItem.fromLocalLibrary( - LocalLibraryItem.fromJson(row), - ), - )); - continue; - } - // Files played from outside the Library stay while they still exist. - if (await fileExists(key)) tracks.add((track: total.track, item: null)); + } + if (tracks.length == _homeShelfLimit || totals.length < _homeRepeatPage) { + break; + } + } + final last = memo.onRepeat; + if (last != null && _sameRepeatTracks(last, tracks)) return last; + return memo.onRepeat = tracks; +}); + +/// A played key's Library entry: a download or a scanned file, or none. +/// [hidden] is a scanned file while Local Library is off. +typedef _HomeRepeatMatch = ({UnifiedLibraryItem? item, bool hidden}); + +/// [totals] that can still play, in order, matched to their Library entry +/// as a download first, then as a scanned file. All lookups of a round run +/// together; [matches] keeps them while the Library stays the same. +Future> _homeRepeatTracks( + List totals, { + required DownloadHistoryNotifier history, + required bool includeLocal, + required Map matches, +}) async { + final missing = [ + for (final total in totals) + if (!matches.containsKey(total.track.key)) total.track.key, + ]; + if (missing.isNotEmpty) { + final database = LibraryDatabase.instance; + final downloads = await Future.wait([ + for (final key in missing) history.getByFilePathAsync(key), + ]); + final scannedKeys = [ + for (final (index, key) in missing.indexed) + if (downloads[index] == null) key, + ]; + final scannedIds = await Future.wait([ + for (final key in scannedKeys) database.getPhysicalFileIds([key]), + ]); + // With Local Library off its songs stay off Home as well. + final rows = includeLocal + ? { + for (final row in await database.getByIds([ + for (final ids in scannedIds) + if (ids.isNotEmpty) ids.first, + ])) + row['id']: row, + } + : const >{}; + for (final (index, key) in missing.indexed) { + if (downloads[index] case final downloaded?) { + matches[key] = ( + item: UnifiedLibraryItem.fromDownloadHistory(downloaded), + hidden: false, + ); } - return tracks; - }); + } + for (final (index, key) in scannedKeys.indexed) { + final ids = scannedIds[index]; + final row = ids.isEmpty ? null : rows[ids.first]; + matches[key] = ( + item: row == null + ? null + : UnifiedLibraryItem.fromLocalLibrary( + LocalLibraryItem.fromJson(row), + ), + hidden: ids.isNotEmpty && !includeLocal, + ); + } + } + // Files played from outside the Library stay while they still exist, + // checked each time since they can go at any moment. + final outside = [ + for (final total in totals) + if (matches[total.track.key] case (item: null, hidden: false)) + total.track.key, + ]; + final present = await Future.wait(outside.map(fileExists)); + final exists = { + for (final (index, key) in outside.indexed) key: present[index], + }; + return [ + for (final total in totals) + if (matches[total.track.key]! case ( + :final item, + :final hidden, + ) when !hidden && (item != null || exists[total.track.key]!)) + (track: total.track, item: item), + ]; +} + +bool _sameRepeatTracks( + List first, + List second, +) { + if (first.length != second.length) return false; + for (var index = 0; index < first.length; index++) { + final (a, b) = (first[index], second[index]); + if (a.track.key != b.track.key || + a.track.title != b.track.title || + a.track.artist != b.track.artist || + a.track.album != b.track.album || + a.track.artwork != b.track.artwork || + !_sameLibraryItem(a.item, b.item)) { + return false; + } + } + return true; +} + +bool _sameLibraryItem(UnifiedLibraryItem? first, UnifiedLibraryItem? second) => + identical(first, second) || + first != null && + second != null && + mapEquals(first.historyItem?.toJson(), second.historyItem?.toJson()) && + mapEquals(first.localItem?.toJson(), second.localItem?.toJson()); + +/// What the Home shelves read last, kept for the app's lifetime, so a reload +/// that finds the same songs returns the same value. +class _HomeShelfMemo { + ({List> rows, List items})? + recentlyAdded; + (int, int, bool)? repeatLibrary; + Map repeatMatches = {}; + List? onRepeat; +} + +final _homeShelfMemoProvider = Provider<_HomeShelfMemo>( + (ref) => _HomeShelfMemo(), +); /// The prompt under the Home greeting, picked again on each launch. class _HomeTagline extends StatelessWidget { @@ -134,11 +305,17 @@ class _HomeSections extends ConsumerWidget { final hasMediaItem = showPlayer && ref.watch(currentMediaItemProvider.select((s) => s.value != null)); + // Only values: a reload's loading state shows the previous one. final hasResume = - showPlayer && ref.watch(playbackResumeProvider).value != null; - final recentlyAdded = ref.watch(_homeRecentlyAddedProvider).value; - final onRepeat = ref.watch(_homeOnRepeatProvider).value ?? const []; - final mixes = ref.watch(libraryMixesProvider).value ?? const []; + showPlayer && + ref.watch(playbackResumeProvider.select((s) => s.value != null)); + final recentlyAdded = ref.watch( + homeRecentlyAddedProvider.select((s) => s.value), + ); + final onRepeat = + ref.watch(homeOnRepeatProvider.select((s) => s.value)) ?? const []; + final mixes = + ref.watch(libraryMixesProvider.select((s) => s.value)) ?? const []; final sections = [ if (hasMediaItem || hasResume) @@ -431,13 +608,18 @@ class _HomeContinueListeningProgress extends ConsumerWidget { final progress = durationMs > 0 ? (positionMs / durationMs).clamp(0.0, 1.0) : 0.0; - return ClipRRect( - borderRadius: BorderRadius.circular(2), - child: LinearProgressIndicator( - value: progress, - minHeight: 4, - color: colorScheme.primary, - backgroundColor: colorScheme.onPrimaryContainer.withValues(alpha: 0.16), + // Repaints on every position tick without repainting the sections. + return RepaintBoundary( + child: ClipRRect( + borderRadius: BorderRadius.circular(2), + child: LinearProgressIndicator( + value: progress, + minHeight: 4, + color: colorScheme.primary, + backgroundColor: colorScheme.onPrimaryContainer.withValues( + alpha: 0.16, + ), + ), ), ); } @@ -696,7 +878,7 @@ class _HomeLibraryTile extends StatelessWidget { class _HomeOnRepeatShelf extends ConsumerWidget { const _HomeOnRepeatShelf({required this.tracks, required this.inset}); - final List<_HomeRepeatTrack> tracks; + final List tracks; final double inset; @override diff --git a/lib/screens/queue_tab.dart b/lib/screens/queue_tab.dart index df456b436..a0c700631 100644 --- a/lib/screens/queue_tab.dart +++ b/lib/screens/queue_tab.dart @@ -45,6 +45,7 @@ import 'package:material_3_expressive/material_3_expressive.dart' M3EButtonSize, M3EButtonStyle; import 'package:mornye/providers/library_browse_provider.dart'; +import 'package:mornye/providers/library_counts_provider.dart'; import 'package:mornye/services/platform_bridge.dart'; import 'package:mornye/widgets/app_shape.dart'; import 'package:mornye/widgets/library_fast_scroller.dart'; @@ -238,7 +239,7 @@ class _QueueTabState extends ConsumerState { final Map _songTableByMode = {}; final QueueLibraryPager< - _QueueLibraryCountsRequest, + LibraryCountsRequest, _QueueLibraryPageRequest, _QueueLibraryPageData > @@ -644,7 +645,7 @@ class _QueueTabState extends ConsumerState { settingsProvider.select((s) => s.libraryQualityLabelMode), ); final colorScheme = Theme.of(context).colorScheme; - final countsRequest = _QueueLibraryCountsRequest( + final countsRequest = LibraryCountsRequest( searchQuery: _searchQuery, filterSource: _filterSource, filterQuality: _filterQuality, @@ -665,7 +666,7 @@ class _QueueTabState extends ConsumerState { singleTrackCount: 0, ), ) - : ref.watch(_queueLibraryCountsProvider(countsRequest)); + : ref.watch(libraryCountsProvider(countsRequest)); if (!identical(_snapshotHistory, inMemoryHistoryItems)) { _snapshotHistory = inMemoryHistoryItems; _historySnapshots.clear(); diff --git a/lib/screens/queue_tab_helpers.dart b/lib/screens/queue_tab_helpers.dart index 6380b1e74..63cb042c9 100644 --- a/lib/screens/queue_tab_helpers.dart +++ b/lib/screens/queue_tab_helpers.dart @@ -182,61 +182,6 @@ class _QueueLibraryPageRequest ); } -class _QueueLibraryCountsRequest { - final String searchQuery; - final String? filterSource; - final String? filterQuality; - final String? filterFormat; - final String? filterMetadata; - final bool localLibraryEnabled; - - const _QueueLibraryCountsRequest({ - required this.searchQuery, - required this.filterSource, - required this.filterQuality, - required this.filterFormat, - required this.filterMetadata, - required this.localLibraryEnabled, - }); - - QueueLibraryDbQuery toDbQuery() => QueueLibraryDbQuery( - searchQuery: searchQuery, - source: filterSource, - quality: filterQuality, - format: filterFormat, - metadata: filterMetadata, - includeLocal: localLibraryEnabled, - ); - - bool get allowsInMemoryHistoryFallback => - searchQuery.trim().isEmpty && - filterSource != 'local' && - filterQuality == null && - filterFormat == null && - filterMetadata == null; - - @override - bool operator ==(Object other) => - identical(this, other) || - other is _QueueLibraryCountsRequest && - searchQuery == other.searchQuery && - filterSource == other.filterSource && - filterQuality == other.filterQuality && - filterFormat == other.filterFormat && - filterMetadata == other.filterMetadata && - localLibraryEnabled == other.localLibraryEnabled; - - @override - int get hashCode => Object.hash( - searchQuery, - filterSource, - filterQuality, - filterFormat, - filterMetadata, - localLibraryEnabled, - ); -} - class _QueueLibraryPageData implements QueueLibraryPageContent { final List items; final List historyItems; @@ -496,19 +441,3 @@ final _queueLibraryPageProvider = FutureProvider.autoDispose nextCursor: page.nextCursor, ); }); - -final _queueLibraryCountsProvider = FutureProvider.autoDispose - .family(( - ref, - request, - ) async { - ref.watch( - downloadHistoryProvider.select((state) => state.loadedIndexVersion), - ); - if (request.localLibraryEnabled) { - ref.watch( - localLibraryProvider.select((state) => state.loadedIndexVersion), - ); - } - return LibraryDatabase.instance.getQueueCounts(request.toDbQuery()); - }); diff --git a/lib/services/library_database.dart b/lib/services/library_database.dart index 9f341ab67..eedec84aa 100644 --- a/lib/services/library_database.dart +++ b/lib/services/library_database.dart @@ -487,19 +487,15 @@ class LibraryDatabase { Future>> getSongFiles() => _queueRead((store) => store.songFiles(), includeLocal: true); - /// One page of every downloaded and scanned song's metadata, for the - /// Home mixes. + /// One page of the downloaded ('downloaded') or scanned ('local') songs' + /// metadata by id after [afterId], for the Home mixes. Future>> getMixSongPage({ - required bool includeLocal, + required String source, + String? afterId, required int limit, - required int offset, }) => _queueRead( - (store) => store.mixSongs( - includeLocal: includeLocal, - limit: limit, - offset: offset, - ), - includeLocal: includeLocal, + (store) => store.mixSongs(source: source, afterId: afterId, limit: limit), + includeLocal: source == 'local', ); @visibleForTesting diff --git a/lib/services/library_queue_store.dart b/lib/services/library_queue_store.dart index d37c9cb03..e17bda4ea 100644 --- a/lib/services/library_queue_store.dart +++ b/lib/services/library_queue_store.dart @@ -150,33 +150,39 @@ class LibraryQueueStore { /// One page of the metadata Home mixes group songs by, over the same set /// as [directoryEntries]: only the columns ranking, decades and the New - /// Additions mix read, so a large Library arrives in small rows. + /// Additions mix read, so a large Library arrives in small rows. Pages of + /// [source] ('downloaded' or 'local') run by id after [afterId], so each + /// seeks its primary key; downloads, then scanned files, each by id, is + /// the order of the whole set. Future>> mixSongs({ - required bool includeLocal, + required String source, + String? afterId, required int limit, - required int offset, - }) => _db.rawQuery( - ''' - SELECT * FROM ( - SELECT 'downloaded' AS source, h.id, h.file_path, h.track_name, - h.artist_name, h.album_name, h.genre, h.duration, h.release_date, - h.downloaded_at AS added_at, NULL AS file_mod_time - FROM history_db.history h - WHERE COALESCE(h.file_path, '') != '' - ${includeLocal ? ''' - UNION ALL - SELECT 'local' AS source, l.id, l.file_path, l.track_name, - l.artist_name, l.album_name, l.genre, l.duration, l.release_date, - l.scanned_at AS added_at, l.file_mod_time - FROM ${LibrarySchema.visibleView} l - WHERE COALESCE(l.file_path, '') != '' AND ${_notShadowedByHistory('l')} - ''' : ''} - ) - ORDER BY source, id - LIMIT ? OFFSET ? - ''', - [limit, offset], - ); + }) { + final after = afterId == null ? '' : 'AND song.id > ?'; + final sql = switch (source) { + 'downloaded' => + ''' + SELECT 'downloaded' AS source, song.id, song.file_path, + song.track_name, song.artist_name, song.album_name, song.genre, + song.duration, song.release_date, song.downloaded_at AS added_at, + NULL AS file_mod_time + FROM history_db.history song + WHERE COALESCE(song.file_path, '') != '' $after + ''', + 'local' => + ''' + SELECT 'local' AS source, song.id, song.file_path, song.track_name, + song.artist_name, song.album_name, song.genre, song.duration, + song.release_date, song.scanned_at AS added_at, song.file_mod_time + FROM ${LibrarySchema.visibleView} song + WHERE COALESCE(song.file_path, '') != '' + AND ${_notShadowedByHistory('song')} $after + ''', + _ => throw ArgumentError.value(source, 'source'), + }; + return _db.rawQuery('$sql ORDER BY song.id LIMIT ?', [?afterId, limit]); + } Future counts(QueueLibraryDbQuery request) async { final fastCounts = await _getUnfilteredQueueCounts(request); diff --git a/lib/services/listening_statistics.dart b/lib/services/listening_statistics.dart index 73d457d59..4392c548e 100644 --- a/lib/services/listening_statistics.dart +++ b/lib/services/listening_statistics.dart @@ -346,6 +346,19 @@ class ListeningStatisticsStore { return readListeningSummary(db, start, end); } + Future> loadReplayableTracks( + DateTime start, + DateTime end, { + required int limit, + int offset = 0, + }) async => readReplayableListeningTracks( + await _open(), + start, + end, + limit: limit, + offset: offset, + ); + /// Runs a read-only aggregate against the statistics database. Future read(Future Function(DatabaseExecutor db) query) async => query(await _open()); @@ -402,6 +415,31 @@ Future writeListeningDeltas( }); } +/// One row per song of the window, most listened first. A song shows its +/// latest day's details, and the latest cover recorded in the window when +/// that day has none. +String _listeningTracksSql({ + String where = '', + String having = '', + String tail = '', +}) => + ''' + SELECT totals.track_key, details.title, details.artist, details.album, + COALESCE(details.artwork, covered.artwork) AS artwork, + totals.milliseconds, totals.plays + FROM ( + SELECT track_key, SUM(milliseconds) AS milliseconds, SUM(plays) AS plays, + MAX(day) AS last_day, + MAX(CASE WHEN artwork IS NOT NULL THEN day END) AS cover_day + FROM listening WHERE day >= ? AND day < ?$where GROUP BY track_key$having + ) AS totals + JOIN listening AS details ON details.track_key = totals.track_key + AND details.day = totals.last_day + LEFT JOIN listening AS covered ON covered.track_key = totals.track_key + AND covered.day = totals.cover_day + ORDER BY totals.milliseconds DESC$tail + '''; + /// Aggregate in SQLite so an annual recap transfers one row per song, artist /// and day, rather than every daily song record into the Flutter heap. /// A song shows its latest day's details, and the latest cover recorded in @@ -412,22 +450,7 @@ Future readListeningSummary( DateTime end, ) async { final arguments = [listeningDay(start), listeningDay(end)]; - final rows = await db.rawQuery(''' - SELECT totals.track_key, details.title, details.artist, details.album, - COALESCE(details.artwork, covered.artwork) AS artwork, - totals.milliseconds, totals.plays - FROM ( - SELECT track_key, SUM(milliseconds) AS milliseconds, SUM(plays) AS plays, - MAX(day) AS last_day, - MAX(CASE WHEN artwork IS NOT NULL THEN day END) AS cover_day - FROM listening WHERE day >= ? AND day < ? GROUP BY track_key - ) AS totals - JOIN listening AS details ON details.track_key = totals.track_key - AND details.day = totals.last_day - LEFT JOIN listening AS covered ON covered.track_key = totals.track_key - AND covered.day = totals.cover_day - ORDER BY totals.milliseconds DESC - ''', arguments); + final rows = await db.rawQuery(_listeningTracksSql(), arguments); final artistRows = await db.rawQuery(''' SELECT artist, SUM(milliseconds) AS milliseconds FROM listening WHERE day >= ? AND day < ? GROUP BY artist @@ -464,6 +487,37 @@ Future readListeningSummary( ); } +/// The tracks of [readListeningSummary] in its order, [limit] from +/// [offset], that can be played again from their key: played at least once +/// and not recorded under a stream URL. Equal listening times keep the +/// summary's order, by key. +Future> readReplayableListeningTracks( + DatabaseExecutor db, + DateTime start, + DateTime end, { + required int limit, + int offset = 0, +}) async { + final rows = await db.rawQuery( + _listeningTracksSql( + where: + " AND track_key NOT GLOB 'http://*'" + " AND track_key NOT GLOB 'https://*'", + having: ' HAVING SUM(plays) > 0', + tail: ', totals.track_key LIMIT ? OFFSET ?', + ), + [listeningDay(start), listeningDay(end), limit, offset], + ); + return [ + for (final row in rows) + ListeningTotal( + ListeningTrack.fromRow(row), + row['milliseconds'] as int, + row['plays'] as int, + ), + ]; +} + final _listeningClock = Stopwatch()..start(); final listeningRecorder = ListeningRecorder( write: ListeningStatisticsStore.instance.add, diff --git a/test/home_shelves_test.dart b/test/home_shelves_test.dart new file mode 100644 index 000000000..77c6069fe --- /dev/null +++ b/test/home_shelves_test.dart @@ -0,0 +1,626 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:audio_service/audio_service.dart' show MediaItem; +import 'package:flutter/services.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mornye/models/settings.dart'; +import 'package:mornye/models/unified_library_item.dart'; +import 'package:mornye/providers/download_history_provider.dart'; +import 'package:mornye/providers/library_counts_provider.dart'; +import 'package:mornye/providers/library_mixes_provider.dart'; +import 'package:mornye/providers/local_library_provider.dart'; +import 'package:mornye/providers/music_player_provider.dart'; +import 'package:mornye/providers/settings_provider.dart'; +import 'package:mornye/screens/home_tab.dart'; +import 'package:mornye/services/history_database.dart'; +import 'package:mornye/services/library_database.dart'; +import 'package:mornye/services/library_mixes.dart'; +import 'package:mornye/services/library_row_mapper.dart'; +import 'package:mornye/services/listening_statistics.dart'; +import 'package:mornye/services/music_player_runtime.dart'; +import 'package:mornye/services/sqlite_helpers.dart' as sqlite; +import 'package:mornye/utils/file_access.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:flutter_riverpod/misc.dart' show Override, ProviderListenable; +import 'package:sqflite/sqflite.dart'; +// The app reaches sqflite_common through sqflite and sqflite_common_ffi. +// ignore: depend_on_referenced_packages +import 'package:sqflite_common/sqflite_logger.dart'; +import 'package:sqflite_common_ffi/sqflite_ffi.dart' + show databaseFactoryFfi, sqfliteFfiInit; + +import 'support/fixed_settings.dart'; + +/// Every read the databases ran, in order. +final _statements = []; + +int _ran(String fragment) => + _statements.where((sql) => sql.contains(fragment)).length; + +/// Statistics reads, one per On Repeat load. +const _statisticsRead = 'FROM listening WHERE day >= ?'; + +/// The one-by-one Library lookups of a played file. +const _lookups = [ + 'JOIN history_path_keys hpk', + 'SELECT DISTINCT item.id, item.file_path', + 'FROM library_visible WHERE id', +]; + +int _lookupsRan() => _lookups.fold(0, (sum, sql) => sum + _ran(sql)); + +/// The unfiltered Library count. +const _countRead = 'SUM(track_count)'; + +late Directory _root; + +String _path(String name) => '${_root.path}/music/$name'; + +Map _download(String id, String path, {int number = 1}) => { + 'id': id, + 'trackName': 'Download $id', + 'artistName': 'Artist', + 'albumName': 'Album $id', + 'filePath': path, + 'service': 'example-provider', + 'downloadedAt': DateTime.utc(2026, 9, number + 1).toIso8601String(), + 'isrc': 'ISRC-$id', +}; + +Future _addScanned(String id, String path, {String? source}) async { + final db = await LibraryDatabase.instance.database; + await db.insert( + 'library', + LibraryRowMapper.encode( + LocalLibraryItem( + id: id, + sourceId: source ?? LibraryDatabase.legacySourceId, + trackName: 'Scanned $id', + artistName: 'Artist', + albumName: 'Album $id', + filePath: path, + scannedAt: DateTime.utc(2026, 10, 1), + ).toJson(), + ), + ); + final batch = db.batch(); + sqlite.putPathKeysInBatch(batch, 'library_path_keys', id, path); + await batch.commit(noResult: true); +} + +/// [plays] plays of [key] for [milliseconds] in total, [daysAgo] days ago. +Future _played( + String key, + int milliseconds, { + int plays = 1, + int daysAgo = 1, + String? artwork, +}) async { + final now = DateTime.now(); + await writeListeningDeltas(await ListeningStatisticsStore.instance.database, [ + ListeningDelta( + listeningDay(DateTime(now.year, now.month, now.day - daysAgo)), + ListeningTrack( + key: key, + title: 'Title of $key', + artist: 'Artist', + album: 'Album', + artwork: artwork, + ), + milliseconds, + plays, + ), + ]); +} + +class _History extends DownloadHistoryNotifier { + _History(this.initial); + + final DownloadHistoryState initial; + + @override + DownloadHistoryState build() => initial; + + void set(DownloadHistoryState next) => state = next; + + void bump() => + state = state.copyWith(loadedIndexVersion: state.loadedIndexVersion + 1); +} + +class _Revision extends LocalLibraryIndexRevision { + void bumpForTest() => state++; +} + +final _media = StreamController.broadcast(); + +ProviderContainer _container({ + required bool includeLocal, + List inMemory = const [], + List overrides = const [], +}) { + final container = ProviderContainer( + overrides: [ + settingsProvider.overrideWith( + () => FixedSettingsNotifier( + AppSettings(localLibraryEnabled: includeLocal), + ), + ), + downloadHistoryProvider.overrideWith( + () => _History(DownloadHistoryState(items: inMemory)), + ), + localLibraryIndexRevisionProvider.overrideWith(_Revision.new), + currentMediaItemProvider.overrideWithBuild( + (ref, notifier) => _media.stream, + ), + ...overrides, + ], + ); + addTearDown(container.dispose); + return container; +} + +/// Lets a provider rerun after a change. +Future _settle() => + Future.delayed(const Duration(milliseconds: 50)); + +/// Reruns of [provider] after its first load. +ProviderSubscription> _countReloads( + ProviderContainer container, + ProviderListenable> provider, + void Function() onReload, +) => container.listen(provider, (previous, next) { + if (next.isLoading) onReload(); +}); + +/// The shelf as the old provider made it: each statistics track in turn, +/// matched one lookup at a time. +Future> _oneByOne( + ProviderContainer container, { + required bool includeLocal, +}) async { + final now = DateTime.now(); + final summary = await ListeningStatisticsStore.instance.load( + DateTime(now.year, now.month, now.day - 27), + DateTime(now.year, now.month, now.day + 1), + ); + final history = container.read(downloadHistoryProvider.notifier); + final database = LibraryDatabase.instance; + final tracks = []; + for (final total in summary.tracks) { + if (tracks.length == 10) break; + final key = total.track.key; + if (total.plays <= 0 || + key.startsWith('http://') || + key.startsWith('https://')) { + continue; + } + final track = [key, total.track.title, total.track.artwork]; + final downloaded = await history.getByFilePathAsync(key); + if (downloaded != null) { + tracks.add([...track, 'dl:${downloaded.id}']); + continue; + } + final ids = await database.getPhysicalFileIds([key]); + if (ids.isNotEmpty && !includeLocal) continue; + final row = ids.isEmpty ? null : await database.getById(ids.first); + if (row != null) { + tracks.add([...track, 'local:${row['id']}']); + continue; + } + if (await fileExists(key)) tracks.add([...track, null]); + } + return tracks; +} + +List _describe(List tracks) => [ + for (final (:track, :item) in tracks) + [ + track.key, + track.title, + track.artwork, + switch (item) { + null => null, + UnifiedLibraryItem(:final historyItem?) => 'dl:${historyItem.id}', + UnifiedLibraryItem(:final localItem?) => 'local:${localItem.id}', + _ => 'other', + }, + ], +]; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + const paths = MethodChannel('plugins.flutter.io/path_provider'); + final messenger = + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; + + setUpAll(() async { + _root = await Directory.systemTemp.createTemp('home-shelves-'); + await Directory('${_root.path}/music').create(); + sqfliteFfiInit(); + // ignore: experimental_member_use + databaseFactory = SqfliteDatabaseFactoryLogger( + databaseFactoryFfi, + options: SqfliteLoggerOptions( + type: SqfliteDatabaseFactoryLoggerType.all, + log: (event) { + if (event is SqfliteLoggerSqlEvent) _statements.add(event.sql); + }, + ), + ); + messenger.setMockMethodCallHandler(paths, (_) async => _root.path); + }); + + tearDownAll(() async { + await LibraryDatabase.instance.close(); + await HistoryDatabase.instance.close(); + messenger.setMockMethodCallHandler(paths, null); + await _root.delete(recursive: true); + }); + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + await LibraryDatabase.instance.clearAll(); + await HistoryDatabase.instance.clearAll(); + await ListeningStatisticsStore.instance.clear(); + _statements.clear(); + }); + + group('Recently added', () { + for (final includeLocal in [true, false]) { + test('reloads per Library revision, not per History change ' + '(Local Library ${includeLocal ? 'on' : 'off'})', () async { + await HistoryDatabase.instance.upsertBatch([ + for (var index = 0; index < 4; index++) + _download('d$index', _path('d$index.flac'), number: index), + ]); + await _addScanned('s0', _path('s0.flac')); + final container = _container(includeLocal: includeLocal); + var reloads = 0; + _countReloads( + container, + homeRecentlyAddedProvider, + () => reloads++, + ); + final first = await container.read(homeRecentlyAddedProvider.future); + expect(first.items, hasLength(includeLocal ? 5 : 4)); + expect(first.trackCount, includeLocal ? 5 : 4); + + // Each completed download lands in memory first; the coalesced + // revision follows. + final history = + container.read(downloadHistoryProvider.notifier) as _History; + for (var index = 0; index < 5; index++) { + history.set( + history.state.copyWith( + items: [ + DownloadHistoryItem.fromJson( + _download('m$index', _path('m$index.flac')), + ), + ...history.state.items, + ], + ), + ); + await _settle(); + } + expect(reloads, 0); + + history.bump(); + final same = await container.read(homeRecentlyAddedProvider.future); + expect(reloads, 1); + // Nothing changed in the Library: the same songs, so Home does not + // rebuild. + expect(same, first); + expect(identical(same.items, first.items), isTrue); + + (container.read(localLibraryIndexRevisionProvider.notifier) + as _Revision) + .bumpForTest(); + await _settle(); + expect(reloads, includeLocal ? 2 : 1); + + await HistoryDatabase.instance.upsertBatch([ + // Added after the scanned song. + _download('d9', _path('d9.flac'), number: 40), + ]); + history.bump(); + final added = await container.read(homeRecentlyAddedProvider.future); + expect(added.items.first.historyItem?.id, 'd9'); + expect(added.trackCount, includeLocal ? 6 : 5); + // The Library index loads with its own startup warmup. + expect(container.exists(localLibraryProvider), isFalse); + }); + } + + test('shares one count with the Library badges per revision', () async { + await HistoryDatabase.instance.upsertBatch([ + _download('d0', _path('d0.flac')), + ]); + final container = _container(includeLocal: true); + container.listen(homeRecentlyAddedProvider, (_, _) {}); + final badges = libraryCountsProvider( + const LibraryCountsRequest( + searchQuery: '', + filterSource: null, + filterQuality: null, + filterFormat: null, + filterMetadata: null, + localLibraryEnabled: true, + ), + ); + container.listen(badges, (_, _) {}); + await container.read(homeRecentlyAddedProvider.future); + await container.read(badges.future); + expect(_ran(_countRead), 1); + + (container.read(downloadHistoryProvider.notifier) as _History).bump(); + await container.read(homeRecentlyAddedProvider.future); + await container.read(badges.future); + expect(_ran(_countRead), 2); + }); + }); + + group('On Repeat', () { + /// Downloads, scanned files (one on an offline source), files outside + /// the Library that exist or are gone, streams, songs never counted as + /// played and equal listening times; more matches than the shelf holds. + Future> seed() async { + final library = await LibraryDatabase.instance.database; + await library.insert('library_sources', { + 'id': 'offline', + 'path': _path('offline'), + 'display_name': 'Offline', + 'enabled': 1, + 'available': 0, + }, conflictAlgorithm: ConflictAlgorithm.replace); + final downloads = [ + for (var index = 0; index < 8; index++) + _download('d$index', _path('d$index.flac'), number: index), + // Another format of a scanned file: the download wins. + _download('dcopy', _path('s0.opus'), number: 9), + ]; + await HistoryDatabase.instance.upsertBatch(downloads); + for (var index = 0; index < 7; index++) { + await _addScanned('s$index', _path('s$index.flac')); + } + await _addScanned('off', _path('off.flac'), source: 'offline'); + await _addScanned('offgone', _path('offgone.flac'), source: 'offline'); + for (final name in ['outside0.flac', 'outside1.flac', 'off.flac']) { + await File(_path(name)).writeAsString('audio'); + } + final played = <(String, int)>[ + ('https://stream.example/a', 900000), + (_path('d0.flac'), 800000), + (_path('s0.flac'), 700000), + (_path('gone0.flac'), 650000), + (_path('s1.flac'), 600000), + (_path('outside0.flac'), 600000), + (_path('d1.flac'), 600000), + ('http://stream.example/b', 550000), + (_path('off.flac'), 500000), + (_path('offgone.flac'), 480000), + (_path('s2.flac'), 450000), + (_path('d2.flac'), 400000), + (_path('gone1.flac'), 380000), + (_path('s3.flac'), 350000), + (_path('outside1.flac'), 300000), + (_path('d3.flac'), 300000), + (_path('s4.flac'), 250000), + (_path('d4.flac'), 200000), + (_path('s5.flac'), 150000), + (_path('d5.flac'), 120000), + (_path('d6.flac'), 100000), + (_path('d7.flac'), 90000), + ]; + for (final (index, (key, milliseconds)) in played.indexed) { + await _played( + key, + milliseconds, + artwork: index.isEven ? 'https://covers.example/$index.jpg' : null, + ); + } + // Listened to, never for 30 seconds in one go. + await _played(_path('s6.flac'), 990000, plays: 0, daysAgo: 2); + // Days add up. + await _played(_path('d4.flac'), 5000, plays: 0, daysAgo: 3); + // Older than four weeks. + await _played(_path('d3.flac'), 2000000, daysAgo: 40); + return [ + for (final json in downloads.take(2)) + DownloadHistoryItem.fromJson(json), + ]; + } + + for (final includeLocal in [true, false]) { + test('matches the one-by-one shelf ' + '(Local Library ${includeLocal ? 'on' : 'off'})', () async { + final inMemory = await seed(); + final container = _container( + includeLocal: includeLocal, + inMemory: inMemory, + ); + container.listen(homeOnRepeatProvider, (_, _) {}); + final shelf = await container.read(homeOnRepeatProvider.future); + final expected = await _oneByOne(container, includeLocal: includeLocal); + expect(expected, hasLength(10)); + expect(_describe(shelf), expected); + }); + } + + test('a song after none follows no play; one after another may', () async { + await seed(); + final container = _container(includeLocal: true); + var reloads = 0; + _countReloads>( + container, + homeOnRepeatProvider, + () => reloads++, + ); + await container.read(homeOnRepeatProvider.future); + + // The restored session's song, then the next one after it plays. + _media.add(const MediaItem(id: 'a', title: 'A')); + await _settle(); + expect(reloads, 0); + _media.add(const MediaItem(id: 'b', title: 'B')); + await _settle(); + expect(reloads, 1); + _media.add(null); + await _settle(); + expect(reloads, 2); + _media.add(const MediaItem(id: 'c', title: 'C')); + await _settle(); + expect(reloads, 2); + }); + + test('continues past missing candidates to fill the shelf', () async { + for (var index = 0; index < 55; index++) { + final path = _path('candidate-$index.flac'); + await _played(path, 1000000 - index * 1000); + if (index >= 45) await File(path).writeAsString('audio'); + } + final container = _container(includeLocal: false); + container.listen(homeOnRepeatProvider, (_, _) {}); + final tracks = await container.read(homeOnRepeatProvider.future); + expect(tracks.map((entry) => entry.track.key), [ + for (var index = 45; index < 55; index++) + _path('candidate-$index.flac'), + ]); + expect(_ran(_statisticsRead), 2); + }); + + test( + 'rechecks memoized files outside the Library after deletion', + () async { + final path = _path('outside.flac'); + await File(path).writeAsString('audio'); + await _played(path, 100000); + final container = _container(includeLocal: false); + container.listen(homeOnRepeatProvider, (_, _) {}); + expect(await container.read(homeOnRepeatProvider.future), hasLength(1)); + + await File(path).delete(); + container.invalidate(homeOnRepeatProvider); + expect(await container.read(homeOnRepeatProvider.future), isEmpty); + }, + ); + + test( + 'a new song reads the statistics again and looks nothing up', + () async { + await seed(); + final container = _container(includeLocal: true); + container.listen(homeOnRepeatProvider, (_, _) {}); + _media.add(const MediaItem(id: 'a', title: 'A')); + final first = await container.read(homeOnRepeatProvider.future); + final lookups = _lookupsRan(); + expect(lookups, greaterThan(10)); + expect(_ran(_statisticsRead), 1); + + _media.add(const MediaItem(id: 'b', title: 'B')); + await _settle(); + final next = await container.read(homeOnRepeatProvider.future); + expect(_ran(_statisticsRead), 2); + expect(_lookupsRan(), lookups); + expect(identical(next, first), isTrue); + + // A play that moves a song up changes the shelf. + await _played(_path('s3.flac'), 5000000); + _media.add(const MediaItem(id: 'c', title: 'C')); + await _settle(); + final moved = await container.read(homeOnRepeatProvider.future); + expect(_lookupsRan(), lookups); + expect(moved.first.track.key, _path('s3.flac')); + + // A Library change looks the songs up again; the same matches keep + // the shelf as it was. + final before = _lookupsRan(); + (container.read(downloadHistoryProvider.notifier) as _History).bump(); + await _settle(); + final again = await container.read(homeOnRepeatProvider.future); + expect(_lookupsRan(), greaterThan(before)); + expect(identical(again, moved), isTrue); + }, + ); + }); + + group('Mixes', () { + test( + 'keep their views while a Library change leaves their songs', + () async { + final downloads = [ + for (var index = 0; index < 30; index++) + _download('d$index', _path('d$index.flac'), number: index % 25), + ]; + await HistoryDatabase.instance.upsertBatch(downloads); + final mix = LibraryMix( + kind: LibraryMixKind.artist, + key: 'artist', + label: 'Artist', + artists: const ['Artist'], + songIds: [for (var index = 0; index < 12; index++) 'dl_d$index'], + ); + SharedPreferences.setMockInitialValues({ + 'home_library_mixes_v2': jsonEncode({ + 'day': listeningDay(DateTime.now()), + 'includeLocal': false, + 'count': 30, + 'mixes': [mix.toJson()], + }), + }); + final runtime = MusicPlayerRuntime(); + addTearDown(runtime.dispose); + final container = _container( + includeLocal: false, + overrides: [ + libraryMixesStartupProvider.overrideWithBuild( + (ref, notifier) => true, + ), + musicPlayerRuntimeProvider.overrideWithValue(runtime), + ], + ); + var reloads = 0; + _countReloads>( + container, + libraryMixesProvider, + () => reloads++, + ); + final first = await container.read(libraryMixesProvider.future); + expect(first.single.items, hasLength(12)); + + final history = + container.read(downloadHistoryProvider.notifier) as _History; + // A download outside the mixes, in memory before its revision. + await HistoryDatabase.instance.upsertBatch([ + _download('d30', _path('d30.flac')), + ]); + history.set( + history.state.copyWith( + items: [ + DownloadHistoryItem.fromJson(_download('d30', _path('d30.flac'))), + ], + ), + ); + await _settle(); + expect(reloads, 0); + history.bump(); + await _settle(); + final kept = await container.read(libraryMixesProvider.future); + expect(reloads, 1); + expect(identical(kept, first), isTrue); + + // A mix song renamed. + await HistoryDatabase.instance.upsertBatch([ + {...downloads[3], 'trackName': 'Renamed'}, + ]); + history.bump(); + await _settle(); + final renamed = await container.read(libraryMixesProvider.future); + expect(identical(renamed, first), isFalse); + expect(renamed.single.items[3].trackName, 'Renamed'); + }, + ); + }); +} diff --git a/test/library_mixes_test.dart b/test/library_mixes_test.dart index 064b15c4a..dba10e78d 100644 --- a/test/library_mixes_test.dart +++ b/test/library_mixes_test.dart @@ -3,6 +3,7 @@ import 'package:mornye/providers/library_mixes_provider.dart'; import 'package:mornye/services/library_mixes.dart'; import 'package:mornye/services/library_queue_store.dart'; import 'package:mornye/services/library_schema.dart'; +import 'package:mornye/services/library_shadow_sql.dart'; import 'package:mornye/services/playable_media.dart'; import 'support/sqlite_process_database.dart'; @@ -580,16 +581,8 @@ void main() { test( 'pages hold downloads and the scanned files they do not shadow', () async { - final first = await store.mixSongs( - includeLocal: true, - limit: 1, - offset: 0, - ); - final second = await store.mixSongs( - includeLocal: true, - limit: 1, - offset: 1, - ); + final first = await store.mixSongs(source: 'downloaded', limit: 1); + final second = await store.mixSongs(source: 'local', limit: 10); expect( [...first, ...second], [ @@ -631,14 +624,128 @@ void main() { libraryMixAddedAt(second.single), DateTime.fromMillisecondsSinceEpoch(1700000000000), ); - final off = await store.mixSongs( - includeLocal: false, - limit: 10, - offset: 0, + // A pending download without a file is no song. + expect( + await store.mixSongs( + source: 'downloaded', + afterId: 'download', + limit: 10, + ), + isEmpty, ); - expect([for (final row in off) row['id']], ['download']); }, ); + + test('keyset pages list the songs OFFSET pages did, in order', () async { + // Ids out of insertion order, scanned copies a download shadows, + // files without a path and a source that is offline. + await db.insert('library_sources', { + 'id': 'offline', + 'path': '/offline', + 'display_name': 'Offline', + 'enabled': 1, + 'available': 0, + }); + for (var index = 0; index < 23; index++) { + final id = 'h${(index * 7) % 23}'; + await db.insert('history_db.history', { + 'id': id, + 'file_path': index % 9 == 4 ? '' : '/music/$id.flac', + 'track_name': 'Download $index', + 'artist_name': 'Artist ${index % 3}', + 'album_name': 'Album', + 'genre': 'Rap', + 'duration': 200 + index, + 'release_date': '2019', + 'downloaded_at': '2026-10-20T10:00:00.000', + }); + if (index % 4 == 0) { + await db.insert('history_db.history_path_keys', { + 'item_id': id, + 'path_key': 'shadow-$index', + }); + } + } + for (var index = 0; index < 31; index++) { + final id = 's${(index * 11) % 31}'; + await db.insert('library', { + 'id': id, + 'source_id': index % 10 == 3 ? 'offline' : 'legacy', + 'track_name': 'Scanned $index', + 'artist_name': 'Artist ${index % 5}', + 'album_name': 'Album $index', + 'album_key': 'album$index|artist', + 'file_path': index == 6 ? '' : '/music/$id.flac', + 'scanned_at': '2026-10-27T00:00:00Z', + 'genre': 'Jazz', + 'duration': 180 + index, + 'release_date': '${1990 + index}', + 'file_mod_time': 1700000000000 + index, + }); + await db.insert('library_path_keys', { + 'item_id': id, + 'path_key': index.isEven && index < 23 + ? 'shadow-$index' + : 'own-$index', + }); + } + + Future>> offsetPages(bool includeLocal) async { + final rows = >[]; + for (var offset = 0; ; offset += 5) { + final page = await db.rawQuery( + ''' + SELECT * FROM ( + SELECT 'downloaded' AS source, h.id, h.file_path, h.track_name, + h.artist_name, h.album_name, h.genre, h.duration, + h.release_date, h.downloaded_at AS added_at, + NULL AS file_mod_time + FROM history_db.history h + WHERE COALESCE(h.file_path, '') != '' + ${includeLocal ? ''' + UNION ALL + SELECT 'local' AS source, l.id, l.file_path, l.track_name, + l.artist_name, l.album_name, l.genre, l.duration, + l.release_date, l.scanned_at AS added_at, l.file_mod_time + FROM ${LibrarySchema.visibleView} l + WHERE COALESCE(l.file_path, '') != '' + AND ${libraryNotShadowedSql('l')} + ''' : ''} + ) + ORDER BY source, id + LIMIT ? OFFSET ? + ''', + [5, offset], + ); + rows.addAll(page); + if (page.length < 5) return rows; + } + } + + Future>> keysetPages(bool includeLocal) async { + final rows = >[]; + for (final source in ['downloaded', if (includeLocal) 'local']) { + String? afterId; + while (true) { + final page = await store.mixSongs( + source: source, + afterId: afterId, + limit: 5, + ); + rows.addAll(page); + afterId = page.lastOrNull?['id'] as String?; + if (page.length < 5 || afterId == null) break; + } + } + return rows; + } + + for (final includeLocal in [true, false]) { + final expected = await offsetPages(includeLocal); + expect(expected, hasLength(includeLocal ? 43 : 21)); + expect(await keysetPages(includeLocal), expected); + } + }); }); group('cached mixes', () { diff --git a/test/library_shadow_snapshot_test.dart b/test/library_shadow_snapshot_test.dart index 78471bdd5..b2e3c03d3 100644 --- a/test/library_shadow_snapshot_test.dart +++ b/test/library_shadow_snapshot_test.dart @@ -287,11 +287,10 @@ Future> _reads( } reads['folders'] = await store.directoryEntries(includeLocal: true); reads['files'] = await store.songFiles(); - reads['mixes'] = await store.mixSongs( - includeLocal: true, - limit: 1000, - offset: 0, - ); + reads['mixes'] = [ + for (final source in ['downloaded', 'local']) + ...await store.mixSongs(source: source, limit: 1000), + ]; for (final kind in [ LibrarySearchKind.songs, LibrarySearchKind.albums, @@ -365,11 +364,13 @@ Future _expectCurrent(String step) async { await rowCheck.songFiles(), reason: '$step files', ); - expect( - await library.getMixSongPage(includeLocal: true, limit: 1000, offset: 0), - await rowCheck.mixSongs(includeLocal: true, limit: 1000, offset: 0), - reason: '$step mixes', - ); + for (final source in ['downloaded', 'local']) { + expect( + await library.getMixSongPage(source: source, limit: 1000), + await rowCheck.mixSongs(source: source, limit: 1000), + reason: '$step $source mixes', + ); + } expect( _hits( await library.searchLibrary( diff --git a/test/listening_statistics_test.dart b/test/listening_statistics_test.dart index 724aa9b1c..26846348b 100644 --- a/test/listening_statistics_test.dart +++ b/test/listening_statistics_test.dart @@ -244,6 +244,80 @@ void main() { }, ); + test('replayable pages keep the summary order of played file keys', () async { + final db = await SqliteProcessDatabase.open(); + addTearDown(db.close); + await db.execute(''' + CREATE TABLE listening ( + day TEXT, track_key TEXT, title TEXT, artist TEXT, album TEXT, + artwork TEXT, milliseconds INTEGER, plays INTEGER, + PRIMARY KEY(day, track_key) + ) + '''); + // Few distinct listening times, so many songs tie; stream URLs, an + // upper-case scheme the old prefix check kept, and songs never played. + String key(int song) => switch (song % 11) { + 0 => 'https://stream.example/$song', + 1 => 'http://stream.example/$song', + 2 => 'HTTP://stream.example/$song', + 3 => 'network://nas/$song.flac', + _ => '/music/${(song * 37) % 101}.flac', + }; + for (var day = 1; day <= 6; day++) { + for (var song = day; song < 90; song += 2) { + await db.insert('listening', { + 'day': '2026-10-0$day', + 'track_key': key(song), + 'title': 'Song $song', + 'artist': 'Artist ${song % 4}', + 'album': 'Album', + 'artwork': (song + day) % 3 == 0 ? 'cover://$song/$day' : null, + 'milliseconds': 1000 * (1 + (song * day) % 3), + 'plays': (song + day) % 4 == 0 ? 0 : 1, + }); + } + } + final start = DateTime(2026, 10, 2), end = DateTime(2026, 10, 6); + List describe(Iterable totals) => [ + for (final total in totals) + [ + total.track.key, + total.track.title, + total.track.artwork, + total.milliseconds, + total.plays, + ], + ]; + final expected = describe( + (await readListeningSummary(db, start, end)).tracks.where( + (total) => + total.plays > 0 && + !total.track.key.startsWith('http://') && + !total.track.key.startsWith('https://'), + ), + ); + expect(expected.length, greaterThan(30)); + // Equal listening times, ordered by key as the summary orders them. + expect( + {for (final total in expected) (total as List)[3]}.length, + lessThan(expected.length ~/ 2), + ); + final pages = []; + for (var offset = 0; ; offset += 7) { + final page = await readReplayableListeningTracks( + db, + start, + end, + limit: 7, + offset: offset, + ); + expect(describe(page), expected.skip(offset).take(7).toList()); + pages.addAll(page); + if (page.length < 7) break; + } + expect(describe(pages), expected); + }); + test( 'artist artwork uses the most listened track with an available cover', () {