protected override async Task OnPlaylistChanged(CancellationToken cancellationToken) { var playlistData = new PlaylistData(Songs, CurrentSongIndex); await sessionDataService.SaveData(PlaylistDataKey, playlistData, cancellationToken); await base.OnPlaylistChanged(cancellationToken); }
private async Task <(IReadOnlyCollection <SongModel> Songs, int?CurrentSongIndex)> LoadPlaylistSongs(PlaylistData playListData, CancellationToken cancellationToken) { var songIds = playListData.Songs .Select(s => s.Id) .Select(id => new ItemId(id)) .Distinct(); var loadedSongs = (await songsService.GetSongs(songIds, cancellationToken)) .ToDictionary(s => s.Id, s => s); var newSongIndex = playListData.CurrentSongIndex; var playListSongs = new List <SongModel>(); foreach (var(playlistSong, songIndex) in playListData.Songs.Select((song, i) => (song, i))) { if (!loadedSongs.TryGetValue(new ItemId(playlistSong.Id), out var loadedSong)) { logger.LogWarning($"Song {playlistSong.Id} from saved playlist was not found in library. Ignoring saved playlist."); return(null, null); } if (loadedSong.IsDeleted) { logger.LogWarning($"Song '{GetSongTitle(loadedSong)}' from saved playlist was deleted from the library. Ignoring this song."); if (songIndex < newSongIndex) { --newSongIndex; } continue; } playListSongs.Add(loadedSong); } if (newSongIndex != null && (newSongIndex < 0 || newSongIndex >= playListSongs.Count)) { logger.LogWarning($"Index of current song in saved playlist is invalid ({newSongIndex}, [0, {playListSongs.Count})). Ignoring saved playlist."); return(null, null); } return(playListSongs, newSongIndex); }