Beispiel #1
0
        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<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;
        }