internal void FilterItems(ItemFilter itemFilter, CancellationToken token)
        {
            if (!Items.Contains(_loadingStatusIndicator))
            {
                Items.Add(_loadingStatusIndicator);
            }
            _loadingStatusIndicator.Status = LoadingStatus.Loading;

            // If there is another async loading process - cancel it.
            var loadCts = CancellationTokenSource.CreateLinkedTokenSource(token);

            Interlocked.Exchange(ref _loadCts, loadCts)?.Cancel();

            try
            {
                if (itemFilter == ItemFilter.UpdatesAvailable)
                {
                    ApplyUIFilterForUpdatesAvailable();
                }
                else
                {
                    //Show all the items, without an Update filter.
                    ClearUIFilter();
                }
            }
            finally
            {
                //If no items are shown in the filter, indicate in the list that no packages are found.
                if (FilteredItemsCount == 0)
                {
                    _loadingStatusIndicator.Status = LoadingStatus.NoItemsFound;
                }
                else
                {
                    if (Items.Contains(_loadingStatusIndicator))
                    {
                        Items.Remove(_loadingStatusIndicator);
                    }
                }
            }

            UpdateCheckBoxStatus();

            LoadItemsCompleted?.Invoke(this, EventArgs.Empty);
        }
Exemple #2
0
        private void LoadItems(PackageItemListViewModel selectedPackageItem, CancellationToken token)
        {
            // If there is another async loading process - cancel it.
            var loadCts = CancellationTokenSource.CreateLinkedTokenSource(token);

            Interlocked.Exchange(ref _loadCts, loadCts)?.Cancel();

            var currentLoader = _loader;

            _joinableTaskFactory.Value.RunAsync(async() =>
            {
                await TaskScheduler.Default;

                var addedLoadingIndicator = false;

                try
                {
                    // add Loading... indicator if not present
                    if (!Items.Contains(_loadingStatusIndicator))
                    {
                        Items.Add(_loadingStatusIndicator);
                        addedLoadingIndicator = true;
                    }

                    await LoadItemsCoreAsync(currentLoader, loadCts.Token);

                    await _joinableTaskFactory.Value.SwitchToMainThreadAsync();

                    if (selectedPackageItem != null)
                    {
                        UpdateSelectedItem(selectedPackageItem);
                    }
                }
                catch (OperationCanceledException) when(!loadCts.IsCancellationRequested)
                {
                    loadCts.Cancel();
                    loadCts.Dispose();
                    currentLoader.Reset();

                    await _joinableTaskFactory.Value.SwitchToMainThreadAsync();

                    // The user cancelled the login, but treat as a load error in UI
                    // So the retry button and message is displayed
                    // Do not log to the activity log, since it is not a NuGet error
                    _logger.Log(ProjectManagement.MessageLevel.Error, Resx.Resources.Text_UserCanceled);

                    _loadingStatusIndicator.SetError(Resx.Resources.Text_UserCanceled);


                    _loadingStatusBar.SetCancelled();
                    _loadingStatusBar.Visibility = Visibility.Visible;
                }
                catch (Exception ex) when(!loadCts.IsCancellationRequested)
                {
                    loadCts.Cancel();
                    loadCts.Dispose();
                    currentLoader.Reset();

                    // Write stack to activity log
                    Mvs.ActivityLog.LogError(LogEntrySource, ex.ToString());

                    await _joinableTaskFactory.Value.SwitchToMainThreadAsync();

                    var errorMessage = ExceptionUtilities.DisplayMessage(ex);
                    _logger.Log(ProjectManagement.MessageLevel.Error, errorMessage);

                    _loadingStatusIndicator.SetError(errorMessage);

                    _loadingStatusBar.SetError();
                    _loadingStatusBar.Visibility = Visibility.Visible;
                }
                finally
                {
                    if (_loadingStatusIndicator.Status != LoadingStatus.NoItemsFound &&
                        _loadingStatusIndicator.Status != LoadingStatus.ErrorOccurred)
                    {
                        // Ideally, After a serach, it should report its status and,
                        // do not keep the LoadingStatus.Loading forever.
                        // This is a workaround.
                        var emptyListCount = addedLoadingIndicator ? 1 : 0;
                        if (Items.Count == emptyListCount)
                        {
                            _loadingStatusIndicator.Status = LoadingStatus.NoItemsFound;
                        }
                        else
                        {
                            Items.Remove(_loadingStatusIndicator);
                        }
                    }
                }

                UpdateCheckBoxStatus();

                LoadItemsCompleted?.Invoke(this, EventArgs.Empty);
            });
        }
        private void LoadItems(PackageItemListViewModel selectedPackageItem, CancellationToken token)
        {
            // If there is another async loading process - cancel it.
            var loadCts = CancellationTokenSource.CreateLinkedTokenSource(token);

            Interlocked.Exchange(ref _loadCts, loadCts)?.Cancel();

            var currentLoader = _loader;

            _joinableTaskFactory.Value.RunAsync(async() =>
            {
                await TaskScheduler.Default;

                try
                {
                    await LoadItemsCoreAsync(currentLoader, loadCts.Token);

                    await _joinableTaskFactory.Value.SwitchToMainThreadAsync();

                    if (selectedPackageItem != null)
                    {
                        UpdateSelectedItem(selectedPackageItem);
                    }
                }
                catch (OperationCanceledException) when(!loadCts.IsCancellationRequested)
                {
                    loadCts.Cancel();
                    loadCts.Dispose();
                    currentLoader.Reset();

                    await _joinableTaskFactory.Value.SwitchToMainThreadAsync();

                    // The user cancelled the login, but treat as a load error in UI
                    // So the retry button and message is displayed
                    // Do not log to the activity log, since it is not a NuGet error
                    _logger.Log(ProjectManagement.MessageLevel.Error, Resx.Resources.Text_UserCanceled);

                    _loadingStatusIndicator.SetError(Resx.Resources.Text_UserCanceled);

                    _loadingStatusBar.SetCancelled();
                    _loadingStatusBar.Visibility = Visibility.Visible;
                }
                catch (Exception ex) when(!loadCts.IsCancellationRequested)
                {
                    loadCts.Cancel();
                    loadCts.Dispose();
                    currentLoader.Reset();

                    // Write stack to activity log
                    Mvs.ActivityLog.LogError(LogEntrySource, ex.ToString());

                    await _joinableTaskFactory.Value.SwitchToMainThreadAsync();

                    var errorMessage = ExceptionUtilities.DisplayMessage(ex);
                    _logger.Log(ProjectManagement.MessageLevel.Error, errorMessage);

                    _loadingStatusIndicator.SetError(errorMessage);

                    _loadingStatusBar.SetError();
                    _loadingStatusBar.Visibility = Visibility.Visible;
                }

                UpdateCheckBoxStatus();

                LoadItemsCompleted?.Invoke(this, EventArgs.Empty);
            });
        }