Skip to content

Commit 1a6d66f

Browse files
committed
refactor(playlists_manager): improve playlist retrieval logic
1 parent 8d7ba7e commit 1a6d66f

1 file changed

Lines changed: 14 additions & 33 deletions

File tree

lib/services/playlists_manager.dart

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -919,63 +919,44 @@ Future<Map> _fetchArtistPlaylist(String artistName) async {
919919
}
920920

921921
Map? _findCustomPlaylist(String id) {
922-
for (final playlist in userCustomPlaylists.value) {
923-
if (playlist['ytid']?.toString() == id) {
924-
return playlist as Map;
925-
}
926-
}
922+
final rootPlaylist = _findPlaylistById(userCustomPlaylists.value, id);
923+
if (rootPlaylist != null) return rootPlaylist;
927924

928925
for (final folder in userPlaylistFolders.value) {
929926
final folderPlaylists = folder['playlists'] as List<dynamic>? ?? [];
930-
for (final playlist in folderPlaylists) {
931-
if (playlist['ytid']?.toString() == id) {
932-
return playlist as Map;
933-
}
934-
}
927+
final folderPlaylist = _findPlaylistById(folderPlaylists, id);
928+
if (folderPlaylist != null) return folderPlaylist;
935929
}
936930

937931
return null;
938932
}
939933

940934
Map? _findOfflinePlaylist(String id) {
941-
for (final playlist in offlinePlaylistService.offlinePlaylists.value) {
935+
return _findPlaylistById(offlinePlaylistService.offlinePlaylists.value, id);
936+
}
937+
938+
Map? _findPlaylistById(Iterable<dynamic> playlists, String id) {
939+
for (final playlist in playlists) {
942940
if (playlist is Map && playlist['ytid']?.toString() == id) {
943941
return playlist;
944942
}
945943
}
944+
946945
return null;
947946
}
948947

949948
Future<Map?> _fetchYouTubePlaylist(String id) async {
950949
// 1. Local DB / in-memory caches (no network).
951-
Map? playlist;
952-
for (final p in playlists) {
953-
if (p['ytid']?.toString() == id) {
954-
playlist = p as Map;
955-
break;
956-
}
957-
}
950+
var playlist = _findPlaylistById(playlists, id);
958951

959952
// 2. User-added YouTube playlists.
960953
if (playlist == null) {
961-
final userPl = await getUserPlaylists();
962-
for (final p in userPl) {
963-
if (p['ytid']?.toString() == id) {
964-
playlist = p as Map;
965-
break;
966-
}
967-
}
954+
final userPlaylists = await getUserPlaylists();
955+
playlist = _findPlaylistById(userPlaylists, id);
968956
}
969957

970958
// 3. Previously fetched online playlists.
971-
if (playlist == null) {
972-
for (final p in onlinePlaylists) {
973-
if (p['ytid']?.toString() == id) {
974-
playlist = p as Map;
975-
break;
976-
}
977-
}
978-
}
959+
playlist ??= _findPlaylistById(onlinePlaylists, id);
979960

980961
// 4. Fetch from YouTube as a last resort.
981962
if (playlist == null) {

0 commit comments

Comments
 (0)