Example #1
0
        public async void Save()
        {
            if (string.IsNullOrWhiteSpace(DraftSource.Id))
            {
                await _progressService.ShowMessageAsync(Resources.SettingsViewModel_SavingSource, Resources.SettingsViewModel_SourceMissingId);

                return;
            }

            if (string.IsNullOrWhiteSpace(DraftSource.Value))
            {
                await _progressService.ShowMessageAsync(Resources.SettingsViewModel_SavingSource, Resources.SettingsViewModel_SourceMissingValue);

                return;
            }

            await _progressService.StartLoading(Resources.SettingsViewModel_SavingSourceLoading);

            try
            {
                if (_isNewItem)
                {
                    if (DraftSource.Id == ChocolateyLicensedSourceId)
                    {
                        await _progressService.StopLoading();

                        await _progressService.ShowMessageAsync(Resources.SettingsViewModel_SavingSource, Resources.SettingsViewModel_InvalidSourceId);

                        return;
                    }

                    await _chocolateyService.AddSource(DraftSource);

                    _isNewItem = false;
                    Sources.Add(DraftSource);
                    NotifyOfPropertyChange(nameof(CanRemove));
                }
                else
                {
                    if (DraftSource.Id == ChocolateyLicensedSourceId)
                    {
                        if (DraftSource.Disabled)
                        {
                            await _chocolateyService.DisableSource(DraftSource.Id);
                        }
                        else
                        {
                            await _chocolateyService.EnableSource(DraftSource.Id);
                        }
                    }
                    else
                    {
                        await _chocolateyService.UpdateSource(_originalId, DraftSource);
                    }

                    Sources[Sources.IndexOf(SelectedSource)] = DraftSource;
                }

                _originalId = DraftSource?.Id;
                await _eventAggregator.PublishOnUIThreadAsync(new SourcesUpdatedMessage());
            }
            catch (UnauthorizedAccessException)
            {
                await _progressService.ShowMessageAsync(
                    Resources.General_UnauthorisedException_Title,
                    Resources.General_UnauthorisedException_Description);
            }
            finally
            {
                SelectedSource = null;
                await _progressService.StopLoading();
            }
        }