Skip to content

Commit

Permalink
save main timer state in registry, so it restores after the program r…
Browse files Browse the repository at this point in the history
…estart
  • Loading branch information
HarpyWar committed Aug 7, 2017
1 parent b04f09a commit fcd144c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 21 deletions.
61 changes: 61 additions & 0 deletions ChronoFagTimer/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

using Hjson;
using Microsoft.Win32;
using NLog;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -220,7 +221,67 @@ private Color FromHex(string hex)
int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
}

#region Registy

string regKey = @"SOFTWARE\ChronoFagTimer";
public void SaveCurrentState(int counter, int currentRound, DateTime lastActiveTime)
{
var key = Registry.CurrentUser.CreateSubKey(regKey);
if (key != null)
{
key.SetValue("Counter", counter);
key.SetValue("CurrentRound", currentRound);
key.SetValue("LastActiveTime", lastActiveTime);
key.Close();
}
}

public int LoadCounterState()
{
var key = Registry.CurrentUser.CreateSubKey(regKey);
int value = 0;
if (key != null)
{
var obj = key.GetValue("Counter");
if (obj != null)
{
int.TryParse(obj.ToString(), out value);
}
}
key.Close();
return value;
}
public int LoadCurrentRoundState()
{
var key = Registry.CurrentUser.CreateSubKey(regKey);
int value = 0;
if (key != null)
{
var obj = key.GetValue("CurrentRound");
if (obj != null)
{
int.TryParse(obj.ToString(), out value);
}
}
key.Close();
return value;
}
public DateTime LoadLastActiveTimeState()
{
var key = Registry.CurrentUser.CreateSubKey(regKey);
DateTime value = DateTime.Now;
if (key != null)
{
var obj = key.GetValue("LastActiveTime");
if (obj != null)
{
DateTime.TryParse(obj.ToString(), out value);
}
}
key.Close();
return value;
}

#endregion
}
}
4 changes: 2 additions & 2 deletions ChronoFagTimer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.3.0")]
[assembly: AssemblyFileVersion("0.1.3.0")]
[assembly: AssemblyVersion("0.1.4.0")]
[assembly: AssemblyFileVersion("0.1.4.0")]
38 changes: 22 additions & 16 deletions ChronoFagTimer/TomatoForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ private void MenuStart_Click(object sender, EventArgs e)

private void Form1_Load(object sender, EventArgs e)
{
Logger.Info("Initialization...");

timeUnitTimer = new Timer()
{
Interval = 1000,
Expand All @@ -281,12 +283,15 @@ private void Form1_Load(object sender, EventArgs e)
lblPomodoroTime.Font = new Font(FontFamily.GenericSerif, Helper.GetTimerFontSize());
lblPomodoroTime.ForeColor = config.Face.PomodoroForeground;


startPomodoro();
this.WindowState = FormWindowState.Normal;

updateTomatoPosition();
updateElementsPosition();

// load state from registry
CurrentRound = config.LoadCurrentRoundState();
Counter = config.LoadCounterState();
lastActiveTime = config.LoadLastActiveTimeState();

Logger.Info("Initialized");
}

private void MenuAutostart_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -597,20 +602,18 @@ private void _timeUnitTimer_Elapsed(object sender, EventArgs e)
updateTomatoPosition();
}
updateElementsPosition();

config.SaveCurrentState(Counter, CurrentRound, lastActiveTime);
}

/// <summary>
/// Last timer active time
/// </summary>
DateTime? lastActiveTime = null;
DateTime lastActiveTime;
private void handleSleepLeap()
{
var now = DateTime.Now;
if (lastActiveTime == null)
{
// first assign
lastActiveTime = now;
}

var diff = (int)(now - (DateTime)lastActiveTime).TotalSeconds;

// if was leap
Expand Down Expand Up @@ -761,7 +764,7 @@ private void updateElementsPosition()
lblDownTitle.Left = this.Width / 2 - lblDownTitle.Width / 2;
lblDownTitle.Top = this.Height - (this.Height - lblBreakTime.Top) / 4;

btnExtraTime.Width = btnExtraTime.Height = 100;
btnExtraTime.Width = btnExtraTime.Height = this.Height / 12;
btnExtraTime.Left = this.Height - lblDownTitle.Top - btnExtraTime.Height;
btnExtraTime.Top = lblDownTitle.Top;
}
Expand Down Expand Up @@ -848,7 +851,7 @@ private bool mouseShowPomodoro()
}


#region show/hide animation
#region show/hide animation

/// <summary>
/// Show form with fade animation
Expand Down Expand Up @@ -922,7 +925,7 @@ public void FadeOut(bool immediate = false)



#endregion
#endregion


/// <summary>
Expand Down Expand Up @@ -983,11 +986,14 @@ private bool AllowMouseEventForCurrentProcess()
/// <returns></returns>
private int getExtraBreakTime()
{
return (int)Math.Floor((double)CurrentTimeUnit.CounterLimit / (getPrevTime(typeof(Pomodoro)).CounterLimit / config.ExtraTime));
var a = (double)CurrentTimeUnit.CounterLimit;
var b = (double)getPrevTime(typeof(Pomodoro)).CounterLimit / config.ExtraTime;
int result = (int)Math.Floor(a / b);
return result;
}


#region User Timers
#region User Timers


/// <summary>
Expand Down Expand Up @@ -1107,6 +1113,6 @@ public void RemoveCustomTimer(string key)
}


#endregion
#endregion
}
}
6 changes: 3 additions & 3 deletions ChronoFagTimer/config.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

"breaktimer": {
# lock keyboard during a break without a possibility to exit, also disable menu items "stop" and "exit" (true | false)
"lockmode": true,
"lockmode": false,

"sound": "sound/break.mp3",

Expand Down Expand Up @@ -168,15 +168,15 @@
"idletitle": "IDLE MODE",
"extramodetitle": "EXTRA MODE",
"stopmodetitle": "STOP MODE",
"addextratime": "I need {0} more minutes to work right now.\n(Next break time will be increased to {1} minute(s))",
"addextratime": "Give me {0} more minutes to work right now.\n(next break time will be increased to {1} minute(s))",
"lockmodesubtitle": "Workspace locked up, you have to rest",
"freemodesubtitle": "Let's go away from keyboard for awhile",
"start": "Start",
"stop": "Stop",
"autostart": "Autostart",
"about": "About...",
"quit": "Quit",
"locked": "(locked)",
"quitlocked": "(locked)",
"addtimer": "Add Timer...",
"removetimer": "Remove Timer",
"timercaption": "Caption",
Expand Down

0 comments on commit fcd144c

Please sign in to comment.