Beispiel #1
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);
                }
            }
        }
Beispiel #2
0
        private async Task HandleDownload(Song song, DownloadOperation download, bool start)
        {
            if (song == null || download == null)
            {
                return;
            }
            try
            {
                song.Download = new BackgroundDownload(download);
                ActiveDownloads.Add(song);
                Progress <DownloadOperation> progressCallback = new Progress <DownloadOperation>(DownloadProgress);
                if (start)
                {
                    // Start the download and attach a progress handler.
                    await download.StartAsync().AsTask(song.Download.CancellationTokenSrc.Token, progressCallback);
                }
                else
                {
                    // The download was already running when the application started, re-attach the progress handler.
                    await download.AttachAsync().AsTask(song.Download.CancellationTokenSrc.Token, progressCallback);

                    try
                    {
                        song.SongState = SongState.Downloading;
                        sqlService.UpdateItem(song);
                    }
                    catch
                    {
                    }
                }

                // song.Download = new BackgroundDownload(download);
                //  ActiveDownloads.Add(song);


                ResponseInformation response = download.GetResponseInformation();
                if (response.StatusCode >= 200)
                {
                    var file = download.ResultFile as StorageFile;

                    if (await IsFileValid(file))
                    {
                        await DownloadFinishedForAsync(song, download);
                    }

                    else
                    {
                        song.SongState = SongState.NoMatch;
                        sqlService.UpdateItem(song);
                        ToastManager.ShowError("Try re matching.");
                        deleteFileSafe(download);
                    }
                }
                else
                {
                    song.SongState = SongState.NoMatch;
                    sqlService.UpdateItem(song);
                    ToastManager.ShowError("No http response.");
                    deleteFileSafe(download);
                }
            }

            catch (Exception e)
            {
                //   var status = BackgroundTransferError.GetStatus(-2147012879);
                //  Debug.WriteLine(status);

                if (e.HResult == -2147012879 || e.Message.Equals("Exception from HRESULT: 0x80072EF1"))
                {
                    MessageHelpers.ShowError("Error while downloading file, operation cancelled by system.",
                                             "Restart device!!!");
                }

                if (e.HResult == -2145844844 || e.Message.Equals("Not found (404). (Exception from HRESULT: 0x80190194)"))
                {
                    MessageHelpers.ShowError("Error while downloading file, link broken.", "Rematch track!!!", song: song, IsRematchError: true);
                }

                try
                {
                    song.Download.CancellationTokenSrc?.Cancel();
                }
                catch
                {
                }

                song.SongState = SongState.DownloadListed;
                sqlService.UpdateItem(song);
                deleteFileSafe(download);
            }

            finally
            {
                ActiveDownloads.Remove(song);
            }
        }
Beispiel #3
0
        public static async Task PlaySongsAsync(Song song, List <Song> songs, bool forceClear = false)
        {
            if (song == null || songs == null || songs.Count == 0)
            {
                return;
            }
            if (!song.IsMatched)
            {
                return;
            }

            if (_cannotOpen)
            {
                return;
            }

            songs = songs.Where(p => p.IsMatched).ToList();

            if (songs.Count == 0)
            {
                ToastManager.ShowError("The songs haven't been matched yet.");
                return;
            }


            var skip    = songs.IndexOf(song);
            var ordered = songs.Skip(skip).ToList();

            ordered.AddRange(songs.Take(skip));

            var overflow = songs.Count - MaxMassPlayQueueCount;

            if (overflow > 0)
            {
                for (var i = 0; i < overflow; i++)
                {
                    ordered.Remove(ordered.LastOrDefault());
                }
            }

            if (_currentlyPreparing)
            {
                // cancel the previous
                _currentlyPreparing = false;
                // wait for it to stop
                await Task.Delay(50);
            }

            _currentlyPreparing = true;

            try
            {
                await App.Locator.CollectionService.ClearQueueAsync().ConfigureAwait(false);
            }
            catch (SQLiteException)
            {
                // retry
                try
                {
                    App.Locator.CollectionService.ClearQueueAsync().Wait();
                }
                catch (SQLiteException)
                {
                    // quit
                    ToastManager.ShowError("Problem clearing the queue. You seem to be running low on storage.");
                    return;
                }
            }

            var queueSong = await App.Locator.CollectionService.AddToQueueAsync(song).ConfigureAwait(false);

            App.Locator.AudioPlayerHelper.PlaySong(queueSong);

            App.Locator.SqlService.BeginTransaction();
            for (var index = 1; index < ordered.Count; index++)
            {
                if (!_currentlyPreparing)
                {
                    break;
                }
                await App.Locator.CollectionService.AddToQueueAsync(ordered[index]).ConfigureAwait(false);
            }
            App.Locator.SqlService.Commit();
            _currentlyPreparing = false;
        }