コード例 #1
0
        private static async Task <SavingError> SaveViezTrackLevel3(WebSong track)
        {
            if (track == null)
            {
                return(SavingError.Unknown);
            }
            while (!App.Locator.CollectionService.IsLibraryLoaded)
            {
            }

            var startTransaction = !App.Locator.SqlService.DbConnection.IsInTransaction;

            if (startTransaction)
            {
                App.Locator.SqlService.BeginTransaction();
            }

            var result = await SaveViezTrackLevel4(track);

            if (startTransaction)
            {
                App.Locator.SqlService.Commit();
            }

            ShowResults(result.Error, track.Name);

            if (result.Song != null)
            {
                if (track != null && track.ArtworkImage != null)
                {
                    await DownloadArtworks.SaveArtworkByUrl(track.ArtworkImage, result.Song.Album.Name);
                }
                else
                {
                    await DownloadArtworks.DownloadAlbumsArtworkAsync();
                }


                if (App.Locator.Setting.SpotifyArtworkSync)
                {
                    await DownloadArtworks.DownloadArtistsArtworkAsyncFromSpotify(false, result.Song.Artist.Name);
                }
                else
                {
                    await DownloadArtworks.DownloadArtistsArtworkAsync(false, result.Song.Artist.Name);
                }
            }
            return(result.Error);
        }
コード例 #2
0
        public static async Task SaveSpotifyAlbumLevel1(FullAlbum album)
        {
            if (album.Tracks.Items.Count == 0)
            {
                ToastManager.ShowError("Nothing found.");
                return;
            }

            //clean track only english letters
            album.Name        = SongSavingHelper.ToCleanQuery(album.Name);
            album.Artist.Name = SongSavingHelper.ToCleanQuery(album.Artist.Name);
            foreach (var obj in album.Tracks.Items)
            {
                obj.Name = SongSavingHelper.ToCleanQuery(obj.Name);
            }

            //check if it is track properties are noll or not;
            if (!CheckUp(album))
            {
                ShowResults(SavingError.Unknown, album.Name);
                return;
            }


            var alreadySaving = SpotifySavingAlbums.FirstOrDefault(p => p == album.Id) != null;

            if (alreadySaving)
            {
                ShowResults(SavingError.AlreadySaving, album.Name);
                return;
            }

            SpotifySavingAlbums.Add(album.Id);

            //ignore
            //problem when os is not under this thread.
            //solution: use dispatcher.
            //testing under android pending.

            while (!App.Locator.CollectionService.IsLibraryLoaded)
            {
            }
            //var collAlbum = App.Locator.CollectionService.Albums.FirstOrDefault(p => p.ProviderId.Contains(album.Id));

            var collAlbum = App.Locator.CollectionService.Albums.FirstOrDefault(p => string.Equals(p.Name.ToLower(), album.Name.ToLower()));

            var alreadySaved = collAlbum != null;

            if (alreadySaved)
            {
                var missingTracks = collAlbum.Songs.Count < album.Tracks.Items.Count;
                if (!missingTracks)
                {
                    ShowResults(SavingError.AlreadyExists, album.Name);
                    SpotifySavingAlbums.Remove(album.Id);
                    return;
                }
            }
            ToastManager.ShowError("Saving " + album.Name + ".");
            var index = 0;

            if (!alreadySaved)
            {
                SavingError result;
                do
                {
                    // first save one song (to avoid duplicate album creation)
                    result = await SaveSpotifyTrackLevel3(album.Tracks.Items[index], album, false);

                    index++;
                }while (result != SavingError.None && index < album.Tracks.Items.Count);
            }

            bool success;
            var  missing = false;

            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 => SaveSpotifyTrackLevel3(track, album, false));
                var results = await Task.WhenAll(songs);

                // now wait a split second before showing success message
                await Task.Delay(1000);

                var alreadySavedCount = results.Count(p => p == SavingError.AlreadyExists);
                var successCount      =
                    results.Count(
                        p =>
                        p == SavingError.None || p == SavingError.AlreadyExists || p == SavingError.AlreadySaving);
                var missingCount = successCount == 0 ? -1 : album.Tracks.Items.Count - (successCount + index);
                success = missingCount == 0;
                missing = missingCount > 0;
                if (missing)
                {
                    ToastManager.ShowError("Problem while saving " + missingCount + " tracks.");
                }
                App.Locator.SqlService.Commit();
            }
            else
            {
                success = App.Locator.CollectionService.Albums.FirstOrDefault(p => p.ProviderId.Contains(album.Id)) != null;
            }

            if (success)
            {
                ShowResults(SavingError.None, album.Name);
            }

            else if (!missing)
            {
                ShowResults(SavingError.Unknown, album.Name);
            }


            SpotifySavingAlbums.Remove(album.Id);

            if (collAlbum == null)
            {
                collAlbum = App.Locator.CollectionService.Albums.FirstOrDefault(p => string.Equals(p.Name.ToLower(), album.Name.ToLower()));
                if (collAlbum != null && !collAlbum.HasArtwork && !collAlbum.NoArtworkFound)
                {
                    await DownloadArtworks.SaveAlbumImageAsync(collAlbum, album.Images[0].Url);
                }

                if (App.Locator.Setting.SpotifyArtworkSync)
                {
                    await DownloadArtworks.DownloadArtistsArtworkAsyncFromSpotify(false, album.Artist.Name);
                }
                else
                {
                    await DownloadArtworks.DownloadArtistsArtworkAsync(false, album.Artist.Name);
                }
            }
        }
コード例 #3
0
        private static async Task <SavingError> SaveSpotifyTrackLevel3(SimpleTrack track, FullAlbum album, bool onFinishDownloadArtwork = true)
        {
            //album = Cleanup(album);
            // track = Cleanup(track);

            if (track == null || album == null)
            {
                return(SavingError.Unknown);
            }
            var alreadySaving = SpotifySavingTracks.FirstOrDefault(p => p == track.Id) != null;

            if (alreadySaving)
            {
                return(SavingError.AlreadySaving);
            }


            //adding id(alway unique) of track to list to avoid duplicate things.
            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 SaveSpotifyTrackLevel4(track, album);

            if (startTransaction)
            {
                App.Locator.SqlService.Commit();
            }

            // ShowResults(result.Error, track.Name);
            SpotifySavingTracks.Remove(track.Id);

            if (!onFinishDownloadArtwork)
            {
                return(result.Error);
            }

            if (result.Song != null)
            {
                if (!result.Song.Album.HasArtwork && !result.Song.Album.NoArtworkFound)
                {
                    await DownloadArtworks.SaveAlbumImageAsync(result.Song.Album, album.Images[0].Url);
                }

                if (App.Locator.Setting.SpotifyArtworkSync)
                {
                    await DownloadArtworks.DownloadArtistsArtworkAsyncFromSpotify(false, result.Song.Artist.Name);
                }
                else
                {
                    await DownloadArtworks.DownloadArtistsArtworkAsync(false, result.Song.Artist.Name);
                }
            }

            return(result.Error);
        }