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