-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.pas
444 lines (389 loc) · 12.5 KB
/
Main.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
unit Main;
interface
uses
// Delphi
System.Classes,
System.ImageList,
// FireMonkey
FMX.Controls,
FMX.Controls.Presentation,
FMX.Edit,
FMX.Forms,
FMX.ImgList,
FMX.Layouts,
FMX.ListBox,
FMX.Objects,
FMX.Platform,
FMX.StdCtrls,
FMX.TabControl,
FMX.Types,
// Project
Delay,
Misc;
type
TfrmMain = class(TForm)
TC: TTabControl;
tabCalculate: TTabItem;
tabIOB: TTabItem;
tabSettings: TTabItem;
TB: TToolBar;
btnAuto: TSpeedButton;
btnMorning: TSpeedButton;
btnDay: TSpeedButton;
btnEvening: TSpeedButton;
GB: TGroupBox;
lblUnits: TLabel;
Line1: TLine;
GL: TGridPanelLayout;
lblGlucoseLeft: TLabel;
rctGlucose: TRoundRect;
btnGlucose: TButton;
lblGlucoseRight: TLabel;
lblCarbsLeft: TLabel;
rctCarbs: TRoundRect;
btnCarbs: TButton;
lblCarbsRight: TLabel;
LB: TListBox;
lbhTarget: TListBoxGroupHeader;
lbiTargetMorning: TListBoxItem;
lbiTargetDay: TListBoxItem;
lbiTargetEvening: TListBoxItem;
edtTargetMorning: TEdit;
edtTargetDay: TEdit;
edtTargetEvening: TEdit;
IL: TImageList;
lghCarbRatio: TListBoxGroupHeader;
lbiCarbRatioMorning: TListBoxItem;
lbiCarbRatioDay: TListBoxItem;
lbiCarbRatioEvening: TListBoxItem;
edtCarbRatioMorning: TEdit;
edtCarbRatioDay: TEdit;
edtCarbRatioEvening: TEdit;
lghInjectionSchedule: TListBoxGroupHeader;
lbiAbove8: TListBoxItem;
edtAbove8: TEdit;
lbiAbove10: TListBoxItem;
edtAbove10: TEdit;
lbiAbove12: TListBoxItem;
edtAbove12: TEdit;
lbiAbove14: TListBoxItem;
edtAbove14: TEdit;
lbiAbove16: TListBoxItem;
edtAbove16: TEdit;
lbiAbove18: TListBoxItem;
edtAbove18: TEdit;
lbiAbove20: TListBoxItem;
edtAbove20: TEdit;
lbiAbove22: TListBoxItem;
edtAbove22: TEdit;
lbiAbove24: TListBoxItem;
edtAbove24: TEdit;
procedure btnGlucoseClick(Sender: TObject);
procedure btnCarbsClick(Sender: TObject);
procedure tbClick(Sender: TObject);
procedure edtTargetMorningExit(Sender: TObject);
procedure edtTargetDayExit(Sender: TObject);
procedure edtTargetEveningExit(Sender: TObject);
procedure edtCarbRatioKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
procedure edtCarbRatioMorningExit(Sender: TObject);
procedure edtCarbRatioDayExit(Sender: TObject);
procedure edtCarbRatioEveningExit(Sender: TObject);
procedure edtAboveExit(Sender: TObject);
procedure edtKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
private
FDelay: IDelay;
function ApplicationEvent(aAppEvent: TApplicationEvent; aContext: TObject): Boolean;
procedure Calculate;
procedure edtTargetExit(edit: TEdit; time: TInsulinTime; default: Double);
function GetButtonTime(btn: TSpeedButton): TInsulinTime;
procedure GlucoseUnitChanged(Sender: TObject);
procedure InsulinUnitChanged(Sender: TObject);
function GetUnits: Double;
procedure SetUnits(Value: Double);
function GetGlucose: TBloodGlucose;
procedure SetGlucose(Value: TBloodGlucose);
function GetCarbs: Integer;
procedure SetCarbs(Value: Integer);
public
constructor Create(aOwner: TComponent); override;
property Units: Double read GetUnits write SetUnits;
property Glucose: TBloodGlucose read GetGlucose write SetGlucose;
property Carbs: Integer read GetCarbs write SetCarbs;
end;
var
frmMain: TfrmMain;
implementation
{$R *.fmx}
uses
// Delphi
System.SysUtils,
System.UITypes,
// FireMonkey
FMX.DialogService,
{$IFDEF IOS}
FMX.Platform.iOS,
{$ENDIF}
// Project
Keyboard,
Settings;
{ TfrmMain }
constructor TfrmMain.Create(aOwner: TComponent);
resourcestring
RS_FIRST_TIME = 'Always consult your physician or diabetes clinic before use and please adjust the settings before using the calculator for the first time.';
begin
inherited Create(aOwner);
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService) then
begin
const AE = IFMXApplicationEventService(TPlatformServices.Current.GetPlatformService(IFMXApplicationEventService));
AE.SetApplicationEventHandler(Self.ApplicationEvent);
end;
const settings = Settings.Get;
settings.OnGlucoseUnitChange := GlucoseUnitChanged;
settings.OnInsulinUnitChange := InsulinUnitChanged;
GlucoseUnitChanged(Self);
edtTargetMorning.Text := settings.Target[itMorning].Format;
edtTargetDay.Text := settings.Target[itDay].Format;
edtTargetEvening.Text := settings.Target[itEvening].Format;
edtCarbRatioMorning.Text := IntToStr(settings.CarbRatio[itMorning]);
edtCarbRatioDay.Text := IntToStr(settings.CarbRatio[itDay]);
edtCarbRatioEvening.Text := IntToStr(settings.CarbRatio[itEvening]);
edtAbove8.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(8)]], TSettings.Format);
edtAbove10.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(10)]], TSettings.Format);
edtAbove12.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(12)]], TSettings.Format);
edtAbove14.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(14)]], TSettings.Format);
edtAbove16.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(16)]], TSettings.Format);
edtAbove18.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(18)]], TSettings.Format);
edtAbove20.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(20)]], TSettings.Format);
edtAbove22.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(22)]], TSettings.Format);
edtAbove24.Text := Format('%.1f', [settings.CorrUnits[TBloodGlucose.Create(24)]], TSettings.Format);
if settings.FirstTime then
begin
FDelay := Delay.Create;
FDelay.&Set(procedure
begin
TDialogService.MessageDialog(RS_FIRST_TIME, TMsgDlgType.mtWarning, [TMsgDlgBtn.mbOK], TMsgDlgBtn.mbOK, 0, procedure(const aResult: TModalResult)
begin
TC.ActiveTab := tabSettings;
end);
end, 500);
end;
TC.ActiveTab := tabCalculate;
end;
function TfrmMain.ApplicationEvent(aAppEvent: TApplicationEvent; aContext: TObject): Boolean;
begin
{$IFDEF IOS}
if aAppEvent = TApplicationEvent.OpenURL then
if Assigned(aContext) then
if aContext is TiOSOpenApplicationContext then
begin
var S := TiOSOpenApplicationContext(aContext).URL;
var I := Pos('carbs=', S);
if I > 0 then
begin
Delete(S, Low(S), I + 5);
// delete non-numeric characters
I := Low(S);
while I < Length(S) do
if CharInSet(S[I], ['0'..'9']) then
Inc(I)
else
Delete(S, I, MaxInt);
// do we have carbs?
if S <> '' then
begin
btnCarbs.Text := S;
Self.Calculate;
end;
end;
end;
{$ENDIF}
Result := True;
end;
procedure TfrmMain.Calculate;
begin
const glucose = Self.Glucose;
if (glucose.ToMMOL < 4) or (glucose.ToMMOL > 10) then
btnGlucose.TextSettings.FontColor := TAlphaColorRec.Darkred
else
btnGlucose.TextSettings.FontColor := TAlphaColorRec.Null;
var Result := 0.0;
try
var IT := InsulinTime;
if not btnAuto.IsPressed then
begin
const aParent = btnAuto.Parent;
for var I := 0 to Pred(aParent.ChildrenCount) do
if aParent.Children[I] is TSpeedButton then
if TSpeedButton(aParent.Children[I]) <> btnAuto then
if TSpeedButton(aParent.Children[I]).IsPressed then
begin
IT := GetButtonTime(TSpeedButton(aParent.Children[I]));
BREAK;
end;
end;
const carbRatio = Settings.Get.CarbRatio[IT];
if carbRatio = 0 then
EXIT;
Result := Self.Carbs / carbRatio;
if glucose = 0 then
EXIT;
const target = Settings.Get.Target[IT];
if glucose.ToMMOL > 8 then
Result := Result + Settings.Get.CorrUnitsEx(glucose)
else
if glucose < target then
begin
const corrRatio = Settings.Get.CorrRatio(IT);
if corrRatio > 0 then
Result := Result + ((glucose - target) / corrRatio);
end;
finally
Units := RoundInsulin(Result);
end;
end;
function TfrmMain.GetButtonTime(btn: TSpeedButton): TInsulinTime;
begin
Result := itMorning;
if btn = btnDay then
Result := itDay
else if btn = btnEvening then
Result := itEvening;
end;
procedure TfrmMain.GlucoseUnitChanged(Sender: TObject);
begin
const text = GlucoseUnitText[Settings.Get.GlucoseUnit];
lblGlucoseRight.Text := text;
lbhTarget.Text := Format('Target (%s)', [text]);
for var I in [lbiAbove8, lbiAbove10, lbiAbove12, lbiAbove14, lbiAbove16, lbiAbove18, lbiAbove20, lbiAbove22, lbiAbove24] do
I.Text := Format('Above %d %s', [I.Children[0].Tag, text]);
end;
procedure TfrmMain.InsulinUnitChanged(Sender: TObject);
begin
Self.Calculate;
end;
function TfrmMain.GetUnits: Double;
begin
Result := StrToFloatDef(lblUnits.Text, 0, TSettings.Format);
end;
procedure TfrmMain.SetUnits(Value: Double);
begin
case Settings.Get.InsulinUnit of
iuHalf : lblUnits.Text := Format('%.1f', [Value], TSettings.Format);
iuWhole: lblUnits.Text := Format('%.0f', [Value], TSettings.Format);
end;
end;
function TfrmMain.GetGlucose: TBloodGlucose;
begin
Result := TBloodGlucose.Create(StrToFloat(btnGlucose.Text, TSettings.Format));
end;
procedure TfrmMain.SetGlucose(Value: TBloodGlucose);
begin
btnGlucose.Text := Value.Format;
end;
function TfrmMain.GetCarbs: Integer;
begin
Result := StrToIntDef(btnCarbs.Text, 0);
end;
procedure TfrmMain.SetCarbs(Value: Integer);
begin
btnCarbs.Text := IntToStr(Value);
end;
{ event handlers }
procedure TfrmMain.tbClick(Sender: TObject);
begin
Self.Calculate;
end;
procedure TfrmMain.btnGlucoseClick(Sender: TObject);
begin
const keyboard = Keyboard.Get(Self.Calculate);
if keyboard.Button = Sender then
begin
keyboard.Button := nil;
EXIT;
end;
case Settings.Get.GlucoseUnit of
guMMOL: begin
Keyboard.Digits := 2;
Keyboard.Decimals := 1;
end;
guMGDL: begin
Keyboard.Digits := 3;
Keyboard.Decimals := 0;
end;
end;
keyboard.Button := Sender as TButton;
Self.Calculate;
end;
procedure TfrmMain.btnCarbsClick(Sender: TObject);
begin
const keyboard = Keyboard.Get(Self.Calculate);
if Keyboard.Button = Sender then
begin
Keyboard.Button := nil;
EXIT;
end;
Keyboard.Digits := 3;
Keyboard.Decimals := 0;
Keyboard.Button := Sender as TButton;
Self.Calculate;
end;
procedure TfrmMain.edtKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
if KeyChar = ',' then KeyChar := '.';
end;
procedure TfrmMain.edtTargetExit(edit: TEdit; time: TInsulinTime; default: Double);
begin
const T = TBloodGlucose.Create(
StrToFloatDef(edit.Text, (function(Value: MMOL): Double
begin
Result := Value;
if Settings.Get.GlucoseUnit = guMGDL then
Result := MMOL2MGDL(Result);
end)(default), TSettings.Format)
);
edit.Text := T.Format;
Settings.Get.Target[time] := TBloodGlucose.Create(StrToFloat(edit.Text, TSettings.Format));
end;
procedure TfrmMain.edtTargetMorningExit(Sender: TObject);
begin
edtTargetExit(edtTargetMorning, itMorning, 5.8);
end;
procedure TfrmMain.edtTargetDayExit(Sender: TObject);
begin
edtTargetExit(edtTargetDay, itDay, 5.8);
end;
procedure TfrmMain.edtTargetEveningExit(Sender: TObject);
begin
edtTargetExit(edtTargetEvening, itEvening, 8.0);
end;
procedure TfrmMain.edtCarbRatioKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
if (KeyChar = '.') or (KeyChar = ',') then KeyChar := #0;
end;
procedure TfrmMain.edtCarbRatioMorningExit(Sender: TObject);
begin
edtCarbRatioMorning.Text := IntToStr(StrToIntDef(edtCarbRatioMorning.Text, 0));
Settings.Get.CarbRatio[itMorning] := StrToInt(edtCarbRatioMorning.Text);
end;
procedure TfrmMain.edtCarbRatioDayExit(Sender: TObject);
begin
edtCarbRatioDay.Text := IntToStr(StrToIntDef(edtCarbRatioDay.Text, 0));
Settings.Get.CarbRatio[itDay] := StrToInt(edtCarbRatioDay.Text);
end;
procedure TfrmMain.edtCarbRatioEveningExit(Sender: TObject);
begin
edtCarbRatioEvening.Text := IntToStr(StrToIntDef(edtCarbRatioEvening.Text, 0));
Settings.Get.CarbRatio[itEvening] := StrToInt(edtCarbRatioEvening.Text);
end;
procedure TfrmMain.edtAboveExit(Sender: TObject);
begin
const E = Sender as TEdit;
const V = StrToFloatDef(E.Text, 0, TSettings.Format);
E.Text := Format('%.1f', [V], TSettings.Format);
Settings.Get.CorrUnits[TBloodGlucose.Create(E.Tag)] := StrToFloat(E.Text, TSettings.Format);
end;
end.