Esempio n. 1
0
        public async Task LoadPackages(bool forceCheckForOutdatedPackages)
        {
            try
            {
                if (!CanLoadRemotePackages() && Packages.Any())
                {
                    return;
                }

                if (!HasLoaded && (_configService.GetEffectiveConfiguration().PreventPreload ?? false))
                {
                    ShowShouldPreventPreloadMessage = true;
                    HasLoaded = true;
                    return;
                }

                HasLoaded = false;
                ShowShouldPreventPreloadMessage = false;

                var sort = SortSelection == Resources.RemoteSourceViewModel_SortSelectionPopularity ? "DownloadCount" : "Title";

                await _progressService.StartLoading(string.Format(Resources.RemoteSourceViewModel_LoadingPage, CurrentPage));

                _progressService.WriteMessage(Resources.RemoteSourceViewModel_FetchingPackages);

                try
                {
                    var result =
                        await
                        _chocolateyPackageService.Search(
                            SearchQuery,
                            new PackageSearchOptions(
                                PageSize,
                                CurrentPage - 1,
                                sort,
                                IncludePrerelease,
                                IncludeAllVersions,
                                MatchWord,
                                Source.Value));

                    var installed = await _chocolateyPackageService.GetInstalledPackages();

                    var outdated = await _chocolateyPackageService.GetOutdatedPackages(false, null, forceCheckForOutdatedPackages);

                    PageCount = (int)Math.Ceiling((double)result.TotalCount / (double)PageSize);
                    Packages.Clear();

                    SourceType sourceType = SourceType.normal;
                    Enum.TryParse <SourceType>(Source.Value, out sourceType);

                    result.Packages.ToList().ForEach(p =>
                    {
                        if (installed.Any(package => string.Equals(package.Id, p.Id, StringComparison.OrdinalIgnoreCase)))
                        {
                            p.IsInstalled = true;
                        }
                        if (outdated.Any(package => string.Equals(package.Id, p.Id, StringComparison.OrdinalIgnoreCase)))
                        {
                            p.IsLatestVersion = false;
                        }

                        var pnew        = Mapper.Map <IPackageViewModel>(p);
                        pnew.SourceType = sourceType;
                        Packages.Add(pnew);
                    });

                    if (_configService.GetEffectiveConfiguration().ExcludeInstalledPackages ?? false)
                    {
                        Packages.RemoveAll(x => x.IsInstalled);
                    }

                    if (PageCount < CurrentPage)
                    {
                        CurrentPage = PageCount == 0 ? 1 : PageCount;
                    }
                }
                finally
                {
                    await _progressService.StopLoading();

                    HasLoaded = true;
                }

                await _eventAggregator.PublishOnUIThreadAsync(new ResetScrollPositionMessage());
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Failed to load new packages.");
                await _dialogService.ShowMessageAsync(
                    Resources.RemoteSourceViewModel_FailedToLoad,
                    string.Format(Resources.RemoteSourceViewModel_FailedToLoadRemotePackages, ex.Message));

                throw;
            }
        }
        public async Task LoadPackages()
        {
            try
            {
                if (!CanLoadRemotePackages() && Packages.Any())
                {
                    return;
                }

                _hasLoaded = false;

                var sort = SortSelection == Resources.RemoteSourceViewModel_SortSelectionPopularity ? "DownloadCount" : "Title";

                await _progressService.StartLoading(string.Format(Resources.RemoteSourceViewModel_LoadingPage, CurrentPage));

                _progressService.WriteMessage(Resources.RemoteSourceViewModel_FetchingPackages);

                try
                {
                    var result =
                        await
                        _chocolateyPackageService.Search(
                            SearchQuery,
                            new PackageSearchOptions(
                                PageSize,
                                CurrentPage - 1,
                                sort,
                                IncludePrerelease,
                                IncludeAllVersions,
                                MatchWord,
                                Source.Value));

                    var installed = await _chocolateyPackageService.GetInstalledPackages();

                    var outdated = await _chocolateyPackageService.GetOutdatedPackages();

                    PageCount = (int)(((double)result.TotalCount / (double)PageSize) + 0.5);
                    Packages.Clear();
                    result.Packages.ToList().ForEach(p =>
                    {
                        if (installed.Any(package => string.Equals(package.Id, p.Id, StringComparison.OrdinalIgnoreCase)))
                        {
                            p.IsInstalled = true;
                        }
                        if (outdated.Any(package => string.Equals(package.Item1, p.Id, StringComparison.OrdinalIgnoreCase)))
                        {
                            p.IsLatestVersion = false;
                        }

                        Packages.Add(Mapper.Map <IPackageViewModel>(p));
                    });

                    if (PageCount < CurrentPage)
                    {
                        CurrentPage = PageCount == 0 ? 1 : PageCount;
                    }
                }
                finally
                {
                    await _progressService.StopLoading();

                    _hasLoaded = true;
                }

                await _eventAggregator.PublishOnUIThreadAsync(new ResetScrollPositionMessage());
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Failed to load new packages.");
                await _progressService.ShowMessageAsync(
                    Resources.RemoteSourceViewModel_FailedToLoad,
                    string.Format(Resources.RemoteSourceViewModel_FailedToLoadRemotePackages, ex.Message));

                throw;
            }
        }