Exemplo n.º 1
0
 private void RefreshCommands()
 {
     GoToNextPageCommand.NotifyCanExecuteChanged();
     GoToPreviousPageCommand.NotifyCanExecuteChanged();
     GoToNextTagPageCommand.NotifyCanExecuteChanged();
     GoToPreviousTagPageCommand.NotifyCanExecuteChanged();
 }
        private void SearchForRecentMixes()
        {
            var ui = TaskScheduler.FromCurrentSynchronizationContext();

            Task.Factory.StartNew(() =>
            {
                using (LoadingIndicatorService.EnterLoadingBlock())
                {
                    using (var session = new EightTracksSession(EightTracksModule.ApiKey))
                    {
                        session.SetHttpClientMaxResponseContentBufferSize(OneMegaByte);
                        var response = session.Query <Mixes>().GetMix(
                            sorting: global::EightTracks.Mixes.Sort.Random,
                            resultsPerPage: 25);
                        _currentPage   = response.Page;
                        _numberOfPages = response.TotalPages;
                        return(response.Mixes);
                    }
                }
            })
            .ContinueWith(t =>
            {
                GoToNextPageCommand.NotifyCanExecuteChanged();
                GoToPreviousPageCommand.NotifyCanExecuteChanged();

                if (t.Exception != null)
                {
                    Logger.Log("Error while fetching mixes: " + t.Exception, Category.Exception, Priority.Medium);
                    ToastService.Show("An error occurred while getting mixes");
                }
                else
                {
                    _tags.Clear();

                    if (t.Result == null)
                    {
                        return;
                    }

                    foreach (var mix in t.Result)
                    {
                        foreach (var tag in mix.TagListCacheAsArray)
                        {
                            var value = tag.Trim();
                            if (!_tags.Contains(value) && !_tagFilterList.Contains(value))
                            {
                                _tags.Add(value);
                            }
                        }
                    }

                    if (!t.Result.Any())
                    {
                        ToastService.Show("No results found");
                        return;
                    }

                    _mixes.Clear();

                    foreach (var mix in t.Result)
                    {
                        _mixes.Add(mix);
                    }
                }
            }, ui);
        }
        private void SearchForMixes(string text, string type)
        {
            _tagFilterList.Clear();

            if (type == "Tag")
            {
                _tagFilterList.Add(text);
            }

            var ui = TaskScheduler.FromCurrentSynchronizationContext();

            Task.Factory.StartNew(() =>
            {
                using (LoadingIndicatorService.EnterLoadingBlock())
                {
                    using (var session = new EightTracksSession(EightTracksModule.ApiKey))
                    {
                        session.SetHttpClientMaxResponseContentBufferSize(OneMegaByte);
                        if (type == "Tag")
                        {
                            var response   = session.Query <Mixes>().GetMix(tag: text);
                            _currentPage   = response.Page;
                            _numberOfPages = response.TotalPages;
                            return(response.Mixes);
                        }
                        else
                        {
                            var response   = session.Query <Mixes>().GetMix(filter: text);
                            _currentPage   = response.Page;
                            _numberOfPages = response.TotalPages;
                            return(response.Mixes);
                        }
                    }
                }
            })
            .ContinueWith(t =>
            {
                GoToNextPageCommand.NotifyCanExecuteChanged();
                GoToPreviousPageCommand.NotifyCanExecuteChanged();

                _tags.Clear();

                foreach (var mix in t.Result)
                {
                    foreach (var tag in mix.TagListCacheAsArray)
                    {
                        var value = tag.Trim();
                        if (!_tags.Contains(value) && !_tagFilterList.Contains(value))
                        {
                            _tags.Add(value);
                        }
                    }
                }

                _mixes.Clear();
                foreach (var mix in t.Result)
                {
                    _mixes.Add(mix);
                }
            }, ui);
        }
        protected virtual void SearchForMixes(Mixes.Sort sortType, int page = 1)
        {
            Task.Factory.StartNew(() =>
            {
                using (LoadingIndicatorService.EnterLoadingBlock())
                {
                    using (var session = new EightTracksSession(EightTracksModule.ApiKey))
                    {
                        session.SetHttpClientMaxResponseContentBufferSize(OneMegaByte);
                        var response = session.Query <Mixes>().GetMix(
                            sorting: sortType,
                            filter: String.Join("+", TagFilterList),
                            page: page,
                            resultsPerPage: 25);

                        if (response != null)
                        {
                            _currentPage   = response.Page;
                            _numberOfPages = response.TotalPages;
                            return(response.Mixes);
                        }
                    }
                }

                return(null);
            })
            .ContinueWith(t =>
            {
                GoToNextPageCommand.NotifyCanExecuteChanged();
                GoToPreviousPageCommand.NotifyCanExecuteChanged();

                if (t.Exception != null)
                {
                    ToastService.Show(t.Exception.Message);
                    Logger.Log(t.Exception.ToString(), Category.Exception, Priority.Medium);
                    return;
                }

                if (t.Result != null)
                {
                    _tags.Clear();

                    foreach (var mix in t.Result)
                    {
                        foreach (var tag in mix.TagListCacheAsArray)
                        {
                            var value = tag.Trim();
                            if (!_tags.Contains(value) && !_tagFilterList.Contains(value))
                            {
                                _tags.Add(value);
                            }
                        }
                    }

                    if (!t.Result.Any() && SortType != global::EightTracks.Mixes.Sort.Random)
                    {
                        ToastService.Show("No results found");
                        return;
                    }

                    _mixes.Clear();

                    foreach (var mix in t.Result)
                    {
                        _mixes.Add(mix);
                    }
                }
                else
                {
                    // TODO : Notify user
                }
            }, _ui);
        }