-
Notifications
You must be signed in to change notification settings - Fork 6
/
uSettings.pas
350 lines (299 loc) · 8.03 KB
/
uSettings.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
unit uSettings;
interface
uses
// Delphi
System.SysUtils,
// FireDACE
FireDAC.Comp.Client,
// Project
uDatabase;
type
TGlucoseUnit = (guMMOL, guMGDL);
TInsulinTime = (itMorning, itDay, itEvening);
TSettings = class
private
FQuery: TFDQuery;
FFirstTime: Boolean;
FDatabase: TDatabase;
function GetIOB: Boolean;
procedure SetIOB(Value: Boolean);
function GetAppBadge: Boolean;
procedure SetAppBadge(Value: Boolean);
function GetTarget(Time: TInsulinTime): REAL;
procedure SetTarget(Time: TInsulinTime; Value: REAL);
function GetAnimations: Boolean;
procedure SetAnimations(Value: Boolean);
function GetCarbF(Time: TInsulinTime): Integer;
procedure SetCarbF(Time: TInsulinTime; Value: Integer);
function GetCorrF(Time: TInsulinTime): REAL;
procedure SetCorrF(Time: TInsulinTime; Value: REAL);
function GetGlucoseUnit: TGlucoseUnit;
procedure SetGlucoseUnit(Value: TGlucoseUnit);
function GetQuery: TFDQuery;
function GetDatabase: TDatabase;
procedure AfterConnect(Sender: TObject);
property Query: TFDQuery read GetQuery;
property Database: TDatabase read GetDatabase;
public
constructor Create;
destructor Destroy; override;
property FirstTime: Boolean read FFirstTime;
property IOB: Boolean read GetIOB write SetIOB;
property AppBadge: Boolean read GetAppBadge write SetAppBadge;
property Target[Time: TInsulinTime]: REAL read GetTarget write SetTarget;
property Animations: Boolean read GetAnimations write SetAnimations;
property GlucoseUnit: TGlucoseUnit read GetGlucoseUnit write SetGlucoseUnit;
property CarbF[Time: TInsulinTime]: Integer read GetCarbF write SetCarbF;
property CorrF[Time: TInsulinTime]: REAL read GetCorrF write SetCorrF;
end;
const
CARB1 = 0;
CARB2 = 0;
CARB3 = 0;
CORR1 = 0.0;
CORR2 = 0.0;
CORR3 = 0.0;
TARGET1 = 5.8;
TARGET2 = 5.8;
TARGET3 = 8.0;
const
GlucoseUnitText: array[TGlucoseUnit] of string = ('mmol/L', 'mg/dL');
function FormatSettingsFloat: TFormatSettings;
function GetInsulinTime: TInsulinTime;
function Settings: TSettings;
implementation
uses
// Delphi
Data.DB;
function FormatSettingsFloat: TFormatSettings;
begin
Result := FormatSettings;
Result.DecimalSeparator := '.';
end;
function GetInsulinTime: TInsulinTime;
begin
Result := itMorning;
if Time > 11/24 then
if Time > 16/24 then
Result := itEvening
else
Result := itDay;
end;
var
varSettings: TSettings = nil;
function Settings: TSettings;
begin
if not Assigned(varSettings) then
varSettings := TSettings.Create;
Result := varSettings;
end;
{ TSettings }
constructor TSettings.Create;
begin
inherited;
FQuery := nil;
FDatabase := nil;
end;
destructor TSettings.Destroy;
begin
if Assigned(FQuery) then
try
if FQuery.Active then
FQuery.Close;
finally
FQuery.Free;
end;
if Assigned(FDatabase) then
FDatabase.Free;
inherited Destroy;
end;
function TSettings.GetDatabase: TDatabase;
begin
if not Assigned(FDatabase) then
begin
FDatabase := TDatabase.Create;
FDatabase.Connection.AfterConnect := AfterConnect;
end;
Result := FDatabase;
end;
function TSettings.GetQuery: TFDQuery;
begin
if not Assigned(FQuery) then
begin
FQuery := TFDQuery.Create(nil);
FQuery.Connection := Database.Connection;
end;
Result := FQuery;
if not Result.Active then
Result.Open('SELECT * FROM SETTINGS');
end;
procedure TSettings.AfterConnect;
begin
FFirstTime := not Database.TableExists('SETTINGS');
if FirstTime then
if Database.Execute('CREATE TABLE SETTINGS (' +
'ID INTEGER PRIMARY KEY, TARGET1 REAL, TARGET2 REAL, TARGET3 REAL, IOB INTEGER, ANIMATIONS INTEGER' +
', CARB1 INTEGER, CARB2 INTEGER, CARB3 INTEGER' +
', CORR1 REAL, CORR2 REAL, CORR3 REAL);') > -1 then
Database.Execute(Format('INSERT INTO SETTINGS (' +
'TARGET1, TARGET2, TARGET3, IOB, ANIMATIONS, CARB1, CARB2, CARB3, CORR1, CORR2, CORR3) ' +
'VALUES(%.2f, %.2f, %.2f, 1, 1, %d, %d, %d, %.2f, %.2f, %.2f);',
[TARGET1, TARGET2, TARGET3, CARB1, CARB2, CARB3, CORR1, CORR2, CORR3], FormatSettingsFloat));
end;
function TSettings.GetIOB: Boolean;
begin
Result := Query.FieldByName('IOB').AsInteger <> 0;
end;
procedure TSettings.SetIOB(Value: Boolean);
begin
Query.Edit;
try
Query.FieldByName('IOB').AsInteger := Ord(Value);
finally
Query.Post;
end;
end;
function TSettings.GetAppBadge: Boolean;
var
F: TField;
begin
Result := True;
F := Query.FindField('AppBadge');
if Assigned(F) then
Result := F.AsInteger <> 0;
end;
procedure TSettings.SetAppBadge(Value: Boolean);
var
F: TField;
begin
F := Query.FindField('AppBadge');
if not Assigned(F) then
if Database.Execute('ALTER TABLE SETTINGS ADD COLUMN AppBadge INTEGER') > 0 then
begin
Database.Execute(Format('UPDATE SETTINGS SET AppBadge = %d', [Ord(Value)]));
Database.Connection.Close;
EXIT;
end;
if Assigned(F) then
begin
Query.Edit;
try
F.AsInteger := Ord(Value);
finally
Query.Post;
end;
end;
end;
function TSettings.GetTarget(Time: TInsulinTime): REAL;
begin
if Time = itEvening then
Result := Query.FieldByName('TARGET3').AsFloat
else if Time = itDay then
Result := Query.FieldByName('TARGET2').AsFloat
else
Result := Query.FieldByName('TARGET1').AsFloat;
end;
procedure TSettings.SetTarget(Time: TInsulinTime; Value: REAL);
begin
Query.Edit;
try
case Time of
itMorning: Query.FieldByName('TARGET1').AsFloat := Value;
itDay : Query.FieldByName('TARGET2').AsFloat := Value;
itEvening: Query.FieldByName('TARGET3').AsFloat := Value;
end;
finally
Query.Post;
end;
end;
function TSettings.GetAnimations: Boolean;
begin
Result := Query.FieldByName('ANIMATIONS').AsInteger <> 0;
end;
procedure TSettings.SetAnimations(Value: Boolean);
begin
Query.Edit;
try
Query.FieldByName('ANIMATIONS').AsInteger := Ord(Value);
finally
Query.Post;
end;
end;
function TSettings.GetCarbF(Time: TInsulinTime): Integer;
begin
if Time = itMorning then
Result := Query.FieldByName('CARB1').AsInteger
else if Time = itDay then
Result := Query.FieldByName('CARB2').AsInteger
else
Result := Query.FieldByName('CARB3').AsInteger;
end;
procedure TSettings.SetCarbF(Time: TInsulinTime; Value: Integer);
begin
Query.Edit;
try
if Time = itMorning then
Query.FieldByName('CARB1').AsInteger := Value
else if Time = itDay then
Query.FieldByName('CARB2').AsInteger := Value
else
Query.FieldByName('CARB3').AsInteger := Value;
finally
Query.Post;
end;
end;
function TSettings.GetCorrF(Time: TInsulinTime): REAL;
begin
if Time = itMorning then
Result := Query.FieldByName('CORR1').AsFloat
else if Time = itDay then
Result := Query.FieldByName('CORR2').AsFloat
else
Result := Query.FieldByName('CORR3').AsFloat;
end;
procedure TSettings.SetCorrF(Time: TInsulinTime; Value: REAL);
begin
Query.Edit;
try
if Time = itMorning then
Query.FieldByName('CORR1').AsFloat := Value
else if Time = itDay then
Query.FieldByName('CORR2').AsFloat := Value
else
Query.FieldByName('CORR3').AsFloat := Value;
finally
Query.Post;
end;
end;
function TSettings.GetGlucoseUnit: TGlucoseUnit;
var
F: TField;
begin
Result := guMMOL;
F := Query.FindField('GlucoUnit');
if Assigned(F) then
Result := TGlucoseUnit(F.AsInteger);
end;
procedure TSettings.SetGlucoseUnit(Value: TGlucoseUnit);
var
F: TField;
begin
F := Query.FindField('GlucoUnit');
if not Assigned(F) then
if Database.Execute('ALTER TABLE SETTINGS ADD COLUMN GlucoUnit INTEGER') > 0 then
begin
Database.Execute(Format('UPDATE SETTINGS SET GlucoUnit = %d', [Ord(Value)]));
Database.Connection.Close;
EXIT;
end;
if Assigned(F) then
begin
Query.Edit;
try
F.AsInteger := Ord(Value);
finally
Query.Post;
end;
end;
end;
end.