-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotification.pas
More file actions
278 lines (249 loc) · 8.21 KB
/
Copy pathNotification.pas
File metadata and controls
278 lines (249 loc) · 8.21 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Unit Notification;
Interface
Uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.WinXCalendars, System.IOUtils,
Vcl.WinXPickers, System.Actions, Vcl.ActnList, Vcl.Samples.Spin, DateUtils, Vcl.ExtCtrls,
System.Notification;
Type
TNotificationForm = class(TForm)
DescriptionComboBox: TComboBox;
CustomDesc: TEdit;
SelectDesc: TLabel;
SelectTime: TLabel;
TimePicker: TTimePicker;
DatePicker: TCalendarPicker;
InformationMemo: TMemo;
AddButton: TButton;
NotificationsActionList: TActionList;
aSetComboBox: TAction;
DeleteSpinEdit: TSpinEdit;
DeleteButton: TButton;
NotificationTime: TTimer;
NotificationCenter: TNotificationCenter;
aLoadAndClose: TAction;
procedure FormActivate(Sender: TObject);
Procedure aSetComboBoxExecute(Sender: TObject);
Procedure DescriptionComboBoxChange(Sender: TObject);
Procedure AddButtonClick(Sender: TObject);
Procedure DeleteButtonClick(Sender: TObject);
Procedure NotificationTimeTimer(Sender: TObject);
Procedure aLoadAndCloseExecute(Sender: TObject);
Private
{ Private declarations }
Public
{ Public declarations }
End;
Var
NotificationForm: TNotificationForm;
Implementation
{$R *.dfm}
Uses Main;
Type
TNotificationInfo = Record
Description: String[128];
NotificationTime: TDateTime;
End;
Const
FILE_NOTIFICATIONS: String = '\ReMind\notification.dat';
Var
ANotifications: Array of TNotificationInfo;
Procedure QuickSort(var A: Array of TNotificationInfo; Min, Max: Integer);
Var
I, J: Integer;
Supp: TDateTime;
Temp: TNotificationInfo;
Begin
Supp := A[Max-((max-min) div 2)].NotificationTime;
I := Min;
J := Max;
While I<j do
Begin
While A[I].NotificationTime < Supp do
I := I + 1;
While A[J].NotificationTime > Supp do
J := J - 1;
If i<=j then
Begin
Temp := A[I];
A[I] := A[J];
A[J] := Temp;
I := I + 1;
J := J - 1;
End;
End;
If Min < j then
QuickSort(A, min, J);
If I < Max then
QuickSort(A, I, Max);
End;
Procedure AddNewNotificationToFile();
Var
FNotification: File of TNotificationInfo;
I: Integer;
Begin
AssignFile(FNotification, TPath.GetDocumentsPath + FILE_NOTIFICATIONS);
Rewrite(FNotification);
For I := 0 to Length(ANotifications) - 1 do
Begin
Write(FNotification, ANotifications[I]);
End;
Close(FNotification);
End;
Procedure AddNewNotificationToArray(Notification: TNotificationInfo);
Begin
SetLength(ANotifications, Length(ANotifications) + 1);
ANotifications[Length(ANotifications) - 1] := Notification;
End;
Procedure ReadFileData();
Var
FNotification: File of TNotificationInfo;
Notification: TNotificationInfo;
Begin
AssignFile(FNotification, TPath.GetDocumentsPath + FILE_NOTIFICATIONS);
If not FileExists(TPath.GetDocumentsPath + FILE_NOTIFICATIONS) then
Begin
Rewrite(FNotification);
End
Else
Reset(FNotification);
While not Eof(FNotification) do
Begin
Read(FNotification, Notification);
AddNewNotificationToArray(Notification);
End;
Close(FNotification);
End;
Procedure PrintNotifications(InformationMemo: TMemo);
Var
I: Integer;
Begin
InformationMemo.Clear;
For I := 0 to Length(ANotifications) - 1 do
Begin
With ANotifications[I] do
Begin
InformationMemo.Lines.Add(Description + ' ' + TimeToStr(NotificationTime) + ' • ' + DateToStr(NotificationTime));
End;
End;
End;
Procedure SetSpinEditValues(SE: TSpinEdit; DeleteButton: TButton);
Begin
If Length(ANotifications) = 0 then
Begin
SE.Text := '0';
SE.MinValue := 0;
End
Else
SE.MinValue := 1;
SE.MaxValue := Length(ANotifications);
DeleteButton.Enabled := Length(ANotifications) > 0;
End;
Procedure TNotificationForm.AddButtonClick(Sender: TObject);
Var
NewNotification: TNotificationInfo;
NotificationTime: TDateTime;
Begin
If DescriptionComboBox.ItemIndex = DescriptionComboBox.Items.Count - 1 then
NewNotification.Description := CustomDesc.Text
Else
NewNotification.Description := DescriptionComboBox.Text;
ReplaceDate(NewNotification.NotificationTime, DatePicker.Date);
ReplaceTime(NewNotification.NotificationTime, TimePicker.Time);
AddNewNotificationToArray(NewNotification);
AddNewNotificationToFile();
QuickSort(ANotifications, 0, Length(ANotifications) - 1);
PrintNotifications(InformationMemo);
aSetComboBox.Execute;
CustomDesc.Text := '';
CustomDesc.Visible := False;
SetSpinEditValues(DeleteSpinEdit, DeleteButton);
End;
Procedure TNotificationForm.aLoadAndCloseExecute(Sender: TObject);
Begin
Self.Close;
End;
Procedure TNotificationForm.aSetComboBoxExecute(Sender: TObject);
Var
I: Integer;
Begin
DescriptionComboBox.Clear;
For I := 1 to MainForm.MainGrid.RowCount - 1 do
Begin
DescriptionComboBox.Items.Add(MainForm.MainGrid.Cells[1, I]);
End;
DescriptionComboBox.Items.Add('Custom...');
DatePicker.Date := Now;
TimePicker.Time := Now;
End;
Procedure DeleteArrayElement(DIndex: Integer);
Var
I: Integer;
Begin
For I := DIndex to Length(ANotifications) - 2 do
Begin
ANotifications[I] := ANotifications[I + 1];
End;
SetLength(ANotifications, Length(ANotifications) - 1);
End;
Procedure TNotificationForm.DeleteButtonClick(Sender: TObject);
Var
DeletingItem: Integer;
Begin
DeletingItem := StrToInt(DeleteSpinEdit.Text) - 1;
DeleteArrayElement(DeletingItem);
PrintNotifications(InformationMemo);
SetSpinEditValues(DeleteSpinEdit, DeleteButton);
AddNewNotificationToFile();
End;
Procedure TNotificationForm.DescriptionComboBoxChange(Sender: TObject);
Begin
CustomDesc.Visible := DescriptionComboBox.ItemIndex = DescriptionComboBox.Items.Count - 1;
End;
Procedure TNotificationForm.FormActivate(Sender: TObject);
Begin
InformationMemo.Clear;
SetLength(ANotifications, 0);
ReadFileData();
If Length(ANotifications) <> 0 then
QuickSort(ANotifications, 0, Length(ANotifications) - 1);
PrintNotifications(InformationMemo);
SetSpinEditValues(DeleteSpinEdit, DeleteButton);
End;
Procedure TNotificationForm.NotificationTimeTimer(Sender: TObject);
Var
Hour, Min, Sec, MSec: Word;
FHour, FMin, FSec, FMSec: Word;
NewTime, ExistingTime: TDateTime;
NewNotification: TNotification;
Begin
If Length(ANotifications) > 0 then
Begin
If CompareDate(Date, ANotifications[0].NotificationTime) = 0 then
Begin
DecodeTime(Now, Hour, Min, Sec, MSec);
NewTime := EncodeTime(Hour, Min, 0, 0);
ExistingTime := ANotifications[0].NotificationTime;
DecodeTime(ExistingTime, FHour, FMin, FSec, FMSec);
ExistingTime := EncodeTime(FHour, FMin, 0, 0);
If CompareTime(NewTime, ExistingTime) = 0 then
Begin
NewNotification := NotificationCenter.CreateNotification;
Try
NewNotification.Name := 'Name';
NewNotification.Title := 'Re:Minder Notification';
With ANotifications[0] do
NewNotification.AlertBody := Description + #13#10 + TimeToStr(NotificationTime) + ' • ' + DateToStr(NotificationTime);
NotificationCenter.PresentNotification(NewNotification);
Finally
NewNotification.Free;
End;
DeleteArrayElement(0);
PrintNotifications(InformationMemo);
SetSpinEditValues(DeleteSpinEdit, DeleteButton);
AddNewNotificationToFile();
End;
End;
End;
End;
End.