public void EnterLoadingBlock_RevertsState()
        {
            using (_indicatorService.EnterLoadingBlock())
            {
                Assert.IsTrue(_indicatorService.IsLoading);
            }

            Assert.IsFalse(_indicatorService.IsLoading);
        }
示例#2
0
        public void Play(ITrackStream trackStream)
        {
            _trackQueue = new ConcurrentQueue <Track>();
            _dispatcher.BeginInvoke(new Action(_trackQueuePublic.Clear));

            CurrentTrackStream = trackStream;

            try
            {
                if (_currentTokenSource != null && !_currentTokenSource.IsCancellationRequested)
                {
                    _currentTokenSource.Cancel();
                }
            }
            catch (Exception e)
            {
                _logger.Log(e.ToString(), Category.Exception, Priority.Low);
            }

            _currentTokenSource = new CancellationTokenSource();
            var tct = _currentTokenSource.Token;

            Task
            .Factory
            .StartNew(() =>
            {
                using (_loadingIndicatorService.EnterLoadingBlock())
                {
                    GetNextBatch(tct);

                    _corePlayer.Stop();

                    while (!MoveToNextTrack())
                    {
                        if (!GetNextBatch(tct))
                        {
                            break;
                        }
                    }

                    PeekToNextTrack();
                }
            }, tct)
            .ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    task.Exception.Handle(e =>
                    {
                        _toastService.Show("Error while playing track.");
                        _logger.Log(e.ToString(), Category.Exception, Priority.High);
                        return(true);
                    });
                }
            });
        }
        private Task <IEnumerable <Track> > GetArtistTracksAsync(string artistName)
        {
            return(Task <IEnumerable <Track> > .Factory
                   .StartNew(state =>
            {
                string name = state.ToString();
                using (_loadingIndicator.EnterLoadingBlock())
                {
                    var result = _radio.GetTracksByName(name)
                                 .Where(t => t.Artist.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                                 .ToArray();

                    return result;
                }
            }, artistName));
        }
        void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
        {
            Task.Factory
            .StartNew(() =>
            {
                var response = MemoryCache.Default.Get("TopHotArtists") as TopHotttResponse;

                if (response == null)
                {
                    using (_loadingIndicator.EnterLoadingBlock())
                    {
                        using (var session = new EchoNestSession(EchoNestModule.ApiKey))
                        {
                            response = session.Query <TopHottt>().Execute(99, bucket:
                                                                          ArtistBucket.Images |
                                                                          ArtistBucket.Songs);

                            if (response != null)
                            {
                                MemoryCache
                                .Default
                                .Add("TopHotArtists", response, DateTimeOffset.Now.AddHours(6));

                                return(response);
                            }
                        }
                    }
                }

                return(response);
            })
            .ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    _logger.Log(task.Exception.ToString(), Category.Exception, Priority.Medium);
                    _toastService.Show("Unable to fetch artists");
                }
                else
                {
                    if (task.Result != null)
                    {
                        if (task.Result.Status.Code == ResponseCode.Success)
                        {
                            var artists = task.Result.Artists.Select((a, i) =>
                            {
                                string name  = a.Name;
                                string image = a.Images.Count > 0 ? a.Images[0].Url : null;
                                string song  = a.Songs.Count > 0 ? a.Songs[0].Title : null;
                                return(new HotArtistModel((i + 1), name, image, song));
                            });

                            const int numberOfObjectsPerPage = 10;
                            int numberOfObjectsTaken         = 0;
                            int count      = artists.Count();
                            int pageNumber = 0;

                            while (numberOfObjectsTaken < count)
                            {
                                IEnumerable <HotArtistModel> queryResultPage = artists
                                                                               .Skip(numberOfObjectsPerPage * pageNumber)
                                                                               .Take(numberOfObjectsPerPage).ToArray();

                                numberOfObjectsTaken += queryResultPage.Count();
                                pageNumber++;

                                _dispatcher
                                .BeginInvoke(
                                    new Action <IEnumerable <HotArtistModel> >(m => m.ForEach(model => _artists.Add(model))),
                                    DispatcherPriority.Background,
                                    queryResultPage);
                            }
                        }
                        else
                        {
                            _toastService.Show(task.Result.Status.Message);
                        }
                    }
                }
            });
        }
示例#5
0
        private void ExecuteStartRadio()
        {
            Task.Factory.StartNew(() =>
            {
                SelectedMoods.ForEach(s => s.Count  = s.Count + 1);
                SelectedStyles.ForEach(s => s.Count = s.Count + 1);

                using (var session = _documentStore.OpenSession())
                {
                    foreach (var selectedStyle in SelectedStyles)
                    {
                        var styleTerm = session.Load <StyleTerm>(selectedStyle.ID);

                        if (styleTerm != null)
                        {
                            styleTerm.Count = selectedStyle.Count;
                            session.Store(styleTerm);
                        }
                    }

                    foreach (var selectedMood in SelectedMoods)
                    {
                        var styleTerm = session.Load <MoodTerm>(selectedMood.ID);

                        if (styleTerm != null)
                        {
                            styleTerm.Count = selectedMood.Count;
                            session.Store(styleTerm);
                        }
                    }

                    session.SaveChanges();
                }
            })
            .ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    _logger.Log(task.Exception.ToString(), Category.Exception, Priority.Medium);
                }
            });

            Task.Factory.StartNew(() =>
            {
                using (_loadingIndicatorService.EnterLoadingBlock())
                {
                    using (var session = new EchoNestSession(EchoNestModule.ApiKey))
                    {
                        SearchArgument arg = new SearchArgument();
                        FillTermList(SelectedMoods, arg.Moods);
                        FillTermList(SelectedStyles, arg.Styles);
                        arg.MinFamiliarity = ArtistFamiliarity.Minimum;
                        arg.MinHotttnesss  = ArtistHotness.Minimum;

                        var response = session.Query <Search>().Execute(arg);

                        if (response == null)
                        {
                            _toastService.Show("Unable to generate playlist");
                            return;
                        }

                        if (response.Status.Code == ResponseCode.Success && response.Artists.Count > 0)
                        {
                            StaticArgument arg2 = new StaticArgument();
                            arg2.Results        = 75;
                            FillTermList(SelectedMoods, arg2.Moods);
                            FillTermList(SelectedStyles, arg2.Styles);
                            arg2.Type = "artist-radio";
                            arg2.Artist.Add(response.Artists[0].Name);
                            arg2.MinTempo             = Tempo.Minimum;
                            arg2.MinLoudness          = Loudness.Minimum;
                            arg2.MinDanceability      = Danceability.Minimum;
                            arg2.MinEnergy            = Energy.Minimum;
                            arg2.ArtistMinFamiliarity = ArtistFamiliarity.Minimum;
                            arg2.ArtistMinHotttnesss  = ArtistHotness.Minimum;
                            arg2.SongMinHotttnesss    = SongHotness.Minimum;

                            _radio.Play(new StyleTrackStream(arg2, _radio, _toastService));
                        }
                        else
                        {
                            if (response.Artists != null && response.Artists.Count == 0)
                            {
                                // TODO : Localize
                                _toastService.Show("Unable to find songs matching the current criterias");
                            }
                            else
                            {
                                _toastService.Show("EchoNest : " + response.Status.Message);
                            }
                        }
                    }
                }
            })
            .ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    _logger.Log(task.Exception.ToString(), Category.Exception, Priority.Medium);
                }
            });
        }