Skip to content

Commit

Permalink
Save window position and fields, regex timespan parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonaza committed Dec 7, 2020
1 parent 8cf5d83 commit 7e4e247
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 43 deletions.
27 changes: 27 additions & 0 deletions Skedaddler/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Skedaddler.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<userSettings>
<Skedaddler.Properties.Settings>
<setting name="WindowState" serializeAs="String">
<value>Normal</value>
</setting>
<setting name="WindowLocation" serializeAs="String">
<value>0, 0</value>
</setting>
<setting name="LastStateUpdate" serializeAs="String">
<value />
</setting>
<setting name="LastArrivalTime" serializeAs="String">
<value />
</setting>
<setting name="LastFlexMinutes" serializeAs="String">
<value />
</setting>
<setting name="LastBreakMinutes" serializeAs="String">
<value />
</setting>
</Skedaddler.Properties.Settings>
</userSettings>
</configuration>
10 changes: 5 additions & 5 deletions Skedaddler/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 112 additions & 13 deletions Skedaddler/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Skedaddler
{
Expand All @@ -23,19 +25,105 @@ public Skedaddler()

private void Skedaddler_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.WindowLocation.X == 0 || Properties.Settings.Default.WindowLocation.Y == 0)
{
Properties.Settings.Default.Upgrade();

CenterToScreen();
Properties.Settings.Default.WindowLocation = this.Location;
}
else
{
this.WindowState = Properties.Settings.Default.WindowState;

// we don't want a minimized window at startup
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;

this.Location = Properties.Settings.Default.WindowLocation;
}

this.Icon = Properties.Resources.skedaddlerIcon;

Timer timer = new Timer();
timer.Interval = 500;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();

arrivalTimeBox.Text = DateTime.Now.ToString(@"H\:mm", CultureInfo.InvariantCulture);
flexMinutesBox.Text = "0:00";
breakMinutesBox.Text = "0:00";
if (!reloadValues())
{
arrivalTimeBox.Text = DateTime.Now.ToString(@"H\:mm", CultureInfo.InvariantCulture);
flexMinutesBox.Text = "0:00";
breakMinutesBox.Text = "0:00";
}
saveCurrentValues();

soundPlayer = new SoundPlayer();
soundPlayer.Stream = Properties.Resources.alarm;
soundPlayer.Stream = Properties.Resources.alarm;
}

private void Skedaddler_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.WindowState = this.WindowState;
if (this.WindowState == FormWindowState.Normal)
{
Properties.Settings.Default.WindowLocation = this.Location;
}
else
{
Properties.Settings.Default.WindowLocation = this.RestoreBounds.Location;
}

Properties.Settings.Default.Save();
}

private void saveCurrentValues()
{
System.Console.WriteLine("Saving data:");
System.Console.WriteLine(" arrivalTimeBox " + arrivalTimeBox.Text);
System.Console.WriteLine(" flexMinutesBox " + flexMinutesBox.Text);
System.Console.WriteLine(" breakMinutesBox " + breakMinutesBox.Text);

Properties.Settings.Default.LastStateUpdate = DateTime.Today;
Properties.Settings.Default.LastArrivalTime = arrivalTimeBox.Text;
Properties.Settings.Default.LastFlexMinutes = flexMinutesBox.Text;
Properties.Settings.Default.LastBreakMinutes = breakMinutesBox.Text;
Properties.Settings.Default.Save();
}

private bool reloadValues()
{
if (!Properties.Settings.Default.LastStateUpdate.Equals(DateTime.Today))
return false;

System.Console.WriteLine("Loading data:");
System.Console.WriteLine(" LastArrivalTime " + Properties.Settings.Default.LastArrivalTime);
System.Console.WriteLine(" LastFlexMinutes " + Properties.Settings.Default.LastFlexMinutes);
System.Console.WriteLine(" LastBreakMinutes " + Properties.Settings.Default.LastBreakMinutes);

// Copy values over since changing the fields will trigger immediate re-save and overwriting values
string lastArrivalTime = Properties.Settings.Default.LastArrivalTime;
string lastFlexMinutes = Properties.Settings.Default.LastFlexMinutes;
string lastBreakMinutes = Properties.Settings.Default.LastBreakMinutes;

DateTime dt;
if (parseDateTime(lastArrivalTime, out dt))
arrivalTimeBox.Text = lastArrivalTime;
else
arrivalTimeBox.Text = DateTime.Now.ToString(@"H\:mm", CultureInfo.InvariantCulture);

TimeSpan ts;
if (parseTimeSpan(lastFlexMinutes, out ts))
flexMinutesBox.Text = lastFlexMinutes;
else
flexMinutesBox.Text = "0:00";

if (parseTimeSpan(lastBreakMinutes, out ts))
breakMinutesBox.Text = lastBreakMinutes;
else
breakMinutesBox.Text = "0:00";

return true;
}

private void timer_Tick(object sender, EventArgs e)
Expand Down Expand Up @@ -63,7 +151,7 @@ public bool parseDateTime(String timeString, out DateTime result)
return false;
}
}

public bool parseTimeSpan(String timeString, out TimeSpan result)
{
if (timeString.Length == 0)
Expand All @@ -72,22 +160,30 @@ public bool parseTimeSpan(String timeString, out TimeSpan result)
return false;
}

bool isNegative = false;
if(timeString[0] == '-')
Regex timeSpanRegex = new Regex(@"(-?)([012]?[0-9]):([0-5][0-9])", RegexOptions.Compiled | RegexOptions.IgnoreCase);

MatchCollection matches = timeSpanRegex.Matches(timeString);

if (matches.Count != 1)
{
isNegative = true;
timeString = timeString.Substring(1);
result = TimeSpan.Zero;
return false;
}

Match match = matches[0];
GroupCollection groups = match.Groups;

try
{
result = new TimeSpan(
int.Parse(timeString.Split(':')[0]),
int.Parse(timeString.Split(':')[1]),
0);
int.Parse(groups[2].Value),
int.Parse(groups[3].Value),
0
);

if (isNegative)
if (groups[1].Value.Equals("-"))
result = -result;

return true;
}
catch
Expand Down Expand Up @@ -138,16 +234,19 @@ public void updateTimeRemaining()
private void arrivalTimeBox_TextChanged(object sender, EventArgs e)
{
updateTimeRemaining();
saveCurrentValues();
}

private void flexMinutesBox_TextChanged(object sender, EventArgs e)
{
updateTimeRemaining();
saveCurrentValues();
}

private void breakMinutesBox_TextChanged(object sender, EventArgs e)
{
updateTimeRemaining();
saveCurrentValues();
}

bool alarmIsSet = false;
Expand Down
105 changes: 86 additions & 19 deletions Skedaddler/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e4e247

Please sign in to comment.