/// <summary> /// Called to process a group of songs belonging to the same album name ( but not necessarily the same artist ) /// </summary> /// <param name="album"></param> private async Task StoreAlbumAsync(ScannedAlbum album) { // Set the modified flag LibraryModified = true; Logger.Log(string.Format("Album: {0} Single artist: {1}", album.Name, album.SingleArtist)); // Get an existing or new Album entry for the songs Album songAlbum = await GetAlbumToHoldSongsAsync(album); // Keep track of Artist and ArtistAlbum entries in case they can be reused for different artists ArtistAlbum songArtistAlbum = null; Artist songArtist = null; // Add all the songs in the album foreach (ScannedSong songScanned in album.Songs) { // If there is no existing Artist entry or the current one if for the wrong Artist name then get or create a new one if ((songArtist == null) || (songArtist.Name != songScanned.ArtistName)) { // As this is a new Artist the ArtistAlbum needs to be re-initialised. if (songArtistAlbum != null) { songArtistAlbum.Songs.Sort((a, b) => a.Track.CompareTo(b.Track)); } songArtistAlbum = null; // Find the Artist for this song songArtist = await GetArtistToHoldSongsAsync(songScanned.ArtistName); } // If there is an existing ArtistAlbum then use it as it will be for the correct Artist, i.e. not cleared above if (songArtistAlbum == null) { // Find an existing or create a new ArtistAlbum entry songArtistAlbum = await GetArtistAlbumToHoldSongsAsync(songArtist, songAlbum); } // Add the song to the database, the album and the album artist Song songToAdd = new() { Title = songScanned.Tags.Title, Track = songScanned.Track, Path = songScanned.SourcePath, ModifiedTime = songScanned.Modified, Length = songScanned.Length, AlbumId = songAlbum.Id, ArtistAlbumId = songArtistAlbum.Id, SourceId = sourceBeingScanned.Id }; // No need to wait for this Songs.AddSongAsync(songToAdd); Logger.Log(string.Format("Artist: {0} Title: {1} Track: {2} Modified: {3} Length {4} Year {5}", songScanned.Tags.Artist, songScanned.Tags.Title, songScanned.Tags.Track, songScanned.Modified, songScanned.Length, songScanned.Year)); // Add to the Album songAlbum.Songs.Add(songToAdd); // Keep track whether or not to update the album bool updateAlbum = false; // Store the artist name with the album if ((songAlbum.ArtistName == null) || (songAlbum.ArtistName.Length == 0)) { songAlbum.ArtistName = songArtist.Name; updateAlbum = true; } else { // The artist has already been stored - check if it is the same artist if (songAlbum.ArtistName != songArtist.Name) { songAlbum.ArtistName = VariousArtistsString; updateAlbum = true; } } // Update the album year if not already set and this song has a year set if ((songAlbum.Year != songScanned.Year) && (songAlbum.Year == 0)) { songAlbum.Year = songScanned.Year; updateAlbum = true; } // Update the album genre. // Only try updating if a genre is defined for the song // If the album does not have a genre then get one for the song and store it in the album if ((songScanned.Tags.Genre.Length > 0) && (songAlbum.Genre.Length == 0)) { songAlbum.Genre = songScanned.Tags.Genre; updateAlbum = true; } if (updateAlbum == true) { await ConnectionDetailsModel.AsynchConnection.UpdateAsync(songAlbum); } // Add to the source sourceBeingScanned.Songs.Add(songToAdd); // Add to the ArtistAlbum songArtistAlbum.Songs.Add(songToAdd); } if (songArtistAlbum != null) { songArtistAlbum.Songs.Sort((a, b) => a.Track.CompareTo(b.Track)); } }
public void DisplayArtist(Artist artist) => Name.Text = artist.Name;
/// <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); }