Skip to content

Commit

Permalink
restore countdown after short interruption (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
bNobo authored Apr 20, 2024
1 parent f87df69 commit 8157cd7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
6 changes: 6 additions & 0 deletions NeedABreak/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ DEBUG
<setting name="DayStart" serializeAs="String">
<value />
</setting>
<setting name="ExitTime" serializeAs="String">
<value />
</setting>
<setting name="StartCountDown" serializeAs="String">
<value />
</setting>
</NeedABreak.Properties.Settings>
</userSettings>
<runtime>
Expand Down
46 changes: 35 additions & 11 deletions NeedABreak/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using log4net;
using NeedABreak.Properties;
using NeedABreak.Utils;
using System;
using System.Collections.Generic;
Expand All @@ -40,7 +41,7 @@ public partial class App : Application
#if !DEBUG
private static System.Threading.Mutex mutex;
#endif
public static int Delay { get; set; } = NeedABreak.Properties.Settings.Default.Delay; // Seconds (put a low value here to facilitate debugging)
public static int Delay { get; set; } = Settings.Delay;
private static Timer _timer = Delay > 120 ? new Timer(60000) : new Timer(10000);
#if DEBUG
private static Timer _debugTimer = new Timer(1000);
Expand All @@ -63,6 +64,8 @@ public partial class App : Application

public static ILog Logger { get; private set; }

private static Settings Settings => Settings.Default;

static App()
{
// Uncomment to force a different language for UI testing
Expand All @@ -71,16 +74,35 @@ static App()
ConfigureLog4Net();

#if DEBUG
Logger.Debug($"User settings path = {ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath}");
Logger.Debug($"User settings path = {ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath}");
#endif
TimeSpan interruptionDuration = DateTime.Now - Settings.ExitTime;

if (NeedABreak.Properties.Settings.Default.DayStart == DateTime.Today)
if (Settings.DayStart == DateTime.Today)
{
_cumulativeScreenTime = NeedABreak.Properties.Settings.Default.TodayScreenTime;
_cumulativeScreenTime = Settings.TodayScreenTime;

if (interruptionDuration.TotalMinutes < 5)
{
// If the interruption was less than 5 minutes it is considered screen time (no break)
_cumulativeScreenTime += interruptionDuration;
}
}

_dayStart = DateTime.Today;
_startShowingScreen = DateTime.Now;

if (interruptionDuration.TotalMinutes < 5)
{
// Restore countdown when interruption was less than five minutes
_startCountdown = Settings.StartCountDown;
}
else
{
// Countdown starts now
_startCountdown = DateTime.Now;
}

}

private static void ConfigureLog4Net()
Expand All @@ -103,7 +125,7 @@ public App()
}
#endif
InitializeComponent();
InitCountdown();

_timer.Elapsed += Timer_Elapsed;
#if DEBUG
_debugTimer.Elapsed += _debugTimer_Elapsed;
Expand All @@ -125,9 +147,11 @@ public App()
private void App_Exit(object sender, ExitEventArgs e)
{
// Store today screen time to restore it when app is launched
NeedABreak.Properties.Settings.Default.TodayScreenTime = GetTodayScreenTime();
NeedABreak.Properties.Settings.Default.DayStart = _dayStart;
NeedABreak.Properties.Settings.Default.Save();
Settings.TodayScreenTime = GetTodayScreenTime();
Settings.DayStart = _dayStart;
Settings.ExitTime = DateTime.Now;
Settings.StartCountDown = _startCountdown;
Settings.Save();
}

private async void UpdateToolTipTimer_Elapsed(object sender, ElapsedEventArgs e)
Expand All @@ -154,7 +178,7 @@ private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
return;
}

if (NeedABreak.Properties.Settings.Default.AutomaticSuspension)
if (Settings.AutomaticSuspension)
{
UserNotificationState state = QueryUserNotificationState.GetState();

Expand Down Expand Up @@ -268,7 +292,7 @@ private static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Se

if (!IsSuspended)
{
InitCountdown();
ResetCountdown();
}

StartTimer();
Expand All @@ -284,7 +308,7 @@ private static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Se
}
}

internal static void InitCountdown()
internal static void ResetCountdown()
{
_startCountdown = DateTime.Now;
}
Expand Down
4 changes: 2 additions & 2 deletions NeedABreak/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private void ReporterBalloon_Click(object sender, RoutedEventArgs e)
private void AnnulerBalloon_Click(object sender, RoutedEventArgs e)
{
uxTaskbarIcon.CloseBalloon();
App.InitCountdown(); // annulation, le compte à rebours repart de zéro
App.ResetCountdown(); // annulation, le compte à rebours repart de zéro
}

private void ReporterButton_Click(object sender, RoutedEventArgs e)
Expand All @@ -270,7 +270,7 @@ private void ReporterButton_Click(object sender, RoutedEventArgs e)
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
Interlocked.Exchange(ref _cancellationTokenSource, new CancellationTokenSource()).Cancel();
App.InitCountdown();
App.ResetCountdown();
}

private void LockButton_Click(object sender, RoutedEventArgs e)
Expand Down
22 changes: 22 additions & 0 deletions NeedABreak/Properties/Settings.Designer.cs

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

6 changes: 6 additions & 0 deletions NeedABreak/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@
<Setting Name="DayStart" Type="System.DateTime" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="ExitTime" Type="System.DateTime" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="StartCountDown" Type="System.DateTime" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

0 comments on commit 8157cd7

Please sign in to comment.