/// <summary> /// Add a list of songs to the playlist /// </summary> /// <param name="playlist"></param> /// <param name="songs"></param> public void AddSongs(IEnumerable <Song> songs) { List <SongPlaylistItem> songPlaylistItems = new(); // For each song create a PlayListItem and add to the PlayList foreach (Song song in songs) { song.Artist = Artists.GetArtistById(ArtistAlbums.GetArtistAlbumById(song.ArtistAlbumId).ArtistId); SongPlaylistItem itemToAdd = new() { Artist = song.Artist, PlaylistId = Id, Song = song, SongId = song.Id, Index = PlaylistItems.Count }; songPlaylistItems.Add(itemToAdd); PlaylistItems.Add(itemToAdd); } // No need to wait for this DbAccess.InsertAllAsync(songPlaylistItems); }
/// <summary> /// Duplicate the SongPlaylist in the specified library /// </summary> /// <param name="playlistToDuplicate"></param> /// <returns></returns> private static async void DuplicateSongPlaylistAsync(SongPlaylist playlistToDuplicate, int libraryId) { // Attempt to find matching songs for each SongPlaylistItem in the SongPlaylist // Need to access the songs via the Sources associated with the Library List <Source> sources = Sources.GetSourcesForLibrary(libraryId); // Keep track of the matching songs List <Song> songsToAdd = new(); foreach (SongPlaylistItem item in playlistToDuplicate.PlaylistItems) { Song matchingSong = null; int sourceIndex = 0; while ((matchingSong == null) && (sourceIndex < sources.Count)) { // Get a list of all the songs with matching Titles in the source List <Song> matchingTitles = Songs.GetSourceSongsWithName(sources[sourceIndex++].Id, item.Song.Title); // Now for each song access the associated artist int titleIndex = 0; while ((matchingSong == null) && (titleIndex < matchingTitles.Count)) { Artist nameCheck = Artists.GetArtistById(ArtistAlbums.GetArtistAlbumById(matchingTitles[titleIndex].ArtistAlbumId).ArtistId); // Correct name? if (nameCheck.Name == item.Artist.Name) { matchingSong = matchingTitles[titleIndex]; songsToAdd.Add(matchingSong); // Make sure that the Artist is stored with the song matchingSong.Artist = nameCheck; } titleIndex++; } } } // Only create the playlist if at least one of the songs was found if (songsToAdd.Count > 0) { SongPlaylist duplicatedPlaylist = new() { Name = playlistToDuplicate.Name, LibraryId = libraryId }; // Wait for the playlist to be added as we're going to use its id await Playlists.AddPlaylistAsync(duplicatedPlaylist); // Add the songs to the new SongPlaylist. duplicatedPlaylist.AddSongs(songsToAdd); } }
/// <summary> /// Get the PlaylistItems and associated songs for this playlist /// </summary> /// <param name="playlistItems"></param> public void GetContents(IEnumerable <SongPlaylistItem> playlistItems) { // Get all the SongPlaylistItem entries associated with this SongPlaylist and then the Song entries for each of them PlaylistItems.AddRange(playlistItems.Where(item => item.PlaylistId == Id)); foreach (SongPlaylistItem playlistItem in PlaylistItems) { playlistItem.Song = Songs.GetSongById(playlistItem.SongId); playlistItem.Artist = Artists.GetArtistById(ArtistAlbums.GetArtistAlbumById(playlistItem.Song.ArtistAlbumId).ArtistId); playlistItem.Song.Artist = playlistItem.Artist; } PlaylistItems.Sort((a, b) => a.Index.CompareTo(b.Index)); }
/// <summary> /// Called to determine whether a song that has been scanned requires adding to the library, or just an existing entry updated /// </summary> /// <param name="song"></param> /// <returns></returns> private async Task <bool> DoesSongRequireAdding(ScannedSong song) { bool needsAdding = true; // Lookup the path of this song in the dictionary if ((songLookup.TryGetValue(song.SourcePath, out Song matchedSong) == true) && (matchedSong.ScanAction == Song.ScanActionType.Differ)) { // The library is about to be changed in some way so set the modified flag LibraryModified = true; // Need to check whether the matched Artist or Album names have changed. If they have then treat this as a new song and mark // the matched song for deletion ArtistAlbum matchedArtistAlbum = ArtistAlbums.GetArtistAlbumById(matchedSong.ArtistAlbumId); Artist matchedArtist = Artists.GetArtistById(matchedArtistAlbum.ArtistId); // If the artist or album name has changed then treat this as a new song. Otherwise update the existing song in the library if ((matchedArtist.Name.ToUpper() != song.ArtistName.ToUpper()) || (matchedArtistAlbum.Name.ToUpper() != song.Tags.Album.ToUpper())) { // The existing song needs to be deleted now. // If the song is not deleted then there will be more than one associated with the same storage location. // This is much less complicated than trying to move the existing song to a new Artist or Album // If an Artist has been deleted due to this song deletion then remove it from the dictionary being used here Artist deletedArtist = LibraryScanController.DeleteSong(matchedSong); if (deletedArtist != null) { artistsInLibrary.Remove(deletedArtist.Name.ToUpper()); } matchedSong.ScanAction = Song.ScanActionType.Matched; } else { // No need to add a new song, update the existing one needsAdding = false; matchedSong.Length = song.Length; matchedSong.ModifiedTime = song.Modified; matchedSong.Title = song.Tags.Title; matchedSong.Track = song.Track; await ConnectionDetailsModel.AsynchConnection.UpdateAsync(matchedSong); // Check if the year or genre fields on the album needs updating // Don't update the Album if it is a 'various artists' album as the these fields is not applicable Album matchedAlbum = Albums.GetAlbumById(matchedArtistAlbum.AlbumId); if (matchedAlbum.ArtistName != SongStorage.VariousArtistsString) { // Update the stored year if it is different to the artist year and the artist year is defined. // Log when a valid year is overwritten by different year if (matchedAlbum.Year != song.Year) { if (song.Year != 0) { matchedAlbum.Year = song.Year; await ConnectionDetailsModel.AsynchConnection.UpdateAsync(matchedAlbum); } } if (song.Tags.Genre.Length > 0) { // If this album does not already have a genre, or the genre has been changed then set it now if ((matchedAlbum.Genre == null) || (matchedAlbum.Genre != song.Tags.Genre)) { matchedAlbum.Genre = song.Tags.Genre; await ConnectionDetailsModel.AsynchConnection.UpdateAsync(matchedAlbum); } } } } } return(needsAdding); }