Skip to content

Commit

Permalink
fix bug with screen time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
bNobo committed Apr 5, 2024
1 parent 993b33b commit 74ba1ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
24 changes: 14 additions & 10 deletions NeedABreak/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace NeedABreak
/// </summary>
public partial class App : Application
{
private static DateTime _startTime;
private static DateTime _startCountdown;
#if !DEBUG
private static System.Threading.Mutex mutex;
#endif
Expand All @@ -59,6 +59,7 @@ public partial class App : Application
// store dayStart to enable reset of today's screen time in the event of the user not shutting down its computer every day
private static DateTime _dayStart;
private static TimeSpan _cumulativeScreenTime;
private static DateTime _startShowingScreen;

public static ILog Logger { get; private set; }

Expand All @@ -69,6 +70,7 @@ static App()
//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
ConfigureLog4Net();
_dayStart = DateTime.Today;
_startShowingScreen = DateTime.Now;
}

private static void ConfigureLog4Net()
Expand All @@ -91,7 +93,7 @@ public App()
}
#endif
InitializeComponent();
InitStartTime();
InitCountdown();
_timer.Elapsed += Timer_Elapsed;
#if DEBUG
_debugTimer.Elapsed += _debugTimer_Elapsed;
Expand Down Expand Up @@ -222,7 +224,7 @@ await Current.Dispatcher.InvokeAsync(() =>

public static double GetMinutesLeft()
{
var minutesElapsed = (DateTime.Now - _startTime).TotalMinutes;
var minutesElapsed = (DateTime.Now - _startCountdown).TotalMinutes;
var minutesLeft = Delay / 60 - minutesElapsed;
return minutesLeft;
}
Expand All @@ -246,29 +248,30 @@ private static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Se

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

StartTimer();
_updateToolTipTimer.Start();
_startShowingScreen = DateTime.Now;
}
else if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionLock)
{
Logger.Debug("SessionLock");
StopTimer();
_updateToolTipTimer.Stop();
_cumulativeScreenTime += DateTime.Now - _startTime;
_cumulativeScreenTime += DateTime.Now - _startShowingScreen;
}
}

internal static void InitStartTime()
internal static void InitCountdown()
{
_startTime = DateTime.Now;
_startCountdown = DateTime.Now;
}

internal static void ShiftStartTime()
internal static void ShiftCountdown()
{
_startTime += TimeSpan.FromMinutes(Delay / 1080d); // time skew proportional to Delay. For a 90 minutes delay it gives 5 minutes time skew which delay lock time to 5 minutes (Delay modification is forbidden)
_startCountdown += TimeSpan.FromMinutes(Delay / 1080d); // time skew proportional to Delay. For a 90 minutes delay it gives 5 minutes time skew which delay lock time to 5 minutes (Delay modification is forbidden)
}

internal static void Suspend(SuspensionCause suspensionCause = SuspensionCause.Manual)
Expand Down Expand Up @@ -301,9 +304,10 @@ public static TimeSpan GetTodayScreenTime()
// day changed, reset today's screen time
_dayStart = DateTime.Today;
_cumulativeScreenTime = TimeSpan.Zero;
_startShowingScreen = DateTime.Now;
}

return _cumulativeScreenTime + (DateTime.Now - _startTime);
return _cumulativeScreenTime + (DateTime.Now - _startShowingScreen);
}
}
}
8 changes: 4 additions & 4 deletions NeedABreak/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,25 @@ private void CloseBalloon_Click(object sender, RoutedEventArgs e)
private void ReporterBalloon_Click(object sender, RoutedEventArgs e)
{
uxTaskbarIcon.CloseBalloon();
App.ShiftStartTime();
App.ShiftCountdown();
}

private void AnnulerBalloon_Click(object sender, RoutedEventArgs e)
{
uxTaskbarIcon.CloseBalloon();
App.InitStartTime(); // annulation, le compte à rebours repart de zéro
App.InitCountdown(); // annulation, le compte à rebours repart de zéro
}

private void ReporterButton_Click(object sender, RoutedEventArgs e)
{
Interlocked.Exchange(ref _cancellationTokenSource, new CancellationTokenSource()).Cancel();
App.ShiftStartTime();
App.ShiftCountdown();
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
Interlocked.Exchange(ref _cancellationTokenSource, new CancellationTokenSource()).Cancel();
App.InitStartTime();
App.InitCountdown();
}

private void LockButton_Click(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit 74ba1ce

Please sign in to comment.