public static async Task<SaveResults> SaveTrackAsync(SimpleTrack track, FullAlbum album) { try { if (track == null || album == null) { return new SaveResults() { Error = SavingError.Unknown }; } var preparedSong = track.ToSong(); var exists = App.Locator.CollectionService.SongAlreadyExists( preparedSong.ProviderId, track.Name, album.Name, album.Artist != null ? album.Artist.Name : track.Artist.Name); if (exists != null) { return new SaveResults { Error = SavingError.AlreadyExists, Entry = exists }; } var fullTrack = track as FullTrack ?? await App.Locator.Spotify.GetTrack(track.Id); var artist = fullTrack != null ? fullTrack.Artist : track.Artist; preparedSong.ArtistName = fullTrack != null ? string.Join(", ", fullTrack.Artists.Select(p => p.Name)) : artist.Name; preparedSong.Album = album.ToAlbum(); preparedSong.Artist = album.Artist.ToArtist(); preparedSong.Album.PrimaryArtist = preparedSong.Artist; await App.Locator.CollectionService.AddSongAsync(preparedSong).ConfigureAwait(false); CollectionHelper.MatchSong(preparedSong); return new SaveResults() { Error = SavingError.None, Entry = preparedSong}; } catch (NetworkException) { return new SaveResults() { Error = SavingError.Network }; } catch { return new SaveResults() { Error = SavingError.Unknown }; } }
public static async Task SaveAlbumAsync(FullAlbum album) { if (album.Tracks.Items.Count == 0) { CurtainPrompt.ShowError("AlbumNoTracks".FromLanguageResource()); return; } var alreadySaving = SpotifySavingAlbums.FirstOrDefault(p => p == album.Id) != null; if (alreadySaving) { CurtainPrompt.ShowError("EntryAlreadySaving".FromLanguageResource(), album.Name); return; } SpotifySavingAlbums.Add(album.Id); while (!App.Locator.CollectionService.IsLibraryLoaded) { } var collAlbum = App.Locator.CollectionService.Albums.FirstOrDefault(p => p.ProviderId.Contains(album.Id)); var alreadySaved = collAlbum != null; if (alreadySaved) { var missingTracks = collAlbum.Songs.Count < album.Tracks.Items.Count; if (!missingTracks) { CurtainPrompt.ShowError(() => { App.Navigator.GoTo<CollectionAlbumPage, ZoomInTransition>(collAlbum); },"EntryAlreadySaved".FromLanguageResource(), album.Name); SpotifySavingAlbums.Remove(album.Id); return; } } CurtainPrompt.Show("EntrySaving".FromLanguageResource(), album.Name); using ( var handler = Insights.TrackTime( "Save Album", new Dictionary<string, string> { {"Type", "Spotify"}, {"ProviderId", album.Id}, {"Name", album.Name}, {"AritstName", album.Artist.Name}, {"TotalCount", album.Tracks.Items.Count.ToString()} })) { var index = 0; if (!alreadySaved) { SavingError result; do { // first save one song (to avoid duplicate album creation) result = (await _SaveTrackAsync(album.Tracks.Items[index], album, false)).Error; index++; } while (result != SavingError.None && index < album.Tracks.Items.Count); } bool success; var missing = false; SaveResults[] results = null; if (album.Tracks.Items.Count > 1) { App.Locator.SqlService.BeginTransaction(); // save the rest at the rest time var songs = album.Tracks.Items.Skip(index).Select(track => _SaveTrackAsync(track, album, false)); results = await Task.WhenAll(songs); // now wait a split second before showing success message await Task.Delay(1000); var alreadySavedCount = results.Count(p => p.Error == SavingError.AlreadyExists); handler.Data.Add("AlreadySaved", alreadySavedCount.ToString()); var successCount = results.Count( p => p.Error == SavingError.None || p.Error == SavingError.AlreadyExists || p.Error == SavingError.AlreadySaving); var missingCount = successCount == 0 ? -1 : album.Tracks.Items.Count - (successCount + index); success = missingCount == 0; missing = missingCount > 0; if (missing) { handler.Data.Add("MissingCount", missingCount.ToString()); CurtainPrompt.ShowError("AlbumMissingTracks".FromLanguageResource(), missingCount, album.Name); } App.Locator.SqlService.Commit(); } else { success = App.Locator.CollectionService.Albums.FirstOrDefault(p => p.ProviderId.Contains(album.Id)) != null; } if (success) { CurtainPrompt.Show(() => { App.Navigator.GoTo<CollectionAlbumPage, ZoomInTransition>(((Song)results[0].Entry).Album); }, "EntrySaved".FromLanguageResource(), album.Name); } else if (!missing) { CurtainPrompt.ShowError("EntrySavingError".FromLanguageResource(), album.Name); } SpotifySavingAlbums.Remove(album.Id); if (collAlbum == null && results != null) { await SaveAlbumImageAsync(((Song) results[0].Entry).Album, album.Images[0].Url); await DownloadArtistsArtworkAsync(); } } }
public static async Task<SaveResults> SaveTrackAsync(SimpleTrack track, FullAlbum album, bool trackTime = true) { var handle = Insights.TrackTime( "Save Song", new Dictionary<string, string> { {"Type", "Spotify"}, {"Subtype", "Simple"}, {"ProviderId", track.Id}, {"Name", track.Name}, {"ArtistName", track.Artist != null ? track.Artist.Name : null} }); if (trackTime) { handle.Start(); } var result = await _SaveTrackAsync(track, album); ShowResults(result, track.Name); if (trackTime) { handle.Data.Add("SavingError", result.ToString()); handle.Stop(); } return result; }
private static async Task<SaveResults> _SaveTrackAsync( SimpleTrack track, FullAlbum album, bool onFinishDownloadArtwork = true) { if (track == null || album == null) { return new SaveResults { Error = SavingError.Unknown }; } var alreadySaving = SpotifySavingTracks.FirstOrDefault(p => p == track.Id) != null; if (alreadySaving) { return new SaveResults { Error = SavingError.AlreadySaving }; } SpotifySavingTracks.Add(track.Id); while (!App.Locator.CollectionService.IsLibraryLoaded) { } var startTransaction = !App.Locator.SqlService.DbConnection.IsInTransaction; if (startTransaction) { App.Locator.SqlService.BeginTransaction(); } var result = await SpotifyHelper.SaveTrackAsync(track, album); if (startTransaction) { App.Locator.SqlService.Commit(); } ShowErrorResults(result.Error, track.Name); SpotifySavingTracks.Remove(track.Id); if (!onFinishDownloadArtwork) { return result; } if (result.Entry != null) { #pragma warning disable 4014 var song = (Song) result.Entry; if (!song.Album.HasArtwork && !song.Album.NoArtworkFound) { SaveAlbumImageAsync(song.Album, album.Images[0].Url); } DownloadArtistsArtworkAsync(); #pragma warning restore 4014 } return result; }
private static DateTime GetDateTime(FullAlbum album) { if (album.ReleaseDatePrecision != "year") return DateTime.Parse(album.ReleaseDate); var year = int.Parse(album.ReleaseDate); return new DateTime(year, 1, 1); }