33interface
44
55uses
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
1020type
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
3039var
@@ -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);
3859begin
39- TWebLocalStorage.SetValue(' pushUserID' , WebEdit1.Text);
40- SetButtonState;
60+ LLocalStorage.Free;
4161end ;
4262
43- procedure TMainView.DoRemoveFromLocalStorage ;
63+ procedure TMainView.edtUserIDChange (Sender: TObject) ;
4464begin
45- TWebLocalStorage.RemoveKey(' pushUserID' );
46- SetButtonState;
65+ Self.SetButtonState;
4766end ;
4867
4968procedure TMainView.SetButtonState ;
5069begin
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 ;
7392end ;
7493
75- function TMainView.ValidUserID : Boolean ;
94+ procedure TMainView.ValidUserID ;
7695begin
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!' );
8398end ;
8499
85- procedure TMainView.WebButton1Click (Sender: TObject);
100+ procedure TMainView.btnWebSubscribeClick (Sender: TObject);
86101begin
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;
110109end ;
111110
112- procedure TMainView.WebFormCreate (Sender: TObject);
111+ procedure TMainView.btnWebUnsubscribeClick (Sender: TObject);
113112begin
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;
126120end ;
127121
128122end .
0 commit comments