-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathDW.AppleIDButton.pas
182 lines (154 loc) · 5.66 KB
/
DW.AppleIDButton.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
unit DW.AppleIDButton;
{*******************************************************}
{ }
{ Kastri }
{ }
{ Delphi Worlds Cross-Platform Library }
{ }
{ Copyright 2020-2024 Dave Nottage under MIT license }
{ which is located in the root folder of this library }
{ }
{*******************************************************}
interface
uses
// RTL
System.Messaging, System.Classes, System.Types,
// FMX
FMX.StdCtrls, FMX.Controls.Presentation, FMX.Controls.Model,
// DW
DW.AuthenticationServices.Types;
const
MM_SET_CORNER_RADIUS = MM_USER + 1;
MM_SET_BUTTON_STYLE = MM_USER + 2;
MM_SET_BUTTON_TYPE = MM_USER + 3;
type
TAppleIDAuthorizationResponseEvent = procedure(Sender: TObject; const Response: TAppleIDAuthorizationResponse) of object;
TAppleIDButtonType = (SignIn, Continue, SignUp);
TAppleIDButtonStyle = (White, WhiteOutline, Black);
TAppleIDButtonModel = class(TDataModel)
private
FButtonStyle: TAppleIDButtonStyle;
FButtonType: TAppleIDButtonType;
FCornerRadius: Double;
procedure SetCornerRadius(const Value: Double);
procedure SetButtonStyle(const Value: TAppleIDButtonStyle);
procedure SetButtonType(const Value: TAppleIDButtonType);
public
property ButtonStyle: TAppleIDButtonStyle read FButtonStyle write SetButtonStyle;
property ButtonType: TAppleIDButtonType read FButtonType write SetButtonType;
property CornerRadius: Double read FCornerRadius write SetCornerRadius;
end;
TAppleIDButton = class(TButton)
private
FOnAuthorizationResponse: TAppleIDAuthorizationResponseEvent;
procedure AppleIDAuthorizationResponseMessageHandler(const Sender: TObject; const AMsg: TMessage);
function GetModel: TAppleIDButtonModel; overload;
procedure SetCornerRadius(const Value: Double);
procedure SetButtonStyle(const Value: TAppleIDButtonStyle);
procedure SetButtonType(const Value: TAppleIDButtonType);
function GetButtonStyle: TAppleIDButtonStyle;
function GetButtonType: TAppleIDButtonType;
function GetCornerRadius: Double;
protected
function DefineModelClass: TDataModelClass; override;
function RecommendSize(const AWishedSize: TSizeF): TSizeF; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property ButtonStyle: TAppleIDButtonStyle read GetButtonStyle write SetButtonStyle;
property ButtonType: TAppleIDButtonType read GetButtonType write SetButtonType;
property CornerRadius: Double read GetCornerRadius write SetCornerRadius;
property Model: TAppleIDButtonModel read GetModel;
property OnAuthorizationResponse: TAppleIDAuthorizationResponseEvent read FOnAuthorizationResponse write FOnAuthorizationResponse;
end;
implementation
{$IF Defined(IOS)}
uses
// DW
DW.OSLog,
DW.AppleIDButton.iOS;
{$ENDIF}
{ TAppleIDButtonModel }
procedure TAppleIDButtonModel.SetButtonStyle(const Value: TAppleIDButtonStyle);
begin
if Value <> FButtonStyle then
begin
FButtonStyle := Value;
SendMessage<TAppleIDButtonStyle>(MM_SET_BUTTON_STYLE, FButtonStyle);
end;
end;
procedure TAppleIDButtonModel.SetButtonType(const Value: TAppleIDButtonType);
begin
if Value <> FButtonType then
begin
FButtonType := Value;
SendMessage<TAppleIDButtonType>(MM_SET_BUTTON_STYLE, FButtonType);
end;
end;
procedure TAppleIDButtonModel.SetCornerRadius(const Value: Double);
begin
if Value <> FCornerRadius then
begin
FCornerRadius := Value;
SendMessage<Double>(MM_SET_CORNER_RADIUS, FCornerRadius);
end;
end;
{ TAppleIDButton }
constructor TAppleIDButton.Create(AOwner: TComponent);
begin
inherited;
ControlType := TControlType.Platform;
Width := 200;
Height := 50;
TMessageManager.DefaultManager.SubscribeToMessage(TAppleIDAuthorizationResponseMessage, AppleIDAuthorizationResponseMessageHandler);
end;
destructor TAppleIDButton.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TAppleIDAuthorizationResponseMessage, AppleIDAuthorizationResponseMessageHandler);
inherited;
end;
function TAppleIDButton.DefineModelClass: TDataModelClass;
begin
Result := TAppleIDButtonModel;
end;
function TAppleIDButton.GetButtonStyle: TAppleIDButtonStyle;
begin
Result := Model.ButtonStyle;
end;
function TAppleIDButton.GetButtonType: TAppleIDButtonType;
begin
Result := Model.ButtonType;
end;
function TAppleIDButton.GetCornerRadius: Double;
begin
Result := Model.CornerRadius;
end;
function TAppleIDButton.GetModel: TAppleIDButtonModel;
begin
Result := GetModel<TAppleIDButtonModel>;
end;
function TAppleIDButton.RecommendSize(const AWishedSize: TSizeF): TSizeF;
begin
Result := AWishedSize;
end;
procedure TAppleIDButton.SetButtonStyle(const Value: TAppleIDButtonStyle);
begin
Model.ButtonStyle := Value;
end;
procedure TAppleIDButton.SetButtonType(const Value: TAppleIDButtonType);
begin
Model.ButtonType := Value;
end;
procedure TAppleIDButton.SetCornerRadius(const Value: Double);
begin
Model.CornerRadius := Value;
end;
procedure TAppleIDButton.AppleIDAuthorizationResponseMessageHandler(const Sender: TObject; const AMsg: TMessage);
var
LResponse: TAppleIDAuthorizationResponse;
begin
LResponse := TAppleIDAuthorizationResponseMessage(AMsg).Value;
if Assigned(FOnAuthorizationResponse) then
FOnAuthorizationResponse(Self, LResponse);
end;
end.