private async Task UpdatePreviewAsync(string envName, string envFilePath, CancellationToken ct)
 {
     try {
         ct.ThrowIfCancellationRequested();
         string result;
         try {
             result = File.ReadAllText(envFilePath);
         } catch (UnauthorizedAccessException ex) {
             result = ex.Message;
         } catch (ArgumentException ex) {
             result = ex.Message;
         } catch (FileNotFoundException ex) {
             result = ex.Message;
         } catch (IOException ex) {
             result = ex.Message;
         }
         ct.ThrowIfCancellationRequested();
         Dispatcher.Invoke(() => {
             var p             = new CondaEnvironmentPreview();
             p.IsInProgress    = false;
             p.IsEnvFile       = result != null;
             p.IsNoEnvFile     = result == null;
             p.EnvFileContents = result ?? "";
             CondaPreview      = p;
         });
     } catch (OperationCanceledException) {
     }
 }
        protected override void ResetProjectDependentProperties()
        {
            _suppressPreview = true;

            SetAsCurrent        = SetAsCurrent && SelectedProject != null;
            EnvName             = GetDefaultEnvName();
            Packages            = string.Empty;
            SelectedEnvFilePath = SelectedProject?.EnvironmentYmlPath;
            if (!string.IsNullOrEmpty(SelectedEnvFilePath))
            {
                IsPackages = false;
                IsEnvFile  = true;
            }
            else
            {
                IsPackages = true;
                IsEnvFile  = false;
            }

            CondaPreview = new CondaEnvironmentPreview();

            _suppressPreview = false;

            TriggerDelayedPreview();
        }
        private async Task UpdatePreviewAsync(string envName, PackageSpec[] specs, CancellationToken ct)
        {
            if (specs.Length == 0)
            {
                Dispatcher.Invoke(() => {
                    CondaPreview = new CondaEnvironmentPreview()
                    {
                        IsNoPackages = true
                    };
                });
                return;
            }

            try {
                ct.ThrowIfCancellationRequested();
                var result = await _condaMgr.PreviewCreateAsync(envName, specs, ct);

                ct.ThrowIfCancellationRequested();
                var preview = result?.Actions?.LinkPackages;
                var msg     = result?.Message;
                Dispatcher.Invoke(() => {
                    var p = new CondaEnvironmentPreview();
                    if (preview != null)
                    {
                        foreach (var package in preview.MaybeEnumerate().OrderBy(pkg => pkg.Name))
                        {
                            p.Packages.Add(new CondaPackageView()
                            {
                                Name    = package.Name.Trim(),
                                Version = package.VersionText,
                            });
                        }
                    }

                    if (result == null)
                    {
                        msg = Strings.AddCondaEnvironmentPreviewFailed;
                    }
                    p.HasPreviewError = !string.IsNullOrEmpty(msg);
                    p.ErrorMessage    = msg;
                    p.IsInProgress    = false;
                    p.IsPackages      = p.Packages.Count > 0;
                    p.IsNoPackages    = !p.IsPackages && !p.HasPreviewError;

                    CondaPreview = p;
                });
            } catch (OperationCanceledException) {
            }
        }
        private void UpdatePreview()
        {
            if (_suppressPreview)
            {
                return;
            }

            if (_condaMgr == null)
            {
                return;
            }

            if (IsEnvFile && IsPackages)
            {
                // Temporary state while switching from one combo box to the other
                // We'll get called again with only one of them set to true
                return;
            }

            _updatePreviewCancelTokenSource?.Cancel();
            _updatePreviewCancelTokenSource = new CancellationTokenSource();

            // Clear the preview, display a progress icon/message
            var p = new CondaEnvironmentPreview();

            p.IsInProgress = true;
            CondaPreview   = p;

            var envName = EnvName ?? string.Empty;

            if (IsEnvFile)
            {
                var envFilePath = SelectedEnvFilePath ?? string.Empty;
                Task.Run(() => UpdatePreviewAsync(envName, envFilePath, _updatePreviewCancelTokenSource.Token)).HandleAllExceptions(Site, typeof(AddCondaEnvironmentView)).DoNotWait();
            }
            else if (IsPackages)
            {
                var specs = PackagesSpecs.ToArray();
                Task.Run(() => UpdatePreviewAsync(envName, specs, _updatePreviewCancelTokenSource.Token)).HandleAllExceptions(Site, typeof(AddCondaEnvironmentView)).DoNotWait();
            }
        }