Skip to content

Commit 8157cd7

Browse files
authored
restore countdown after short interruption (#128)
1 parent f87df69 commit 8157cd7

File tree

5 files changed

+71
-13
lines changed

5 files changed

+71
-13
lines changed

NeedABreak/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ DEBUG
5050
<setting name="DayStart" serializeAs="String">
5151
<value />
5252
</setting>
53+
<setting name="ExitTime" serializeAs="String">
54+
<value />
55+
</setting>
56+
<setting name="StartCountDown" serializeAs="String">
57+
<value />
58+
</setting>
5359
</NeedABreak.Properties.Settings>
5460
</userSettings>
5561
<runtime>

NeedABreak/App.xaml.cs

+35-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919
using log4net;
20+
using NeedABreak.Properties;
2021
using NeedABreak.Utils;
2122
using System;
2223
using System.Collections.Generic;
@@ -40,7 +41,7 @@ public partial class App : Application
4041
#if !DEBUG
4142
private static System.Threading.Mutex mutex;
4243
#endif
43-
public static int Delay { get; set; } = NeedABreak.Properties.Settings.Default.Delay; // Seconds (put a low value here to facilitate debugging)
44+
public static int Delay { get; set; } = Settings.Delay;
4445
private static Timer _timer = Delay > 120 ? new Timer(60000) : new Timer(10000);
4546
#if DEBUG
4647
private static Timer _debugTimer = new Timer(1000);
@@ -63,6 +64,8 @@ public partial class App : Application
6364

6465
public static ILog Logger { get; private set; }
6566

67+
private static Settings Settings => Settings.Default;
68+
6669
static App()
6770
{
6871
// Uncomment to force a different language for UI testing
@@ -71,16 +74,35 @@ static App()
7174
ConfigureLog4Net();
7275

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

77-
if (NeedABreak.Properties.Settings.Default.DayStart == DateTime.Today)
81+
if (Settings.DayStart == DateTime.Today)
7882
{
79-
_cumulativeScreenTime = NeedABreak.Properties.Settings.Default.TodayScreenTime;
83+
_cumulativeScreenTime = Settings.TodayScreenTime;
84+
85+
if (interruptionDuration.TotalMinutes < 5)
86+
{
87+
// If the interruption was less than 5 minutes it is considered screen time (no break)
88+
_cumulativeScreenTime += interruptionDuration;
89+
}
8090
}
8191

8292
_dayStart = DateTime.Today;
8393
_startShowingScreen = DateTime.Now;
94+
95+
if (interruptionDuration.TotalMinutes < 5)
96+
{
97+
// Restore countdown when interruption was less than five minutes
98+
_startCountdown = Settings.StartCountDown;
99+
}
100+
else
101+
{
102+
// Countdown starts now
103+
_startCountdown = DateTime.Now;
104+
}
105+
84106
}
85107

86108
private static void ConfigureLog4Net()
@@ -103,7 +125,7 @@ public App()
103125
}
104126
#endif
105127
InitializeComponent();
106-
InitCountdown();
128+
107129
_timer.Elapsed += Timer_Elapsed;
108130
#if DEBUG
109131
_debugTimer.Elapsed += _debugTimer_Elapsed;
@@ -125,9 +147,11 @@ public App()
125147
private void App_Exit(object sender, ExitEventArgs e)
126148
{
127149
// Store today screen time to restore it when app is launched
128-
NeedABreak.Properties.Settings.Default.TodayScreenTime = GetTodayScreenTime();
129-
NeedABreak.Properties.Settings.Default.DayStart = _dayStart;
130-
NeedABreak.Properties.Settings.Default.Save();
150+
Settings.TodayScreenTime = GetTodayScreenTime();
151+
Settings.DayStart = _dayStart;
152+
Settings.ExitTime = DateTime.Now;
153+
Settings.StartCountDown = _startCountdown;
154+
Settings.Save();
131155
}
132156

133157
private async void UpdateToolTipTimer_Elapsed(object sender, ElapsedEventArgs e)
@@ -154,7 +178,7 @@ private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
154178
return;
155179
}
156180

157-
if (NeedABreak.Properties.Settings.Default.AutomaticSuspension)
181+
if (Settings.AutomaticSuspension)
158182
{
159183
UserNotificationState state = QueryUserNotificationState.GetState();
160184

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

269293
if (!IsSuspended)
270294
{
271-
InitCountdown();
295+
ResetCountdown();
272296
}
273297

274298
StartTimer();
@@ -284,7 +308,7 @@ private static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Se
284308
}
285309
}
286310

287-
internal static void InitCountdown()
311+
internal static void ResetCountdown()
288312
{
289313
_startCountdown = DateTime.Now;
290314
}

NeedABreak/MainWindow.xaml.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private void ReporterBalloon_Click(object sender, RoutedEventArgs e)
258258
private void AnnulerBalloon_Click(object sender, RoutedEventArgs e)
259259
{
260260
uxTaskbarIcon.CloseBalloon();
261-
App.InitCountdown(); // annulation, le compte à rebours repart de zéro
261+
App.ResetCountdown(); // annulation, le compte à rebours repart de zéro
262262
}
263263

264264
private void ReporterButton_Click(object sender, RoutedEventArgs e)
@@ -270,7 +270,7 @@ private void ReporterButton_Click(object sender, RoutedEventArgs e)
270270
private void CancelButton_Click(object sender, RoutedEventArgs e)
271271
{
272272
Interlocked.Exchange(ref _cancellationTokenSource, new CancellationTokenSource()).Cancel();
273-
App.InitCountdown();
273+
App.ResetCountdown();
274274
}
275275

276276
private void LockButton_Click(object sender, RoutedEventArgs e)

NeedABreak/Properties/Settings.Designer.cs

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

NeedABreak/Properties/Settings.settings

+6
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@
1717
<Setting Name="DayStart" Type="System.DateTime" Scope="User">
1818
<Value Profile="(Default)" />
1919
</Setting>
20+
<Setting Name="ExitTime" Type="System.DateTime" Scope="User">
21+
<Value Profile="(Default)" />
22+
</Setting>
23+
<Setting Name="StartCountDown" Type="System.DateTime" Scope="User">
24+
<Value Profile="(Default)" />
25+
</Setting>
2026
</Settings>
2127
</SettingsFile>

0 commit comments

Comments
 (0)