1+ unit ClientMainView;
2+
3+ interface
4+
5+ 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;
9+
10+ type
11+ TClientMain = class (TWebForm)
12+ WebEdit1: TWebEdit;
13+ WebButton1: TWebButton;
14+ WebButton2: TWebButton;
15+ WebLabel1: TWebLabel;
16+ WebPushNotifications1: TWebPushNotifications;
17+ WebLabel3: TWebLabel;
18+ WebLabel4: TWebLabel;
19+ procedure WebButton1Click (Sender: TObject);
20+ procedure WebButton2Click (Sender: TObject);
21+ procedure WebEdit1Change (Sender: TObject);
22+ procedure WebFormCreate (Sender: TObject);
23+ procedure WebFormExit (Sender: TObject);
24+ private
25+ ls: TLocalStorage;
26+ procedure DoAddToLocalStorage ;
27+ procedure DoRemoveFromLocalStorage ;
28+ procedure SetButtonState ;
29+ function ValidUserID : Boolean;
30+ end ;
31+
32+ var
33+ ClientMain: TClientMain;
34+
35+ implementation
36+
37+ { $R *.dfm}
38+
39+ procedure TClientMain.DoAddToLocalStorage ;
40+ begin
41+ TWebLocalStorage.SetValue(' pushUserID' , WebEdit1.Text);
42+ SetButtonState;
43+ end ;
44+
45+ procedure TClientMain.DoRemoveFromLocalStorage ;
46+ begin
47+ TWebLocalStorage.RemoveKey(' pushUserID' );
48+ SetButtonState;
49+ end ;
50+
51+ procedure TClientMain.SetButtonState ;
52+ begin
53+ if WebEdit1.Text = ' ' then
54+ begin
55+ WebButton1.Enabled := False;
56+ WebButton2.Enabled := False;
57+ Exit;
58+ end ;
59+
60+ if TLocalStorage.GetValue(' pushUserID' ) = WebEdit1.Text then
61+ begin
62+ WebButton1.Enabled := False;
63+ WebButton2.Enabled := True;
64+ end
65+ else if TLocalStorage.GetValue(' pushUserID' ) <> ' ' then
66+ begin
67+ WebButton1.Enabled := False;
68+ WebButton2.Enabled := False;
69+ end
70+ else
71+ begin
72+ WebButton1.Enabled := True;
73+ WebButton2.Enabled := False;
74+ end ;
75+ end ;
76+
77+ function TClientMain.ValidUserID : Boolean;
78+ begin
79+ Result := True;
80+ if WebEdit1.Text = ' ' then
81+ begin
82+ Result := False;
83+ ShowMessage(' UserID cannot be empty!' );
84+ end ;
85+ end ;
86+
87+ procedure TClientMain.WebButton1Click (Sender: TObject);
88+ begin
89+ if ValidUserID then
90+ begin
91+ WebPushNotifications1.RegistrationUserID := WebEdit1.Text;
92+ WebPushNotifications1.RegisterServiceWorker;
93+
94+ DoAddToLocalStorage;
95+ end ;
96+ end ;
97+
98+ procedure TClientMain.WebButton2Click (Sender: TObject);
99+ begin
100+ if ValidUserID then
101+ begin
102+ WebPushNotifications1.RegistrationUserID := WebEdit1.Text;
103+ WebPushNotifications1.Unsubscribe;
104+
105+ DoRemoveFromLocalStorage;
106+ end ;
107+ end ;
108+
109+ procedure TClientMain.WebEdit1Change (Sender: TObject);
110+ begin
111+ SetButtonState;
112+ end ;
113+
114+ procedure TClientMain.WebFormCreate (Sender: TObject);
115+ begin
116+ ls := TLocalStorage.Create(Self);
117+ if ls.Count > 0 then
118+ begin
119+ WebEdit1.Text := TLocalStorage.GetValue(' pushUserID' );
120+ end ;
121+
122+ SetButtonState;
123+ end ;
124+
125+ procedure TClientMain.WebFormExit (Sender: TObject);
126+ begin
127+ ls.Free;
128+ end ;
129+
130+ end .
0 commit comments