-
Notifications
You must be signed in to change notification settings - Fork 11
/
YOTM.Manager.pas
181 lines (158 loc) · 4.32 KB
/
YOTM.Manager.pas
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
unit YOTM.Manager;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.ExtCtrls, Direct2D, D2D1, System.Generics.Collections,
HGM.Controls.PanelExt, Vcl.ComCtrls, System.Types, Vcl.StdCtrls,
HGM.Controls.SpinEdit, Vcl.Grids, HGM.Controls.VirtualTable, YOTM.DB,
Vcl.AppEvnts, Vcl.Menus, YOTM.DB.Comments, YOTM.DB.Labels, YOTM.DB.Tasks,
YOTM.DB.Times, HGM.Common.Utils, YOTM.DB.LabelTypes, YOTM.DB.Notes;
type
TTaskNotify = procedure(Task: TTaskItem) of object;
TNotifyWait = record
TaskID: Integer;
Time: TDateTime;
end;
TNotifyWaitList = class(TList<TNotifyWait>)
end;
TManager = class(TObject)
private
FDataBase: TDB;
FTimerCheck: TTimer;
FActivate: Boolean;
FTasks: TTaskItems;
FTimeNow: Integer;
FOnWorkDayStarted: TNotifyEvent;
FOnTaskNotify: TTaskNotify;
FNotifyWaitList: TNotifyWaitList;
procedure SetDataBase(const Value: TDB);
procedure OnTimerCheck(Sender: TObject);
procedure SetActivate(const Value: Boolean);
procedure SetOnWorkDayStarted(const Value: TNotifyEvent);
procedure SetOnTaskNotify(const Value: TTaskNotify);
protected
procedure Notify(Task: TTaskItem);
procedure WorkDayStarted;
public
constructor Create(ADataBase: TDB);
destructor Destroy; override;
procedure AddNotifyWait(TaskID: Integer; Time: TTime);
function MoveTask(TaskID: Integer; NewDate: TDate): Boolean;
property DataBase: TDB read FDataBase write SetDataBase;
property Activate: Boolean read FActivate write SetActivate;
property OnWorkDayStarted: TNotifyEvent read FOnWorkDayStarted write SetOnWorkDayStarted;
property OnTaskNotify: TTaskNotify read FOnTaskNotify write SetOnTaskNotify;
end;
var
Manager: TManager;
implementation
uses
YOTM.Main;
{ TManager }
procedure TManager.AddNotifyWait(TaskID: Integer; Time: TTime);
var Item: TNotifyWait;
begin
Item.TaskID := TaskID;
Item.Time := Time;
FNotifyWaitList.Add(Item);
end;
constructor TManager.Create(ADataBase: TDB);
begin
FDataBase := ADataBase;
FTimerCheck := TTimer.Create(nil);
FTimerCheck.Enabled := False;
FTimerCheck.Interval := MSecsPerSec * SecsPerMin;
FTimerCheck.OnTimer := OnTimerCheck;
FTimeNow := GetMins(Now);
FTasks := TTaskItems.Create(FDataBase, nil);
FNotifyWaitList := TNotifyWaitList.Create;
end;
destructor TManager.Destroy;
begin
FTasks.Free;
FTimerCheck.Free;
FNotifyWaitList.Free;
inherited;
end;
function TManager.MoveTask(TaskID: Integer; NewDate: TDate): Boolean;
var Task: TTaskItem;
begin
Task := FTasks.GetItem(TaskID);
Task.DateDeadline := NewDate;
Task.SaveIt;
FTasks.Update(Task);
Task.Free;
end;
procedure TManager.Notify(Task: TTaskItem);
begin
if Assigned(FOnTaskNotify) then
FOnTaskNotify(Task);
end;
procedure TManager.OnTimerCheck(Sender: TObject);
var
i: Integer;
TkTime: Integer;
DT: TDateTime;
Item: TTaskItem;
begin
DT := Now;
TkTime := GetMins(DT);
FTimeNow := TkTime;
FTasks.ShowDate := DT;
FTasks.TaskFilter := tkfDated;
FTasks.Upcoming := True;
FTasks.Reload;
for i := 0 to FTasks.Count - 1 do
begin
TkTime := GetMins(FTasks[i].TimeNotify);
if TkTime <= FTimeNow then
Notify(FTasks[i]);
end;
//Åñëè åñòü îæèäàþùèå óâåäîìëåíèÿ
FTasks.Reload;
if FNotifyWaitList.Count > 0 then
begin
i := 0;
while i < FNotifyWaitList.Count do
begin
Item := FTasks.GetItem(FNotifyWaitList[i].TaskID);
if Assigned(Item) then
begin
TkTime := GetMins(FNotifyWaitList[i].Time);
if TkTime <= FTimeNow then
begin
Notify(Item);
FNotifyWaitList.Delete(i);
end
else Inc(i);
end
else FNotifyWaitList.Delete(i);
end;
end;
end;
procedure TManager.SetActivate(const Value: Boolean);
begin
FActivate := Value;
FTimerCheck.Enabled := Value;
if Value then
OnTimerCheck(nil);
end;
procedure TManager.SetDataBase(const Value: TDB);
begin
FDataBase := Value;
end;
procedure TManager.SetOnTaskNotify(const Value: TTaskNotify);
begin
FOnTaskNotify := Value;
end;
procedure TManager.SetOnWorkDayStarted(const Value: TNotifyEvent);
begin
FOnWorkDayStarted := Value;
end;
procedure TManager.WorkDayStarted;
begin
if Assigned(FOnWorkDayStarted) then
FOnWorkDayStarted(Self);
end;
end.