Skip to content

Commit

Permalink
Readability fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jokedst committed Jul 17, 2018
1 parent ea6669c commit ba14ecd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 84 deletions.
1 change: 1 addition & 0 deletions ATray.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Global
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
Description = Various tools to make my life easier
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{096B868B-82CE-4E60-951D-E82102288EE3}.Debug|x64.ActiveCfg = Debug|x64
Expand Down
2 changes: 2 additions & 0 deletions ATray/ATray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@
<Compile Include="Dialogs\SettingsForm.Designer.cs">
<DependentUpon>SettingsForm.cs</DependentUpon>
</Compile>
<Compile Include="Tools\Extensions.cs" />
<Compile Include="Tools\IFactory.cs" />
<Compile Include="Tools\InternalTraceListener.cs" />
<Compile Include="Tools\IShowNotifications.cs" />
<Compile Include="Tools\OverallStatusType.cs" />
<Compile Include="Tools\PubSubHub.cs" />
<Compile Include="Tools\SimpleFactory.cs" />
Expand Down
14 changes: 6 additions & 8 deletions ATray/Dialogs/ActivityHistoryForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using ATray.Tools;

namespace ATray
{
Expand Down Expand Up @@ -26,15 +27,14 @@ public partial class ActivityHistoryForm : Form
private Point _lastPosition;
private int _lastScrollPositionY;

private int _currentMonth = (DateTime.Now.Year * 100) + DateTime.Now.Month;
private int _currentMonth = DateTime.Now.IntegerMonth();

private bool _forceRedraw;
private bool _ignoreEvents;

private uint _graphWidth;
private uint _graphSeconds;
private uint _graphFirstSecond;
//private Dictionary<string, MonthActivities> _shownHistory;
private byte[] _indexToDaynumber;

private bool _showSharedHistory;
Expand Down Expand Up @@ -160,8 +160,9 @@ private string SecondToTime(uint second)
private void ActivityHistoryForm_Paint(object sender, PaintEventArgs e)
{
// If we're showing the current month, redraw regularly so the image don't get stale
var monthNow = DateTime.Now.Year * 100 + DateTime.Now.Month;
var imageAgeInMinutes = DateTime.Now.Subtract(_lastHistoryRedraw).TotalMinutes;
var now = DateTime.Now;
var monthNow = now.Year * 100 + now.Month;
var imageAgeInMinutes = now.Subtract(_lastHistoryRedraw).TotalMinutes;
var imageGettingOld = _currentMonth == monthNow && imageAgeInMinutes > Program.Configuration.HistoryRedrawTimeout;

if (_historyGraph != null
Expand Down Expand Up @@ -191,9 +192,6 @@ private void ActivityHistoryForm_Paint(object sender, PaintEventArgs e)
if (computer == AllComputers) computer = null;
history = ActivityManager.GetSharedMonthActivities(year, month, computer);
}

// if(hidePlay)
//history = WorkPlayFilter.Filter(history, WorkPlayType.WorkOnly);

_indexToDaynumber = history.SelectMany(x=>x.Value.Days.Keys).Distinct().OrderBy(x => x).ToArray();

Expand Down Expand Up @@ -251,7 +249,7 @@ private void ActivityHistoryForm_Paint(object sender, PaintEventArgs e)
}

_lastWindowWidth = ClientRectangle.Width;
_lastHistoryRedraw = DateTime.Now;
_lastHistoryRedraw = now;
_forceRedraw = false;
}

Expand Down
50 changes: 21 additions & 29 deletions ATray/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ATray.Tools;

