-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivityHandler.cs
101 lines (89 loc) · 3.86 KB
/
ActivityHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace potter
{
class ActivityHandler
{
System.Timers.Timer queryUserActivity = new System.Timers.Timer();
object handlingUserActivity = new object();
bool showConfiguration = false;
bool startNow = false;
Timesheet timesheet;
DateTime lastSystemUnlockTime = DateTime.Now;
public delegate void DelegateInitiateToQueryUserActivity(bool showConfiguration, bool startNow, bool systemUnlock);
public ActivityHandler(Timesheet timesheet)
{
this.timesheet = timesheet;
queryUserActivity.Elapsed += QueryUserActivity;
InitiateToQueryUserActivity(false, true, false);
}
internal void InitiateToQueryUserActivity(bool showConfiguration, bool startNow, bool systemUnlock)
{
if (systemUnlock)
{
lastSystemUnlockTime = DateTime.Now;
}
this.showConfiguration = showConfiguration;
this.startNow = startNow;
queryUserActivity.Interval = 1;
queryUserActivity.Start();
}
private void QueryUserActivity(object sender, EventArgs e)
{
if (Monitor.TryEnter(handlingUserActivity))
{
try
{
queryUserActivity.Stop();
Activity activity = new Activity(Configuration.CurrentActivity, Configuration.CurrentCategory, showConfiguration);
showConfiguration = false;
DateTime startTime = DateTime.Now;
DialogResult dialogResult = activity.ShowDialog();
try
{
Configuration.CurrentActivity = activity.Current;
Configuration.CurrentCategory = activity.Category;
}
catch (System.Exception ex)
{
Configuration.ShowSaveError(ex);
}
string activityDescription;
if (startNow)
{
if (lastSystemUnlockTime > startTime)
{
startTime = lastSystemUnlockTime;
activityDescription = "started when the system was unlocked";
}
else
{
activityDescription = "started when the dialog opened";
}
}
else
{
// the activity does not start when the dialog opens, but only after the user closes the dialog
startTime = DateTime.Now;
activityDescription = "started after the user closed the dialog";
}
Logger.Append("Adding activity that " + activityDescription + ", i. e. " + startTime.ToString(Timesheet.dateFormat) + " " + startTime.ToString(Timesheet.timeFormat) + ": " + activity.Category + " | " + activity.Current);
timesheet.AddActivity(startTime, activity.Current, activity.Category);
TimeSpan selectedInterval = dialogResult == DialogResult.OK ? activity.DefaultTime : activity.OptionalTime;
queryUserActivity.Interval = selectedInterval.TotalMilliseconds;
queryUserActivity.Start();
Logger.Append("Set timer to open dialog in " + selectedInterval.Minutes.ToString() + " minutes");
}
finally
{
Monitor.Exit(handlingUserActivity);
}
}
}
}
}