-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathindex.ios.ts
More file actions
378 lines (323 loc) · 10.8 KB
/
index.ios.ts
File metadata and controls
378 lines (323 loc) · 10.8 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { Application, Utils, View } from '@nativescript/core';
import { colorSchemeProperty, ColorSchemeType, colorStyleProperty, ColorStyleType, Configuration, GoogleSignInButtonBase, IUser } from './common';
export class GoogleError extends Error {
private _native: NSError;
static fromNative(native: NSError, message?: string) {
const error = new GoogleError(message || native?.localizedDescription);
error._native = native;
return error;
}
get native() {
return this._native;
}
}
export class User implements IUser {
private _native: GIDGoogleUser;
private _grantedScopes: string[];
private _serverAuthCode: string;
static fromNative(user: GIDGoogleUser) {
if (user instanceof GIDGoogleUser) {
const usr = new User();
usr._native = user;
return usr;
}
return null;
}
get id() {
return this.native.userID;
}
get displayName() {
return this.native?.profile?.name;
}
get email() {
return this.native?.profile?.email;
}
get givenName() {
return this.native?.profile?.givenName;
}
get familyName() {
return this.native?.profile?.familyName;
}
get idToken() {
return this.native?.idToken.tokenString;
}
get accessToken() {
return this.native?.accessToken.tokenString;
}
get grantedScopes() {
if (!this._grantedScopes) {
const grantedScopes = [];
const count = this.native.grantedScopes.count;
for (let i = 0; i < count; i++) {
grantedScopes.push(this.native.grantedScopes.objectAtIndex(i));
}
this._grantedScopes = grantedScopes;
}
return this._grantedScopes;
}
get photoUrl() {
if (!this.native?.profile?.hasImage) {
return null;
}
return this.native?.profile?.imageURLWithDimension(120)?.absoluteString;
}
get serverAuthCode() {
return this._serverAuthCode;
}
requestScopes(scopes: string[]): Promise<User> {
return new Promise((resolve, reject) => {
GIDSignIn.sharedInstance.signInWithPresentingViewControllerHintAdditionalScopesCompletion(GoogleSignin.topViewController, 'Requesting additional scopes', scopes, (result, error) => {
if (error) {
reject(GoogleError.fromNative(error));
} else {
this._serverAuthCode = result.serverAuthCode;
resolve(User.fromNative(result.user));
}
});
});
}
get native() {
return this._native;
}
get ios() {
return this.native;
}
}
export class GoogleSignin {
static _nativeConfig: GIDConfiguration;
static _profileImageSize = 120;
static configure(configuration: Configuration = {}): Promise<void> {
return new Promise((resolve, reject) => {
const pathName = configuration['googleServicePlistPath'] ? configuration['googleServicePlistPath'] : 'GoogleService-Info';
const path = NSBundle.mainBundle.pathForResourceOfType(pathName, 'plist');
if (!configuration['clientId'] && !path) {
const message = 'GoogleSignin: failed to determine clientID - GoogleService-Info.plist was not found and iosClientId was not provided. To fix this error: if you have GoogleService-Info.plist file (usually downloaded from firebase) place it into the project as seen in the iOS guide. Otherwise pass clientId option to configure()';
reject(GoogleError.fromNative(null, message));
return;
}
let plist: NSDictionary<any, any>;
let clientId;
if (configuration['clientId']) {
clientId = configuration['clientId'];
} else {
plist = NSDictionary.alloc().initWithContentsOfFile(path);
clientId = plist.objectForKey('CLIENT_ID');
}
let serverClientId;
if (configuration['serverClientId']) {
serverClientId = configuration['serverClientId'];
} else {
if (!plist) {
plist = NSDictionary.alloc().initWithContentsOfFile(path);
}
serverClientId = plist.objectForKey('SERVER_CLIENT_ID');
}
this._profileImageSize = Number(configuration['profileImageSize']) ?? 120;
const config = GIDConfiguration.alloc().initWithClientIDServerClientIDHostedDomainOpenIDRealm(clientId, serverClientId, configuration.hostedDomain || null, configuration['openIDRealm'] || null);
this._nativeConfig = config;
GIDSignIn.sharedInstance.configuration = config;
resolve();
});
}
static getCurrentUser(): User | null {
return User.fromNative(GIDSignIn.sharedInstance?.currentUser);
}
static isSignedIn() {
return GIDSignIn.sharedInstance.hasPreviousSignIn();
}
static disconnect(): Promise<void> {
return new Promise((resolve, reject) => {
GIDSignIn.sharedInstance.disconnectWithCompletion((error) => {
if (error) {
reject(GoogleError.fromNative(error));
} else {
resolve();
}
});
});
}
static signIn() {
return new Promise((resolve, reject) => {
GIDSignIn.sharedInstance.signInWithPresentingViewControllerCompletion(this.topViewController, (result, error) => {
if (error) {
reject(GoogleError.fromNative(error));
} else {
resolve(User.fromNative(result?.user));
}
});
});
}
static signInSilently(): Promise<User> {
return new Promise((resolve, reject) => {
GIDSignIn.sharedInstance.restorePreviousSignInWithCompletion((user, error) => {
if (error) {
reject(GoogleError.fromNative(error));
} else {
resolve(User.fromNative(user));
}
});
});
}
static signOut(): Promise<void> {
return new Promise((resolve, reject) => {
GIDSignIn.sharedInstance.signOut();
resolve();
});
}
static getTokens(): Promise<{}> {
return new Promise((resolve, reject) => {
const user = GIDSignIn.sharedInstance?.currentUser;
if (!user) {
reject(new GoogleError('getTokens requires a user to be signed in'));
return;
}
user.refreshTokensIfNeededWithCompletion((auth, error) => {
if (error) {
reject(GoogleError.fromNative(error));
} else {
resolve({
idToken: auth?.idToken.tokenString,
accessToken: auth?.accessToken.tokenString,
});
}
});
});
}
static playServicesAvailable() {
return Promise.resolve(true);
}
static get topViewController(): UIViewController | undefined {
const root = this.rootViewController;
if (!root) {
return undefined;
}
return this.findTopViewController(root);
}
private static get rootViewController(): UIViewController | undefined {
const keyWindow = UIApplication.sharedApplication.keyWindow;
return keyWindow ? keyWindow.rootViewController : undefined;
}
private static findTopViewController(root: UIViewController): UIViewController | undefined {
const presented = root.presentedViewController;
if (presented != null) {
return this.findTopViewController(presented);
}
if (root instanceof UISplitViewController) {
const last = root.viewControllers.lastObject;
if (last == null) {
return root;
}
return this.findTopViewController(last);
} else if (root instanceof UINavigationController) {
const top = root.topViewController;
if (top == null) {
return root;
}
return this.findTopViewController(top);
} else if (root instanceof UITabBarController) {
const selected = root.selectedViewController;
if (selected == null) {
return root;
}
return this.findTopViewController(selected);
} else {
return root;
}
}
}
export class GoogleSignInButton extends GoogleSignInButtonBase {
private _tapHandler: NSObject;
createNativeView() {
return GIDSignInButton.new();
}
public initNativeView(): void {
super.initNativeView();
this._tapHandler = TapHandlerImpl.initWithOwner(new WeakRef(this));
this.nativeViewProtected.addTargetActionForControlEvents(this._tapHandler, 'tap', UIControlEvents.TouchUpInside);
}
public disposeNativeView(): void {
this._tapHandler = null;
super.disposeNativeView();
}
[colorSchemeProperty.setNative](value: ColorSchemeType) {
const nativeView: GIDSignInButton = this.nativeView;
switch (value) {
case 'dark':
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeDark;
break;
case 'light':
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeLight;
break;
default:
const mode = Application.systemAppearance();
switch (mode) {
case 'dark':
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeDark;
break;
default:
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeLight;
break;
}
break;
}
}
[colorStyleProperty.setNative](value: ColorStyleType) {
const nativeView: GIDSignInButton = this.nativeView;
switch (value) {
case 'wide':
nativeView.style = GIDSignInButtonStyle.kGIDSignInButtonStyleWide;
break;
case 'icon':
nativeView.style = GIDSignInButtonStyle.kGIDSignInButtonStyleIconOnly;
break;
default:
nativeView.style = GIDSignInButtonStyle.kGIDSignInButtonStyleStandard;
break;
}
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
const layout = Utils.layout;
const nativeView = this.nativeViewProtected;
if (nativeView) {
const width = layout.getMeasureSpecSize(widthMeasureSpec);
const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
const height = layout.getMeasureSpecSize(heightMeasureSpec);
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
const horizontalPadding = this.effectivePaddingLeft + this.effectiveBorderLeftWidth + this.effectivePaddingRight + this.effectiveBorderRightWidth;
let verticalPadding = this.effectivePaddingTop + this.effectiveBorderTopWidth + this.effectivePaddingBottom + this.effectiveBorderBottomWidth;
// The default button padding for UIButton - 6dip top and bottom.
if (verticalPadding === 0) {
verticalPadding = layout.toDevicePixels(12);
}
const desiredSize = layout.measureNativeView(nativeView, width - horizontalPadding, widthMode, height - verticalPadding, heightMode);
desiredSize.width = desiredSize.width + horizontalPadding;
desiredSize.height = desiredSize.height + verticalPadding;
const measureWidth = Math.max(desiredSize.width, this.effectiveMinWidth);
const measureHeight = Math.max(desiredSize.height, this.effectiveMinHeight);
const widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
const heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
}
}
@NativeClass
class TapHandlerImpl extends NSObject {
private _owner: WeakRef<GoogleSignInButton>;
public static initWithOwner(owner: WeakRef<GoogleSignInButton>): TapHandlerImpl {
const handler = <TapHandlerImpl>TapHandlerImpl.new();
handler._owner = owner;
return handler;
}
public tap(args) {
// _owner is a {N} view which could get destroyed when a tap initiates (protect!)
if (this._owner) {
const owner = this._owner?.deref();
if (owner) {
owner._emit(GoogleSignInButton.tapEvent);
}
}
}
public static ObjCExposedMethods = {
tap: { returns: interop.types.void, params: [interop.types.id] },
};
}