Esempio n. 1
0
        private async void SaveAPIButton_Click(Object sender, EventArgs e)
        {
            if (!_apiTextBox.CheckValidFormat())
            {
                return;
            }

            if (_apiTextBox.Text != Globals.APIKey.GetValue())
            {
                String apiKey = _apiTextBox.Text;


                Boolean apiTextBoxCurrentEnabled      = _apiTextBox.Enabled;
                Boolean saveAPIButtonCurrentEnabled   = _saveAPIButton.Enabled;
                Boolean changeAPIButtonCurrentEnabled = _changeAPIButton.Enabled;
                Boolean resetAPIButtonCurrentEnabled  = _resetAPIButton.Enabled;
                DisableButtons();

                void RestoreButtonsEnabledState()
                {
                    _apiTextBox.Enabled      = apiTextBoxCurrentEnabled;
                    _saveAPIButton.Enabled   = saveAPIButtonCurrentEnabled;
                    _changeAPIButton.Enabled = changeAPIButtonCurrentEnabled;
                    _resetAPIButton.Enabled  = resetAPIButtonCurrentEnabled;
                }

                if (!await DerpiAPI.CheckValidAPIAsync(apiKey).ConfigureAwait(true))
                {
                    RestoreButtonsEnabledState();
                    ChangeAPI();
                    new MessageForm(Globals.Localization.EnteredAPIKeyInvalid, Globals.Localization.APIKeyInvalid, Images.Basic.Error, Images.Basic.Error)
                    .ShowDialog();
                    return;
                }

                RestoreButtonsEnabledState();
                Globals.APIKey.SetValue(apiKey);
            }

            _saveAPIButton.Enabled = false;
            _apiTextBox.Enabled    = false;
        }