Skip to content

Commit 137bc93

Browse files
committed
feat(google-signin): button view
1 parent 6c09566 commit 137bc93

File tree

6 files changed

+157
-4
lines changed

6 files changed

+157
-4
lines changed

packages/google-signin/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ try {
6161
} catch (e) {}
6262
```
6363

64+
### Button
65+
66+
Ensure you've included xmlns:ui="@nativescript/google-signin" on the Page element
67+
68+
```xml
69+
<ui:GoogleSigninButton tap="handleSignIn" />
70+
```
71+
72+
```ts
73+
import { GoogleSignin } from '@nativescript/google-signin';
74+
export function handleSignIn(args){
75+
try {
76+
await GoogleSignin.configure();
77+
const user = await GoogleSignin.signIn();
78+
} catch (e) {}
79+
}
80+
```
81+
6482
## configure(options)
6583

6684
It is mandatory to call this method before attempting to call signIn() and signInSilently(). In typical scenarios, configure needs to be called only once, after your app starts. All parameters are optional.

packages/google-signin/common.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { CSSType, Property, View } from '@nativescript/core';
2+
13
export interface Configuration {
24
scopes?: string[];
35
signInOptions?: 'default' | 'games';
@@ -29,3 +31,25 @@ export interface IUser {
2931

3032
requestScopes(scopes: string[]): Promise<IUser>;
3133
}
34+
35+
export type ColorSchemeType = 'dark' | 'light' | 'auto';
36+
37+
export type ColorStyleType = 'standard' | 'wide' | 'icon';
38+
39+
export const colorSchemeProperty = new Property<GoogleSignInButtonBase, ColorSchemeType>({
40+
name: 'colorScheme',
41+
defaultValue: 'auto',
42+
});
43+
export const colorStyleProperty = new Property<GoogleSignInButtonBase, ColorStyleType>({
44+
name: 'colorStyle',
45+
defaultValue: 'standard',
46+
});
47+
48+
@CSSType('GoogleSignInButton')
49+
export class GoogleSignInButtonBase extends View {
50+
colorScheme: ColorSchemeType;
51+
colorStyle: ColorStyleType;
52+
}
53+
54+
colorSchemeProperty.register(GoogleSignInButtonBase);
55+
colorStyleProperty.register(GoogleSignInButtonBase);

packages/google-signin/index.android.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { AndroidApplication, Application, AndroidActivityResultEventData, Utils } from '@nativescript/core';
2-
import { Configuration, IUser } from './common';
2+
import { colorSchemeProperty, ColorSchemeType, colorStyleProperty, ColorStyleType, Configuration, GoogleSignInButtonBase, IUser } from './common';
33
import { GoogleError } from './index.ios';
4+
import lazy from '@nativescript/core/utils/lazy';
45

56
const REQUEST_CODE_SIGNIN = 610210;
67
const REQUEST_CODE_REQUEST_SCOPE = 610211;
8+
9+
const STYLE_STANDARD = lazy(() => com.google.android.gms.common.SignInButton.SIZE_STANDARD);
10+
const STYLE_ICON = lazy(() => com.google.android.gms.common.SignInButton.SIZE_ICON_ONLY);
11+
const STYLE_WIDE = lazy(() => com.google.android.gms.common.SignInButton.SIZE_WIDE);
12+
13+
const COLOR_DARK = lazy(() => com.google.android.gms.common.SignInButton.COLOR_DARK);
14+
const COLOR_LIGHT = lazy(() => com.google.android.gms.common.SignInButton.COLOR_LIGHT);
15+
const COLOR_AUTO = lazy(() => com.google.android.gms.common.SignInButton.COLOR_AUTO);
16+
717
export class User implements IUser {
818
#native: com.google.android.gms.auth.api.signin.GoogleSignInAccount;
919
#grantedScopes: string[];
@@ -225,3 +235,39 @@ export class GoogleSignin {
225235
return org.nativescript.plugins.googlesignin.GoogleSignIn.playServicesAvailable(false, Application.android.foregroundActivity || Application.android.startActivity);
226236
}
227237
}
238+
239+
export class GoogleSignInButton extends GoogleSignInButtonBase {
240+
createNativeView() {
241+
return new com.google.android.gms.common.SignInButton(this._context);
242+
}
243+
244+
[colorSchemeProperty.setNative](value: ColorSchemeType) {
245+
const nativeView: com.google.android.gms.common.SignInButton = this.nativeView;
246+
switch (value) {
247+
case 'dark':
248+
nativeView.setColorScheme(COLOR_DARK());
249+
break;
250+
case 'light':
251+
nativeView.setColorScheme(COLOR_LIGHT());
252+
break;
253+
default:
254+
nativeView.setColorScheme(COLOR_AUTO());
255+
break;
256+
}
257+
}
258+
259+
[colorStyleProperty.setNative](value: ColorStyleType) {
260+
const nativeView: com.google.android.gms.common.SignInButton = this.nativeView;
261+
switch (value) {
262+
case 'wide':
263+
nativeView.setSize(STYLE_WIDE());
264+
break;
265+
case 'icon':
266+
nativeView.setSize(STYLE_ICON());
267+
break;
268+
default:
269+
nativeView.setSize(STYLE_STANDARD());
270+
break;
271+
}
272+
}
273+
}

packages/google-signin/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { AndroidApplication, Application, AndroidActivityResultEventData, Utils } from '@nativescript/core';
2-
import { Configuration, IUser } from './common';
2+
import { Configuration, IUser, GoogleSignInButtonBase, ColorStyleType, ColorSchemeType } from './common';
3+
4+
export { colorSchemeProperty, colorStyleProperty } from './common';
35
import { GoogleError } from './index.ios';
46

57
declare class User implements IUser {
@@ -51,3 +53,8 @@ declare class GoogleSignin {
5153

5254
static playServicesAvailable(): Promise<boolean>;
5355
}
56+
57+
declare class GoogleSignInButton extends GoogleSignInButtonBase {
58+
colorScheme: ColorSchemeType;
59+
colorStyle: ColorStyleType;
60+
}

packages/google-signin/index.ios.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Configuration, IUser } from './common';
1+
import { Application, Utils } from '@nativescript/core';
2+
import { colorSchemeProperty, ColorSchemeType, colorStyleProperty, ColorStyleType, Configuration, GoogleSignInButtonBase, IUser } from './common';
23

34
export class GoogleError extends Error {
45
#native: NSError;
@@ -256,3 +257,60 @@ export class GoogleSignin {
256257
}
257258
}
258259
}
260+
261+
export class GoogleSignInButton extends GoogleSignInButtonBase {
262+
createNativeView() {
263+
return GIDSignInButton.new();
264+
}
265+
266+
initNativeView() {
267+
super.initNativeView();
268+
}
269+
270+
[colorSchemeProperty.setNative](value: ColorSchemeType) {
271+
const nativeView: GIDSignInButton = this.nativeView;
272+
switch (value) {
273+
case 'dark':
274+
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeDark;
275+
break;
276+
case 'light':
277+
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeLight;
278+
break;
279+
default:
280+
const mode = Application.systemAppearance();
281+
switch (mode) {
282+
case 'dark':
283+
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeDark;
284+
break;
285+
default:
286+
nativeView.colorScheme = GIDSignInButtonColorScheme.kGIDSignInButtonColorSchemeLight;
287+
break;
288+
}
289+
break;
290+
}
291+
}
292+
293+
[colorStyleProperty.setNative](value: ColorStyleType) {
294+
const nativeView: GIDSignInButton = this.nativeView;
295+
switch (value) {
296+
case 'wide':
297+
nativeView.style = GIDSignInButtonStyle.kGIDSignInButtonStyleWide;
298+
break;
299+
case 'icon':
300+
nativeView.style = GIDSignInButtonStyle.kGIDSignInButtonStyleIconOnly;
301+
break;
302+
default:
303+
nativeView.style = GIDSignInButtonStyle.kGIDSignInButtonStyleStandard;
304+
break;
305+
}
306+
}
307+
308+
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
309+
const nativeView = this.nativeView;
310+
if (nativeView) {
311+
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
312+
const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
313+
this.setMeasuredDimension(width, height);
314+
}
315+
}
316+
}

packages/google-signin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nativescript/google-signin",
3-
"version": "1.0.0-alpha.0",
3+
"version": "1.0.0-alpha.1",
44
"description": "Google Sign-in for your NativeScript applications",
55
"main": "index",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)