@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
17
17
along with this program. If not, see <https://www.gnu.org/licenses/>.
18
18
*/
19
19
using log4net ;
20
+ using NeedABreak . Properties ;
20
21
using NeedABreak . Utils ;
21
22
using System ;
22
23
using System . Collections . Generic ;
@@ -40,7 +41,7 @@ public partial class App : Application
40
41
#if ! DEBUG
41
42
private static System . Threading . Mutex mutex ;
42
43
#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 ;
44
45
private static Timer _timer = Delay > 120 ? new Timer ( 60000 ) : new Timer ( 10000 ) ;
45
46
#if DEBUG
46
47
private static Timer _debugTimer = new Timer ( 1000 ) ;
@@ -63,6 +64,8 @@ public partial class App : Application
63
64
64
65
public static ILog Logger { get ; private set ; }
65
66
67
+ private static Settings Settings => Settings . Default ;
68
+
66
69
static App ( )
67
70
{
68
71
// Uncomment to force a different language for UI testing
@@ -71,16 +74,35 @@ static App()
71
74
ConfigureLog4Net ( ) ;
72
75
73
76
#if DEBUG
74
- Logger . Debug ( $ "User settings path = { ConfigurationManager . OpenExeConfiguration ( ConfigurationUserLevel . PerUserRoamingAndLocal ) . FilePath } ") ;
77
+ Logger . Debug ( $ "User settings path = { ConfigurationManager . OpenExeConfiguration ( ConfigurationUserLevel . PerUserRoamingAndLocal ) . FilePath } ") ;
75
78
#endif
79
+ TimeSpan interruptionDuration = DateTime . Now - Settings . ExitTime ;
76
80
77
- if ( NeedABreak . Properties . Settings . Default . DayStart == DateTime . Today )
81
+ if ( Settings . DayStart == DateTime . Today )
78
82
{
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
+ }
80
90
}
81
91
82
92
_dayStart = DateTime . Today ;
83
93
_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
+
84
106
}
85
107
86
108
private static void ConfigureLog4Net ( )
@@ -103,7 +125,7 @@ public App()
103
125
}
104
126
#endif
105
127
InitializeComponent ( ) ;
106
- InitCountdown ( ) ;
128
+
107
129
_timer . Elapsed += Timer_Elapsed ;
108
130
#if DEBUG
109
131
_debugTimer . Elapsed += _debugTimer_Elapsed ;
@@ -125,9 +147,11 @@ public App()
125
147
private void App_Exit ( object sender , ExitEventArgs e )
126
148
{
127
149
// 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 ( ) ;
131
155
}
132
156
133
157
private async void UpdateToolTipTimer_Elapsed ( object sender , ElapsedEventArgs e )
@@ -154,7 +178,7 @@ private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
154
178
return ;
155
179
}
156
180
157
- if ( NeedABreak . Properties . Settings . Default . AutomaticSuspension )
181
+ if ( Settings . AutomaticSuspension )
158
182
{
159
183
UserNotificationState state = QueryUserNotificationState . GetState ( ) ;
160
184
@@ -268,7 +292,7 @@ private static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Se
268
292
269
293
if ( ! IsSuspended )
270
294
{
271
- InitCountdown ( ) ;
295
+ ResetCountdown ( ) ;
272
296
}
273
297
274
298
StartTimer ( ) ;
@@ -284,7 +308,7 @@ private static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.Se
284
308
}
285
309
}
286
310
287
- internal static void InitCountdown ( )
311
+ internal static void ResetCountdown ( )
288
312
{
289
313
_startCountdown = DateTime . Now ;
290
314
}
0 commit comments