From 7e4e247e18b53e6c6b1f81169f4c28132101bebb Mon Sep 17 00:00:00 2001 From: Sonaza Date: Mon, 7 Dec 2020 17:19:00 +0200 Subject: [PATCH] Save window position and fields, regex timespan parsing --- Skedaddler/App.config | 27 +++++ Skedaddler/Main.Designer.cs | 10 +- Skedaddler/Main.cs | 125 ++++++++++++++++++--- Skedaddler/Properties/Settings.Designer.cs | 105 +++++++++++++---- Skedaddler/Properties/Settings.settings | 29 ++++- Skedaddler/Settings.cs | 28 +++++ 6 files changed, 281 insertions(+), 43 deletions(-) create mode 100644 Skedaddler/Settings.cs diff --git a/Skedaddler/App.config b/Skedaddler/App.config index 88fa402..83bc1c4 100644 --- a/Skedaddler/App.config +++ b/Skedaddler/App.config @@ -1,6 +1,33 @@  + + +
+ + + + + + Normal + + + 0, 0 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Skedaddler/Main.Designer.cs b/Skedaddler/Main.Designer.cs index ed00f90..39b8269 100644 --- a/Skedaddler/Main.Designer.cs +++ b/Skedaddler/Main.Designer.cs @@ -63,7 +63,7 @@ private void InitializeComponent() this.arrivalTimeBox.TabIndex = 1; this.arrivalTimeBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.arrivalTimeBox.TextChanged += new System.EventHandler(this.arrivalTimeBox_TextChanged); - this.arrivalTimeBox.KeyDown += new KeyEventHandler(this.arrivalTimeBox_KeyDown); + this.arrivalTimeBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.arrivalTimeBox_KeyDown); // // flexMinutesBox // @@ -74,8 +74,8 @@ private void InitializeComponent() this.flexMinutesBox.TabIndex = 2; this.flexMinutesBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.flexMinutesBox.TextChanged += new System.EventHandler(this.flexMinutesBox_TextChanged); + this.flexMinutesBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.flexMinutesBox_KeyDown); this.flexMinutesBox.LostFocus += new System.EventHandler(this.flexMinutesBox_LostFocus); - this.flexMinutesBox.KeyDown += new KeyEventHandler(this.flexMinutesBox_KeyDown); // // label2 // @@ -107,8 +107,8 @@ private void InitializeComponent() this.breakMinutesBox.TabIndex = 3; this.breakMinutesBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.breakMinutesBox.TextChanged += new System.EventHandler(this.breakMinutesBox_TextChanged); + this.breakMinutesBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.breakMinutesBox_KeyDown); this.breakMinutesBox.LostFocus += new System.EventHandler(this.breakMinutesBox_LostFocus); - this.breakMinutesBox.KeyDown += new KeyEventHandler(this.breakMinutesBox_KeyDown); // // label4 // @@ -165,9 +165,8 @@ private void InitializeComponent() this.alarmTimeBox.TabIndex = 10; this.alarmTimeBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.alarmTimeBox.TextChanged += new System.EventHandler(this.alarmTimeBox_TextChanged); + this.alarmTimeBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.alarmTimeBox_KeyDown); this.alarmTimeBox.LostFocus += new System.EventHandler(this.alarmTimeBox_LostFocus); - this.alarmTimeBox.KeyDown += new KeyEventHandler(this.alarmTimeBox_KeyDown); - // // clearAlarmButton // @@ -216,6 +215,7 @@ private void InitializeComponent() this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Name = "Skedaddler"; this.Text = "Skedaddler"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Skedaddler_FormClosing); this.Load += new System.EventHandler(this.Skedaddler_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/Skedaddler/Main.cs b/Skedaddler/Main.cs index 361e54a..cd611e0 100644 --- a/Skedaddler/Main.cs +++ b/Skedaddler/Main.cs @@ -9,6 +9,8 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using System.IO; +using System.Text.RegularExpressions; namespace Skedaddler { @@ -23,6 +25,24 @@ 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(); @@ -30,12 +50,80 @@ private void Skedaddler_Load(object sender, EventArgs e) 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) @@ -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) @@ -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 @@ -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; diff --git a/Skedaddler/Properties/Settings.Designer.cs b/Skedaddler/Properties/Settings.Designer.cs index 221c4df..212307f 100644 --- a/Skedaddler/Properties/Settings.Designer.cs +++ b/Skedaddler/Properties/Settings.Designer.cs @@ -8,23 +8,90 @@ // //------------------------------------------------------------------------------ -namespace Skedaddler.Properties -{ - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase - { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default - { - get - { - return defaultInstance; - } - } - } +namespace Skedaddler.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.8.1.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("Normal")] + public global::System.Windows.Forms.FormWindowState WindowState { + get { + return ((global::System.Windows.Forms.FormWindowState)(this["WindowState"])); + } + set { + this["WindowState"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0, 0")] + public global::System.Drawing.Point WindowLocation { + get { + return ((global::System.Drawing.Point)(this["WindowLocation"])); + } + set { + this["WindowLocation"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + public global::System.DateTime LastStateUpdate { + get { + return ((global::System.DateTime)(this["LastStateUpdate"])); + } + set { + this["LastStateUpdate"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string LastArrivalTime { + get { + return ((string)(this["LastArrivalTime"])); + } + set { + this["LastArrivalTime"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string LastFlexMinutes { + get { + return ((string)(this["LastFlexMinutes"])); + } + set { + this["LastFlexMinutes"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string LastBreakMinutes { + get { + return ((string)(this["LastBreakMinutes"])); + } + set { + this["LastBreakMinutes"] = value; + } + } + } } diff --git a/Skedaddler/Properties/Settings.settings b/Skedaddler/Properties/Settings.settings index 3964565..3c3a992 100644 --- a/Skedaddler/Properties/Settings.settings +++ b/Skedaddler/Properties/Settings.settings @@ -1,7 +1,24 @@  - - - - - - + + + + + Normal + + + 0, 0 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Skedaddler/Settings.cs b/Skedaddler/Settings.cs new file mode 100644 index 0000000..e0c1843 --- /dev/null +++ b/Skedaddler/Settings.cs @@ -0,0 +1,28 @@ +namespace Skedaddler.Properties { + + + // This class allows you to handle specific events on the settings class: + // The SettingChanging event is raised before a setting's value is changed. + // The PropertyChanged event is raised after a setting's value is changed. + // The SettingsLoaded event is raised after the setting values are loaded. + // The SettingsSaving event is raised before the setting values are saved. + internal sealed partial class Settings { + + public Settings() { + // // To add event handlers for saving and changing settings, uncomment the lines below: + // + // this.SettingChanging += this.SettingChangingEventHandler; + // + // this.SettingsSaving += this.SettingsSavingEventHandler; + // + } + + private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) { + // Add code to handle the SettingChangingEvent event here. + } + + private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) { + // Add code to handle the SettingsSaving event here. + } + } +}