Skip to content

Commit 356d365

Browse files
committed
bk/2025-05-08-2310
1 parent 3bf6b84 commit 356d365

2 files changed

Lines changed: 85 additions & 86 deletions

File tree

WebClient/WebMainView.dfm

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
object MainView: TMainView
22
Width = 322
33
Height = 432
4+
Caption = 'lbUserID'
45
Font.Charset = DEFAULT_CHARSET
56
Font.Color = clWindowText
67
Font.Height = -13
@@ -11,50 +12,54 @@ object MainView: TMainView
1112
OnCreate = WebFormCreate
1213
OnExit = WebFormExit
1314
object WebLabel1: TWebLabel
14-
Left = 8
15-
Top = 11
15+
Left = 24
16+
Top = 27
1617
Width = 43
1718
Height = 16
1819
Caption = 'UserID:'
20+
ElementID = 'lbUserID'
1921
HeightPercent = 100.000000000000000000
2022
WidthPercent = 100.000000000000000000
2123
end
22-
object WebEdit1: TWebEdit
23-
Left = 57
24-
Top = 8
24+
object edtUserID: TWebEdit
25+
Left = 73
26+
Top = 24
2527
Width = 216
2628
Height = 19
29+
ElementID = 'edtUserID'
2730
HeightPercent = 100.000000000000000000
2831
WidthPercent = 100.000000000000000000
29-
OnChange = WebEdit1Change
32+
OnChange = edtUserIDChange
3033
end
31-
object WebButton1: TWebButton
32-
Left = 8
33-
Top = 33
34+
object btnWebSubscribe: TWebButton
35+
Left = 24
36+
Top = 49
3437
Width = 265
3538
Height = 25
3639
Caption = 'Subscribe for push notifications'
3740
ChildOrder = 1
41+
ElementID = 'btnSubscribe'
3842
HeightPercent = 100.000000000000000000
3943
WidthPercent = 100.000000000000000000
40-
OnClick = WebButton1Click
44+
OnClick = btnWebSubscribeClick
4145
end
42-
object WebButton2: TWebButton
43-
Left = 8
44-
Top = 64
46+
object btnWebUnsubscribe: TWebButton
47+
Left = 24
48+
Top = 80
4549
Width = 265
4650
Height = 25
4751
Caption = 'Unsubscribe from push notifications'
4852
ChildOrder = 2
53+
ElementID = 'btnUnsubscribe'
4954
HeightPercent = 100.000000000000000000
5055
WidthPercent = 100.000000000000000000
51-
OnClick = WebButton2Click
56+
OnClick = btnWebUnsubscribeClick
5257
end
5358
object WebPushNotifications1: TWebPushNotifications
5459
RegisterSubscriptionURL = 'http://localhost:8081/registerSubscription'
5560
UnregisterSubscriptionURL = 'http://localhost:8081/unregisterSubscription'
5661
VapidPublickeyURL = 'http://localhost:8081/vapidPublicKey'
57-
Left = 168
58-
Top = 120
62+
Left = 184
63+
Top = 136
5964
end
6065
end

WebClient/WebMainView.pas

Lines changed: 64 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@
33
interface
44

55
uses
6-
System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
7-
WEBLib.StdCtrls, WEBLib.Forms, WEBLib.Dialogs, WEBLib.PushNotifications,
8-
WEBLib.Storage, Vcl.StdCtrls, Vcl.Controls;
6+
System.SysUtils,
7+
System.Classes,
8+
JS,
9+
Web,
10+
WEBLib.Graphics,
11+
WEBLib.Controls,
12+
WEBLib.StdCtrls,
13+
WEBLib.Forms,
14+
WEBLib.Dialogs,
15+
WEBLib.PushNotifications,
16+
WEBLib.Storage,
17+
Vcl.StdCtrls,
18+
Vcl.Controls;
919

1020
type
1121
TMainView = class(TWebForm)
12-
WebEdit1: TWebEdit;
13-
WebButton1: TWebButton;
14-
WebButton2: TWebButton;
22+
edtUserID: TWebEdit;
23+
btnWebSubscribe: TWebButton;
24+
btnWebUnsubscribe: TWebButton;
1525
WebLabel1: TWebLabel;
1626
WebPushNotifications1: TWebPushNotifications;
17-
procedure WebButton1Click(Sender: TObject);
18-
procedure WebButton2Click(Sender: TObject);
19-
procedure WebEdit1Change(Sender: TObject);
27+
procedure btnWebSubscribeClick(Sender: TObject);
28+
procedure btnWebUnsubscribeClick(Sender: TObject);
29+
procedure edtUserIDChange(Sender: TObject);
2030
procedure WebFormCreate(Sender: TObject);
2131
procedure WebFormExit(Sender: TObject);
2232
private
23-
ls: TLocalStorage;
24-
procedure DoAddToLocalStorage;
25-
procedure DoRemoveFromLocalStorage;
33+
LLocalStorage: TLocalStorage;
2634
procedure SetButtonState;
27-
function ValidUserID: Boolean;
35+
procedure ValidUserID;
36+
public
2837
end;
2938

3039
var
@@ -34,95 +43,80 @@ implementation
3443

