Exemplo n.º 1
0
        private async Task RefreshContent()
        {
            try
            {
                var respond = await API.GetLevelList();

                var respond_contest = await API.GetContestLevelList();

                var selections = new List <ChartSelection>();
                foreach (var level in respond.Levels)
                {
                    var item = new ChartSelection()
                    {
                        Charter       = level.Designer,
                        DownloadCount = level.DownloadCount,
                        Artist        = level.SongArtist,
                        SongName      = level.Title,
                        GUID          = level.Guid,
                        Index         = selections.Count + 1,
                        Rate          = level.Rating
                    };

                    if (respond_contest.Levels.Exists((x) => x.Guid.Equals(item.GUID)))
                    {
                        item.ParticipantCurrentContest = true;
                    }

                    selections.Add(item);
                }

                await Task.Run(() =>
                {
                    Parallel.ForEach(respond.Levels, async level =>
                    {
                        var image = await DownloadImage(level.Guid);
                        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
                        {
                            var item      = selections.Find(x => x.GUID.Equals(level.Guid));
                            item.CoverURL = image;
                        }));
                    });
                });

                if (ChartList.ItemsSource != null)
                {
                    (ChartList.ItemsSource as List <ChartSelection>).Clear();
                    ChartList.Items.SortDescriptions.Clear();
                }

                ChartList.ItemsSource = selections;

                ICollectionView view = CollectionViewSource.GetDefaultView(ChartList.ItemsSource);
                view.Filter = (o) =>
                {
                    bool           flag = false;
                    ChartSelection item = o as ChartSelection;
                    if (CheckBox_ContestChart.IsChecked.Value)
                    {
                        flag = item.ParticipantCurrentContest;
                    }
                    else
                    {
                        flag = true;
                    }

                    if (flag)
                    {
                        if (SearchBar.Text.Length > 0)
                        {
                            string query = SearchBar.Text.ToLower();
                            return(item.SongName.ToLower().Contains(query) ||
                                   item.Artist.ToLower().Contains(query) ||
                                   item.Charter.ToLower().Contains(query));
                        }
                        return(true);
                    }
                    return(false);
                };
            }
            catch (LayestaWebAPINeedLoginException ex)
            {
                await TryLogin(true);
                await RefreshContent();
            }
            catch (Exception ex)
            {
                ErrorWindow.ShowException(ex);
            }
        }