Немного валидации настроек, например, при появлении новой RountToMinutes, делили на 0 и крэш

This commit is contained in:
rutriff
2019-03-18 11:34:09 +03:00
parent 0fcce1bf1c
commit e8213d940f
4 changed files with 107 additions and 17 deletions
+83 -3
View File
@@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -19,6 +21,70 @@ namespace JiraTimeBotForm.Configuration
public TimeSpan ActivationTime { get; set; } public TimeSpan ActivationTime { get; set; }
public bool AddCommentsToWorklog { get; set; } public bool AddCommentsToWorklog { get; set; }
public static Settings LoadAndCheck(Action settingsSource, Action<string> errorMessageReporter)
{
Settings settings;
bool settingsValid;
do
{
settings = Load();
settingsValid = SettingsIsValid(settings, out var message);
if (!settingsValid)
{
if (!string.IsNullOrEmpty(message))
{
errorMessageReporter(message);
}
settingsSource();
}
} while (!settingsValid);
return settings;
}
private static bool SettingsIsValid(Settings settings, out string message)
{
if (settings == null)
{
message = "Заполните настройки";
return false;
}
var errors = new List<string>();
message = null;
if (settings.RountToMinutes == default(int))
{
errors.Add("Укажите округление времени, например, 10 минут");
}
if (string.IsNullOrEmpty(settings.JiraPassword))
{
errors.Add("Укажите Password от Jira");
}
if (string.IsNullOrEmpty(settings.JiraUserName))
{
errors.Add("Укажите UserName от Jira");
}
if (string.IsNullOrEmpty(settings.RepositoryPath) || !Directory.Exists(settings.RepositoryPath))
{
errors.Add("Укажите RepositoryPath");
}
var hasErrors = errors.Any();
if (hasErrors)
{
message = string.Join(Environment.NewLine, errors);
}
return !hasErrors;
}
public static Settings Load() public static Settings Load()
{ {
@@ -29,10 +95,24 @@ namespace JiraTimeBotForm.Configuration
} }
var settingsText = File.ReadAllText(settingsPath); var settingsText = File.ReadAllText(settingsPath);
var settings = JsonConvert.DeserializeObject<Settings>(settingsText);
var password = new PasswordEncryptionClass().Decrypt(settings.JiraUserName, settings.JiraPassword, settings.JiraUrl); Settings settings;
settings.JiraPassword = password;
try
{
settings = JsonConvert.DeserializeObject<Settings>(settingsText);
if (settings == null)
{
return null;
}
var password = new PasswordEncryptionClass().Decrypt(settings.JiraUserName, settings.JiraPassword, settings.JiraUrl);
settings.JiraPassword = password;
}
catch (JsonReaderException)
{
return null;
}
return settings; return settings;
} }
-1
View File
@@ -67,7 +67,6 @@
// //
// tmrStart // tmrStart
// //
this.tmrStart.Enabled = true;
this.tmrStart.Interval = 1500; this.tmrStart.Interval = 1500;
this.tmrStart.Tick += new System.EventHandler(this.tmrStart_Tick); this.tmrStart.Tick += new System.EventHandler(this.tmrStart_Tick);
// //
+17 -5
View File
@@ -1,6 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.Net.Configuration;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@@ -25,6 +25,8 @@ namespace JiraTimeBotForm.UI
private readonly ITrayMenu _trayIcon; private readonly ITrayMenu _trayIcon;
private CancellationTokenSource _tokenSource; private CancellationTokenSource _tokenSource;
private Settings _settings; private Settings _settings;
private readonly Action _settingsWindowShow;
private readonly Action<string> _settingsErrorReporter;
private CancellationTokenSource GetTokenSource() private CancellationTokenSource GetTokenSource()
{ {
@@ -46,13 +48,19 @@ namespace JiraTimeBotForm.UI
_trayIcon = _container.Resolve<ITrayMenu>(); _trayIcon = _container.Resolve<ITrayMenu>();
_trayIcon.Create(this); _trayIcon.Create(this);
_settings = Settings.Load();
_job = _container.Resolve<Job>(); _job = _container.Resolve<Job>();
_log = _container.Resolve<ILog>(); _log = _container.Resolve<ILog>();
_tasksProcessors = _container.Resolve<IAllTasksProcessors>(); _tasksProcessors = _container.Resolve<IAllTasksProcessors>();
_controls = new Control[] { txtDummyMode, btnStart, btnMeeting, btnSettings }; _controls = new Control[] { txtDummyMode, btnStart, btnMeeting, btnSettings };
_settingsWindowShow = () =>
{
var frmSettings = new frmSettings();
frmSettings.ShowDialog(this);
};
_settingsErrorReporter = msg => MessageBox.Show(msg, "Загрузка настроек", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);;
} }
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
@@ -62,7 +70,7 @@ namespace JiraTimeBotForm.UI
public Settings ReadSettingsAndLock() public Settings ReadSettingsAndLock()
{ {
_settings = Settings.Load(); _settings = Settings.LoadAndCheck(_settingsWindowShow, _settingsErrorReporter);
LockUnlock(false); LockUnlock(false);
@@ -79,9 +87,13 @@ namespace JiraTimeBotForm.UI
btnCancel.Enabled = !enabled; btnCancel.Enabled = !enabled;
tmrStart.Enabled = enabled; tmrStart.Enabled = enabled;
} }
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
_settings = Settings.Load(); _settings = Settings.LoadAndCheck(_settingsWindowShow, _settingsErrorReporter);
tmrStart.Enabled = true;
_log.Info($"Загружен бот для {_settings.JiraUserName}, работаем в {_settings.RepositoryPath}");
} }
private async void btnStart_Click(object sender, EventArgs e) private async void btnStart_Click(object sender, EventArgs e)
+7 -8
View File
@@ -1,11 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using JiraTimeBotForm.Configuration; using JiraTimeBotForm.Configuration;
@@ -21,7 +14,10 @@ namespace JiraTimeBotForm.UI
private void frmSettings_Load(object sender, EventArgs e) private void frmSettings_Load(object sender, EventArgs e)
{ {
var settings = Settings.Load(); var settings = Settings.Load();
SetSettings(settings); if (settings != null)
{
SetSettings(settings);
}
} }
private void SetSettings(Settings settings) private void SetSettings(Settings settings)
@@ -64,7 +60,10 @@ namespace JiraTimeBotForm.UI
{ {
var settings = ReadSettingsAndLock(); var settings = ReadSettingsAndLock();
settings.Save(); settings.Save();
MessageBox.Show("Настройки сохранены."); MessageBox.Show("Настройки сохранены.");
DialogResult = DialogResult.OK;
this.Close(); this.Close();
} }