-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuModel.pas
173 lines (146 loc) · 3.93 KB
/
uModel.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
unit uModel;
interface
uses
Classes,
SysUtils;
//System.Generics.Collections;
type
TToDoItem = class(TObject)
private
FCreatedDate: TDateTime;
FDescription: string;
FCompleted: boolean;
FCompletedDate: TDateTime;
FPriority: Char;
procedure SetCompleted(const Value: boolean);
procedure SetCompletedDate(const Value: TDateTime);
procedure SetCreatedDate(const Value: TDateTime);
procedure SetDescription(const Value: string);
procedure CheckCreatedDate;
procedure SetPriority(const Value: Char);
public
constructor Create; overload;
constructor Create(AString: string); overload;
function AsString: string;
function IsValidCreatedDate: boolean;
property Completed: boolean read FCompleted write SetCompleted;
property Priority: Char read FPriority write SetPriority;
property CompletedDate: TDateTime read FCompletedDate write SetCompletedDate;
property CreatedDate: TDateTime read FCreatedDate write SetCreatedDate;
property Description: string read FDescription write SetDescription;
end;
implementation
uses
Character;
function StrToken(var S: string; Separator: Char): string;
var
NPos: integer;
begin
NPos := Pos(Separator, S);
if NPos >= 1 then
begin
Result := Copy(S, 1, NPos-1);
S := Trim(Copy(S, NPos, MaxInt));
end
else
begin
Result := S;
S := '';
end;
end;
{ TToDoItem }
procedure TToDoItem.CheckCreatedDate;
begin
if FCreatedDate < EncodeDate(1980, 1, 1) then
FCreatedDate := Date;
end;
constructor TToDoItem.Create(AString: string);
var
WorkString: string;
Token: string;
Settings: TFormatSettings;
begin
self.Create;
if AString = '' then
Exit;
//Settings := TFormatSettings.Create;
Settings.ShortDateFormat := 'yyyy-mm-dd';
Settings.DateSeparator := '-';
WorkString := AString;
FCompleted := WorkString.StartsWith('x ');
if FCompleted then
WorkString := WorkString.Substring(2);
if WorkString[1] = '(' then
begin
Token := StrToken(WorkString, ' ');
FPriority := Token[2];
end;
if WorkString[1].IsDigit then
begin
Token := StrToken(WorkString, ' ');
FCreatedDate := StrToDate(Token, Settings);
end;
if WorkString[1].IsDigit then
begin
Token := StrToken(WorkString, ' ');
FCompletedDate := FCreatedDate;
FCreatedDate := StrToDate(Token, Settings);
end;
self.FDescription := WorkString;
end;
function TToDoItem.IsValidCreatedDate: boolean;
begin
Result := FCreatedDate > EncodeDate(1980, 1, 1);
end;
constructor TToDoItem.Create;
begin
FCreatedDate := 0;
FPriority := '-';
FDescription := 'Empty';
FCompleted := False;
FCompletedDate := 0;
end;
procedure TToDoItem.SetCompleted(const Value: boolean);
begin
FCompleted := Value;
if FCompleted then
begin
FCompletedDate := Now;
CheckCreatedDate;
end;
end;
procedure TToDoItem.SetCompletedDate(const Value: TDateTime);
begin
FCompletedDate := Value;
FCompleted := True;
CheckCreatedDate;
end;
procedure TToDoItem.SetCreatedDate(const Value: TDateTime);
begin
FCreatedDate := Value;
end;
procedure TToDoItem.SetDescription(const Value: string);
begin
FDescription := Value;
end;
procedure TToDoItem.SetPriority(const Value: Char);
begin
FPriority := Value;
end;
function TToDoItem.AsString: string;
function DateValToStr(ADate: TDateTime): string; begin
Result := FormatDateTime('yyyy-mm-dd', ADate);
end;
begin
Result := '';
if self.Completed then
Result := Result + 'x ';
if self.FPriority <> '-' then
Result := Result + '(' + self.FPriority + ') ';
if self.FCompletedDate > 1 then
Result := Result + DateValToStr(self.FCompletedDate) + ' ';
if self.FCreatedDate > 1 then
Result := Result + DateValToStr(self.FCreatedDate) + ' ';
Result := Result + self.FDescription;
end;
end.