3544
{$R *.dfm}
3645

37-
procedure TMainView.DoAddToLocalStorage;
46+
const
47+
STORAGE_KEY = 'pushUserID';
48+
49+
procedure TMainView.WebFormCreate(Sender: TObject);
50+
begin
51+
LLocalStorage := TLocalStorage.Create(Self);
52+
if LLocalStorage.Count > 0 then
53+
edtUserID.Text := TLocalStorage.GetValue(STORAGE_KEY);
54+
55+
Self.SetButtonState;
56+
end;
57+
58+
procedure TMainView.WebFormExit(Sender: TObject);
3859
begin
39-
TWebLocalStorage.SetValue('pushUserID', WebEdit1.Text);
40-
SetButtonState;
60+
LLocalStorage.Free;
4161
end;
4262

43-
procedure TMainView.DoRemoveFromLocalStorage;
63+
procedure TMainView.edtUserIDChange(Sender: TObject);
4464
begin
45-
TWebLocalStorage.RemoveKey('pushUserID');
46-
SetButtonState;
65+
Self.SetButtonState;
4766
end;
4867

4968
procedure TMainView.SetButtonState;
5069
begin
51-
if WebEdit1.Text = '' then
70+
if edtUserID.Text = '' then
5271
begin
53-
WebButton1.Enabled := False;
54-
WebButton2.Enabled := False;
72+
btnWebSubscribe.Enabled := False;
73+
btnWebUnsubscribe.Enabled := False;
5574
Exit;
5675
end;
5776

58-
if TLocalStorage.GetValue('pushUserID') = WebEdit1.Text then
77+
if TLocalStorage.GetValue(STORAGE_KEY) = edtUserID.Text then
5978
begin
60-
WebButton1.Enabled := False;
61-
WebButton2.Enabled := True;
79+
btnWebSubscribe.Enabled := False;
80+
btnWebUnsubscribe.Enabled := True;
6281
end
63-
else if TLocalStorage.GetValue('pushUserID') <> '' then
82+
else if not Trim(TLocalStorage.GetValue(STORAGE_KEY)).IsEmpty then
6483
begin
65-
WebButton1.Enabled := False;
66-
WebButton2.Enabled := False;
84+
btnWebSubscribe.Enabled := False;
85+
btnWebUnsubscribe.Enabled := False;
6786
end
6887
else
6988
begin
70-
WebButton1.Enabled := True;
71-
WebButton2.Enabled := False;
89+
btnWebSubscribe.Enabled := True;
90+
btnWebUnsubscribe.Enabled := False;
7291
end;
7392
end;
7493

75-
function TMainView.ValidUserID: Boolean;
94+
procedure TMainView.ValidUserID;
7695
begin
77-
Result := True;
78-
if WebEdit1.Text = '' then
79-
begin
80-
Result := False;
81-
ShowMessage('UserID cannot be empty!');
82-
end;
96+
if Trim(edtUserID.Text).IsEmpty then
97+
raise Exception.Create('UserID cannot be empty!');
8398
end;
8499

85-
procedure TMainView.WebButton1Click(Sender: TObject);
100+
procedure TMainView.btnWebSubscribeClick(Sender: TObject);
86101
begin
87-
if ValidUserID then
88-
begin
89-
WebPushNotifications1.RegistrationUserID := WebEdit1.Text;
90-
WebPushNotifications1.RegisterServiceWorker;
102+
Self.ValidUserID;
91103

92-
DoAddToLocalStorage;
93-
end;
94-
end;
104+
WebPushNotifications1.RegistrationUserID := edtUserID.Text;
105+
WebPushNotifications1.RegisterServiceWorker;
95106

96-
procedure TMainView.WebButton2Click(Sender: TObject);
97-
begin
98-
if ValidUserID then
99-
begin
100-
WebPushNotifications1.RegistrationUserID := WebEdit1.Text;
101-
WebPushNotifications1.Unsubscribe;
102-
103-
DoRemoveFromLocalStorage;
104-
end;
105-
end;
106-
107-
procedure TMainView.WebEdit1Change(Sender: TObject);
108-
begin
109-
SetButtonState;
107+
TWebLocalStorage.SetValue(STORAGE_KEY, edtUserID.Text);
108+
Self.SetButtonState;
110109
end;
111110

112-
procedure TMainView.WebFormCreate(Sender: TObject);
111+
procedure TMainView.btnWebUnsubscribeClick(Sender: TObject);
113112
begin
114-
ls := TLocalStorage.Create(Self);
115-
if ls.Count > 0 then
116-
begin
117-
WebEdit1.Text := TLocalStorage.GetValue('pushUserID');
118-
end;
113+
Self.ValidUserID;
119114

120-
SetButtonState;
121-
end;
115+
WebPushNotifications1.RegistrationUserID := edtUserID.Text;
116+
WebPushNotifications1.Unsubscribe;
122117

123-
procedure TMainView.WebFormExit(Sender: TObject);
124-
begin
125-
ls.Free;
118+
TWebLocalStorage.RemoveKey(STORAGE_KEY);
119+
Self.SetButtonState;
126120
end;
127121

128122
end.

0 commit comments

Comments
 (0)