namespace ATray
namespace ATray
{
using System;
using System.Collections.Generic;
Expand All @@ -15,18 +13,14 @@ namespace ATray
using Newtonsoft.Json;
using RepositoryManager;
using RepositoryManager.Git;

public interface IShowNotifications
{
void ShowNotification(string text);
}
using Tools;

public partial class MainWindow : Form, IShowNotifications
{
private bool inWarnState;
private bool reallyClose;
private ActivityHistoryForm historyForm;
private OverallStatusType OverallStatus;
private bool _inWarnState;
private bool _reallyExitProgram;
private ActivityHistoryForm _historyForm;
private OverallStatusType _overallStatus;

private readonly IRepositoryCollection _repositoryCollection;
private readonly IFactory<ISettingsDialog> _settingsDialogFactory;
Expand All @@ -53,14 +47,14 @@ public MainWindow(IRepositoryCollection repositoryCollection,
lblInfo.Text = "Take a break!";
ShowMe();
TopMost = true;
inWarnState = true;
_inWarnState = true;
};
activityMonitor.UserHasTakenBreak += (sender, e) =>
{
BackColor = Color.Green;
lblInfo.Text = "You can start working now";
TopMost = false;
inWarnState = false;
_inWarnState = false;
};
activityMonitor.UserIsBackFromAbsense += (sender, e) =>
{
Expand All @@ -79,8 +73,6 @@ public MainWindow(IRepositoryCollection repositoryCollection,
CreateRepositoryMenyEntries();
//var animTray = new IconAnimator(trayIcon, Properties.Resources.anim1);
//animTray.StartAnimation();


NativeMethods.RegisterForPowerNotifications(this.Handle);
}

Expand Down Expand Up @@ -142,9 +134,9 @@ public void CreateRepositoryMenyEntries()
private void UpdateIcon()
{
var worstStatus = _repositoryCollection.Select(x => x.LastStatus.ToOverallStatus()).OrderBy(x=>x).LastOrDefault();
if (worstStatus == OverallStatus) return;
OverallStatus = worstStatus;
switch (OverallStatus)
if (worstStatus == _overallStatus) return;
_overallStatus = worstStatus;
switch (_overallStatus)
{
case OverallStatusType.Ok:
this.trayIcon.Icon = Program.GreyIcon;
Expand Down Expand Up @@ -195,7 +187,7 @@ private void OnRepositoryStatusChanged(object sender, RepositoryEventArgs e)

private void OnResize(object sender, EventArgs e)
{
if (inWarnState) ShowMe();
if (_inWarnState) ShowMe();
else if (WindowState == FormWindowState.Minimized) Hide();
}

Expand All @@ -216,7 +208,7 @@ private void ShowMe()
/// </summary>
private void OnMenuClickExit(object sender, EventArgs e)
{
reallyClose = true;
_reallyExitProgram = true;
Close();
}

Expand All @@ -225,31 +217,31 @@ private void OnMenuClickExit(object sender, EventArgs e)
/// </summary>
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (!inWarnState)
if (!_inWarnState)
Hide();
}

private void OnFormClosing(object sender, FormClosingEventArgs e)
{
// Don't close the program, just minimize it (unless they used the menu)
if (e.CloseReason == CloseReason.UserClosing && !reallyClose)
if (e.CloseReason == CloseReason.UserClosing && !_reallyExitProgram)
{
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
else
{
historyForm?.Close();
_historyForm?.Close();
}
}

private void OnMenuClickHistory(object sender, EventArgs e)
{
if (historyForm == null || historyForm.IsDisposed)
historyForm = new ActivityHistoryForm();
if (historyForm.IsDisposed) return;
historyForm.Show();
historyForm.Focus();
if (_historyForm == null || _historyForm.IsDisposed)
_historyForm = new ActivityHistoryForm();
if (_historyForm.IsDisposed) return;
_historyForm.Show();
_historyForm.Focus();
}

private void OnMenuClickSettings(object sender, EventArgs e)
Expand Down
56 changes: 56 additions & 0 deletions ATray/Tools/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Drawing;
using RepositoryManager;

namespace ATray.Tools
{
public static class Extensions
{
public static OverallStatusType ToOverallStatus(this RepoStatus status)
{
switch (status)
{
case RepoStatus.Conflict:
return OverallStatusType.CodeRed;
case RepoStatus.Behind:
return OverallStatusType.WarnBehind;
case RepoStatus.Dirty:
return OverallStatusType.WarnAhead;
default:
return OverallStatusType.Ok;
}
}

public static OverallStatusType WorstOf(this OverallStatusType overallStatus,RepoStatus status)
{
var converted = status.ToOverallStatus();
return converted > overallStatus ? converted : overallStatus;
}

public static Color ToColor(OverallStatusType overallStatus)
{
switch (overallStatus)
{
case OverallStatusType.Ok: return Color.Transparent;
case OverallStatusType.WarnAhead: return Color.FromArgb(0x80, 0xB1, 0xB1, 0xFF);
case OverallStatusType.CodeRed: return Color.FromArgb(0x80, Color.Red);
case OverallStatusType.WarnBehind: return Color.FromArgb(0x80, Color.Yellow);
default: throw new ArgumentOutOfRangeException(nameof(overallStatus), overallStatus, null);
}
}

public static (string, string) Divide(this string main, char dividor)
{
var parts = main.Split(new[] {dividor}, 2);
return (parts[0], parts.Length == 1 ? null : parts[1]);
}

/// <summary>
/// Converts DateTime to an integer representing the month in a readable format, e.g. 2018-03-01 becomes 201803
/// </summary>
public static int IntegerMonth(this DateTime date)
{
return date.Year * 100 + date.Month;
}
}
}
7 changes: 7 additions & 0 deletions ATray/Tools/IShowNotifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ATray.Tools
{
public interface IShowNotifications
{
void ShowNotification(string text);
}
}
48 changes: 1 addition & 47 deletions ATray/Tools/OverallStatusType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Drawing;
using RepositoryManager;

namespace ATray.Tools
namespace ATray.Tools
{
public enum OverallStatusType
{
Expand All @@ -11,46 +7,4 @@ public enum OverallStatusType
WarnBehind=2,
CodeRed=3
}

public static class Extensions
{
public static OverallStatusType ToOverallStatus(this RepoStatus status)
{
switch (status)
{
case RepoStatus.Conflict:
return OverallStatusType.CodeRed;
case RepoStatus.Behind:
return OverallStatusType.WarnBehind;
case RepoStatus.Dirty:
return OverallStatusType.WarnAhead;
default:
return OverallStatusType.Ok;
}
}

public static OverallStatusType WorstOf(this OverallStatusType overallStatus,RepoStatus status)
{
var converted = status.ToOverallStatus();
return converted > overallStatus ? converted : overallStatus;
}

public static Color ToColor(OverallStatusType overallStatus)
{
switch (overallStatus)
{
case OverallStatusType.Ok: return Color.Transparent;
case OverallStatusType.WarnAhead: return Color.FromArgb(0x80, 0xB1, 0xB1, 0xFF);
case OverallStatusType.CodeRed: return Color.FromArgb(0x80, Color.Red);
case OverallStatusType.WarnBehind: return Color.FromArgb(0x80, Color.Yellow);
default: throw new ArgumentOutOfRangeException(nameof(overallStatus), overallStatus, null);
}
}

public static (string, string) Divide(this string main, char dividor)
{
var parts = main.Split(new[] {dividor}, 2);
return (parts[0], parts.Length == 1 ? null : parts[1]);
}
}
}

0 comments on commit ba14ecd

Please sign in to comment.