Рефакторинг + DI

This commit is contained in:
Danil Kislov
2019-03-15 17:49:58 +03:00
parent fd02201b42
commit d56094febb
19 changed files with 288 additions and 105 deletions
+65
View File
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JiraTimeBotForm.UI
{
public class TrayMenu: ITrayMenu
{
private NotifyIcon _trayIcon;
public NotifyIcon Create(frmMain form)
{
var trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
_trayIcon = new NotifyIcon
{
Text = "JiraTimeBot",
Icon = form.Icon,
ContextMenu = trayMenu,
Visible = false,
Tag = form
};
_trayIcon.Click += btnTray_Click;
_trayIcon.DoubleClick += btnTray_Click;
return _trayIcon;
}
public void Show()
{
_trayIcon.Visible = true;
}
public void Hide()
{
_trayIcon.Visible = false;
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
private void btnTray_Click(object sender, EventArgs e)
{
var icon = (NotifyIcon) sender;
var form = (frmMain) icon.Tag;
form.Visible = true;
form.Activate();
form.BringToFront();
Extensions.Extensions.Restore(form);
}
}
public interface ITrayMenu
{
NotifyIcon Create(frmMain form);
void Show();
void Hide();
}
}
+275
View File
@@ -0,0 +1,275 @@
namespace JiraTimeBotForm
{
partial class frmMain
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
this.actTime = new System.Windows.Forms.DateTimePicker();
this.txtJiraLogin = new System.Windows.Forms.TextBox();
this.lbl1 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txtJiraPassword = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtMercurialEmail = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.btnSave = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.txtRepoPath = new System.Windows.Forms.TextBox();
this.txtLog = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.ntfyIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.tmrStart = new System.Windows.Forms.Timer(this.components);
this.txtDummyMode = new System.Windows.Forms.CheckBox();
this.btnMeeting = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.chkAddComments = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// actTime
//
this.actTime.Format = System.Windows.Forms.DateTimePickerFormat.Time;
this.actTime.Location = new System.Drawing.Point(94, 120);
this.actTime.Name = "actTime";
this.actTime.Size = new System.Drawing.Size(100, 20);
this.actTime.TabIndex = 0;
//
// txtJiraLogin
//
this.txtJiraLogin.Location = new System.Drawing.Point(94, 42);
this.txtJiraLogin.Name = "txtJiraLogin";
this.txtJiraLogin.Size = new System.Drawing.Size(100, 20);
this.txtJiraLogin.TabIndex = 1;
//
// lbl1
//
this.lbl1.AutoSize = true;
this.lbl1.Location = new System.Drawing.Point(28, 45);
this.lbl1.Name = "lbl1";
this.lbl1.Size = new System.Drawing.Size(60, 13);
this.lbl1.TabIndex = 2;
this.lbl1.Text = "Логин Jira:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(21, 71);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(67, 13);
this.label1.TabIndex = 4;
this.label1.Text = "Пароль Jira:";
//
// txtJiraPassword
//
this.txtJiraPassword.Location = new System.Drawing.Point(94, 68);
this.txtJiraPassword.Name = "txtJiraPassword";
this.txtJiraPassword.PasswordChar = '*';
this.txtJiraPassword.Size = new System.Drawing.Size(100, 20);
this.txtJiraPassword.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(6, 97);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(82, 13);
this.label2.TabIndex = 6;
this.label2.Text = "Mercurial name:";
//
// txtMercurialEmail
//
this.txtMercurialEmail.Location = new System.Drawing.Point(94, 94);
this.txtMercurialEmail.Name = "txtMercurialEmail";
this.txtMercurialEmail.Size = new System.Drawing.Size(100, 20);
this.txtMercurialEmail.TabIndex = 5;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(45, 126);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(43, 13);
this.label3.TabIndex = 7;
this.label3.Text = "Время:";
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(82, 169);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 23);
this.btnSave.TabIndex = 8;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 19);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(76, 13);
this.label4.TabIndex = 10;
this.label4.Text = "Путь до репо:";
//
// txtRepoPath
//
this.txtRepoPath.Location = new System.Drawing.Point(94, 16);
this.txtRepoPath.Name = "txtRepoPath";
this.txtRepoPath.Size = new System.Drawing.Size(100, 20);
this.txtRepoPath.TabIndex = 9;
//
// txtLog
//
this.txtLog.Location = new System.Drawing.Point(218, 16);
this.txtLog.Multiline = true;
this.txtLog.Name = "txtLog";
this.txtLog.ReadOnly = true;
this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtLog.Size = new System.Drawing.Size(510, 264);
this.txtLog.TabIndex = 11;
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(9, 224);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(114, 23);
this.btnStart.TabIndex = 12;
this.btnStart.Text = "Проставить время";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// ntfyIcon
//
this.ntfyIcon.Text = "notifyIcon1";
this.ntfyIcon.Visible = true;
//
// tmrStart
//
this.tmrStart.Enabled = true;
this.tmrStart.Interval = 1500;
this.tmrStart.Tick += new System.EventHandler(this.tmrStart_Tick);
//
// txtDummyMode
//
this.txtDummyMode.AutoSize = true;
this.txtDummyMode.Location = new System.Drawing.Point(9, 201);
this.txtDummyMode.Name = "txtDummyMode";
this.txtDummyMode.Size = new System.Drawing.Size(114, 17);
this.txtDummyMode.TabIndex = 13;
this.txtDummyMode.Text = "Тестовый прогон";
this.txtDummyMode.UseVisualStyleBackColor = true;
//
// btnMeeting
//
this.btnMeeting.Location = new System.Drawing.Point(142, 224);
this.btnMeeting.Name = "btnMeeting";
this.btnMeeting.Size = new System.Drawing.Size(70, 23);
this.btnMeeting.TabIndex = 14;
this.btnMeeting.Text = "Митинг";
this.btnMeeting.UseVisualStyleBackColor = true;
this.btnMeeting.Click += new System.EventHandler(this.btnMeeting_Click);
//
// btnCancel
//
this.btnCancel.Enabled = false;
this.btnCancel.Location = new System.Drawing.Point(9, 256);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(203, 23);
this.btnCancel.TabIndex = 15;
this.btnCancel.Text = "Отменить операцию";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// chkAddComments
//
this.chkAddComments.AutoSize = true;
this.chkAddComments.Location = new System.Drawing.Point(61, 146);
this.chkAddComments.Name = "chkAddComments";
this.chkAddComments.Size = new System.Drawing.Size(133, 17);
this.chkAddComments.TabIndex = 16;
this.chkAddComments.Text = "Добавлять описание";
this.chkAddComments.UseVisualStyleBackColor = true;
//
// frmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(740, 292);
this.Controls.Add(this.chkAddComments);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnMeeting);
this.Controls.Add(this.txtDummyMode);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtLog);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtRepoPath);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtMercurialEmail);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtJiraPassword);
this.Controls.Add(this.lbl1);
this.Controls.Add(this.txtJiraLogin);
this.Controls.Add(this.actTime);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "frmMain";
this.Text = "Бот логирования времени";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMain_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.Resize += new System.EventHandler(this.frmMain_Resize);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.DateTimePicker actTime;
private System.Windows.Forms.TextBox txtJiraLogin;
private System.Windows.Forms.Label lbl1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtJiraPassword;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtMercurialEmail;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtRepoPath;
private System.Windows.Forms.TextBox txtLog;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.NotifyIcon ntfyIcon;
private System.Windows.Forms.Timer tmrStart;
private System.Windows.Forms.CheckBox txtDummyMode;
private System.Windows.Forms.Button btnMeeting;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.CheckBox chkAddComments;
}
}
+215
View File
@@ -0,0 +1,215 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autofac;
using JiraTimeBotForm.Configuration;
using JiraTimeBotForm.DI;
using JiraTimeBotForm.Mercurial;
using JiraTimeBotForm.TasksProcessors;
using JiraTimeBotForm.TaskTime;
using JiraTimeBotForm.UI;
using Newtonsoft.Json;
// This is the code for your desktop app.
// Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
namespace JiraTimeBotForm
{
public partial class frmMain : Form
{
private readonly ILog _log;
private readonly Job _job;
private readonly IReadOnlyList<Control> _controls;
private readonly IContainer _container;
private readonly ITasksProcessors _tasksProcessors;
private readonly ITrayMenu _trayIcon;
private CancellationTokenSource _tokenSource;
private CancellationTokenSource GetTokenSource()
{
var tokenSource = new CancellationTokenSource();
tokenSource.Token.Register(() =>
{
_log.Info("Операция отменена...");
LockUnlock(true);
});
return tokenSource;
}
public frmMain()
{
InitializeComponent();
_container = new Bootstrapper().Build(txtLog);
_trayIcon = _container.Resolve<ITrayMenu>();
_trayIcon.Create(this);
_job = _container.Resolve<Job>();
_log = _container.Resolve<ILog>();
_tasksProcessors = _container.Resolve<ITasksProcessors>();
_controls = new Control[] { txtJiraLogin, txtJiraPassword, txtMercurialEmail, actTime, txtRepoPath, txtDummyMode, btnSave, btnStart, btnMeeting, chkAddComments };
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
_trayIcon.Hide();
}
public Settings ReadSettingsAndLock()
{
var settings = new Settings
{
JiraUserName = txtJiraLogin.Text,
JiraPassword = txtJiraPassword.Text,
MercurialAuthorEmail = txtMercurialEmail.Text,
ActivationTime = TimeSpan.Parse(actTime.Text),
RepositoryPath = txtRepoPath.Text,
DummyMode = txtDummyMode.Checked,
AddCommentsToWorklog = chkAddComments.Checked,
};
LockUnlock(false);
return settings;
}
public void LockUnlock(bool enabled)
{
foreach (var control in _controls)
{
control.Enabled = enabled;
}
btnCancel.Enabled = !enabled;
tmrStart.Enabled = enabled;
}
private void btnSave_Click(object sender, EventArgs e)
{
var settings = ReadSettingsAndLock();
LockUnlock(true);
settings.Save();
MessageBox.Show("Settings saved.");
settings = Settings.Load();
if (settings != null)
{
SetSettings(settings);
}
}
private void Form1_Load(object sender, EventArgs e)
{
var settings = Settings.Load();
if (settings != null)
{
SetSettings(settings);
}
}
private void SetSettings(Settings settings)
{
txtJiraLogin.Text = settings.JiraUserName;
txtJiraPassword.Text = settings.JiraPassword;
txtMercurialEmail.Text = settings.MercurialAuthorEmail;
txtRepoPath.Text = settings.RepositoryPath;
actTime.Text = settings.ActivationTime.ToString("hh\\:mm\\:ss");
txtDummyMode.Checked = settings.DummyMode;
chkAddComments.Checked = settings.AddCommentsToWorklog;
}
private async void btnStart_Click(object sender, EventArgs e)
{
var settings = ReadSettingsAndLock();
if (!Directory.Exists(settings.RepositoryPath))
{
MessageBox.Show("Папка с репо не сушествует.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (_tokenSource = GetTokenSource())
{
await _job.DoTheJob(settings, _tasksProcessors.WorkLogTasksProcessor, _tokenSource.Token);
}
LockUnlock(true);
}
private void frmMain_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
_trayIcon.Show();
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
_trayIcon.Hide();
}
}
private async void tmrStart_Tick(object sender, EventArgs e)
{
var runTime = TimeSpan.Parse(actTime.Text);
if (Math.Abs((DateTime.Now.TimeOfDay - runTime).TotalSeconds) < 1)
{
if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
{
_log.Error("Сегодня суббота или воскресенье. Работать нельзя =)");
return;
}
_log.Info("Автоматическое внесение времени.");
var settings = ReadSettingsAndLock();
await Task.Delay(2000);
if (settings.DummyMode)
{
settings.DummyMode = false;
_log.Info("Выключаем режим Тестового прогона");
}
using (var tokenSource = GetTokenSource())
{
await _job.DoTheJob(settings, _tasksProcessors.WorkLogTasksProcessor, tokenSource.Token);
}
LockUnlock(true);
}
}
private async void btnMeeting_Click(object sender, EventArgs e)
{
var settings = ReadSettingsAndLock();
if (!Directory.Exists(settings.RepositoryPath))
{
MessageBox.Show("Папка с репо не сушествует.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (_tokenSource = GetTokenSource())
{
await _job.DoTheJob(settings, _tasksProcessors.MeetingProcessor, _tokenSource.Token);
}
LockUnlock(true);
}
private void btnCancel_Click(object sender, EventArgs e)
{
_tokenSource.Cancel();
}
}
}
File diff suppressed because it is too large Load Diff