diff --git a/lib/services/library_mixes.dart b/lib/services/library_mixes.dart index 2ab548d6f..f622befda 100644 --- a/lib/services/library_mixes.dart +++ b/lib/services/library_mixes.dart @@ -719,55 +719,67 @@ class _MixBuilder { List build() { _index(); if (songs.length < libraryMixMinLibrary) return const []; - final mixes = []; - if (_favorites() case final mix?) mixes.add(mix); - - // Adds up to [count] mixes from [keys], passing over a key that makes - // none, and returns how many it added. - int take(Iterator keys, LibraryMix? Function(T) make, int count) { - var added = 0; - while (added < count && - mixes.length < libraryMixMaxCount && - keys.moveNext()) { - if (_add(mixes, make(keys.current))) added++; + // Made in the order of who keeps the songs two mixes share: favorites, + // then genres, before the kinds that cut across them. + final made = []; + List make( + Iterable keys, + LibraryMix? Function(T) build, + int count, + ) { + final added = []; + for (final key in keys) { + if (added.length == count) break; + final mix = build(key); + if (_add(made, mix)) added.add(mix!); } return added; } - // Every kind gets a turn before any gets more, so the shelf is varied - // before it is deep. + final favorites = make([_favorites], (build) => build(), 1); final ranked = _families(); - final families = ranked.iterator; final tried = {}; LibraryMix? genreMix(String family) { tried.add(family); return _genreMix(family); } - final genres = take(families, genreMix, _leadGenreMixes); - for (final make in [_fresh, _rediscover, _discover, _deepCuts, _yearMix]) { - if (mixes.length >= libraryMixMaxCount) break; - _add(mixes, make()); - } - final decades = _decades().iterator; - take(decades, _decadeMix, 1); + final genres = make(ranked, genreMix, _maxGenreMixes); + final found = make( + [_fresh, _rediscover, _discover, _deepCuts, _yearMix], + (build) => build(), + 5, + ); + final decades = make(_decades(), _decadeMix, _maxDecadeMixes); // Where tags are missing or little else came out, the artists played // most anchor up to three mixes of their own. final tagged = songs.where((song) => familiesById[song.id]!.isNotEmpty); - final sparse = mixes.length < 3 || tagged.length * 2 < songs.length; - final artists = _mixArtists(sparse: sparse).iterator; - LibraryMix? artistMix(String artist) => - mixes.any((mix) => mix.key == artist) ? null : _artistMix(artist); - take(artists, artistMix, 1); - take(families, genreMix, _maxGenreMixes - genres); - take(decades, _decadeMix, _maxDecadeMixes - 1); - take(artists, artistMix, (sparse ? 3 : _maxArtistMixes) - 1); + final sparse = made.length < 3 || tagged.length * 2 < songs.length; + final artists = make( + _mixArtists(sparse: sparse), + (artist) => + made.any((mix) => mix.key == artist) ? null : _artistMix(artist), + sparse ? 3 : _maxArtistMixes, + ); // One genre past those played most, a different one each day. final others = [ for (final family in ranked) if (!tried.contains(family)) family, ]..shuffle(_random(LibraryMixKind.genre, 'rotation')); - take(others.iterator, genreMix, 1); + final extra = make(others, genreMix, 1); + // Every kind gets a turn before any gets more, so a full shelf is + // varied before it is deep. + final mixes = [ + ...favorites, + ...genres.take(_leadGenreMixes), + ...found, + ...decades.take(1), + ...artists.take(1), + ...genres.skip(_leadGenreMixes), + ...decades.skip(1), + ...artists.skip(1), + ...extra, + ].take(libraryMixMaxCount).toList(); // Favorites lead and the two mixes played most follow. Discoveries, which // no play count brings forward, then take turns with the rest by plays. double weight(LibraryMix mix) => diff --git a/test/library_mixes_test.dart b/test/library_mixes_test.dart index 70258768d..da67fc1c9 100644 --- a/test/library_mixes_test.dart +++ b/test/library_mixes_test.dart @@ -591,6 +591,38 @@ void main() { expect(first.length, greaterThan(1)); }); + test('a genre keeps its mix over an artist mix of the same songs', () { + const played = ['Rock', 'Pop', 'Jazz']; + final songs = [ + for (final genre in played) + for (var a = 0; a < 12; a++) + for (var s = 0; s < 3; s++) + _song('$genre$a-$s', '$genre Act $a', genre), + // The fourth genre, whose one played artist has songs enough to + // name a mix drawn from the same songs. + for (var a = 0; a < 8; a++) + for (var s = 0; s < 5; s++) + _song('metal$a-$s', 'Metal Act $a', 'Metal'), + ]; + final mixes = buildLibraryMixes( + LibraryMixRequest( + songs: songs, + at: day, + exists: exists, + plays: { + for (final genre in played) + for (var a = 0; a < 4; a++) 'local_$genre$a-0': 9, + for (var s = 0; s < 5; s++) 'local_metal0-$s': 2, + }, + ), + ); + expect([ + for (final mix in mixes) + if (mix.kind == LibraryMixKind.genre) mix.key, + ], containsAll(['rock', 'pop', 'jazz', 'metal'])); + expect(mixes.where((mix) => mix.kind == LibraryMixKind.artist), isEmpty); + }); + test('one genre past the five played most joins, a new one each day', () { final songs = _wideLibrary(); const leading = ['hiphop', 'rock', 'country', 'electronic', 'folk'];