Skip to content

Commit 841a394

Browse files
triniwizNathanWalker
authored andcommitted
fix(google-signin): handle button tap
1 parent 492122e commit 841a394

6 files changed

Lines changed: 9325 additions & 12 deletions

File tree

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { Observable, EventData, Page } from '@nativescript/core';
22
import { DemoSharedGoogleSignin } from '@demo/shared';
3-
import { } from '@nativescript/google-signin';
3+
import {} from '@nativescript/google-signin';
44

55
export function navigatingTo(args: EventData) {
66
const page = <Page>args.object;
77
page.bindingContext = new DemoModel();
88
}
99

1010
export class DemoModel extends DemoSharedGoogleSignin {
11-
11+
signInTapped(args) {
12+
console.log('signInTapped');
13+
this.testIt();
14+
}
1215
}

apps/demo/src/plugin-demos/google-signin.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
2+
xmlns:ui="@nativescript/google-signin"
3+
navigatingTo="navigatingTo" class="page">
24
<Page.actionBar>
35
<ActionBar title="google-signin" icon="" class="action-bar">
46
</ActionBar>
@@ -7,7 +9,7 @@
79
<ScrollView class="h-full">
810
<StackLayout>
911
<Button text="Test google-signin" tap="{{ testIt }}" class="btn btn-primary"/>
10-
12+
<ui:GoogleSignInButton tap="{{ signInTapped }}"/>
1113
</StackLayout>
1214
</ScrollView>
1315
</StackLayout>

packages/google-signin/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export const colorStyleProperty = new Property<GoogleSignInButtonBase, ColorStyl
4848

4949
@CSSType('GoogleSignInButton')
5050
export class GoogleSignInButtonBase extends View {
51+
public static tapEvent = 'tap';
52+
5153
colorScheme: ColorSchemeType;
5254
colorStyle: ColorStyleType;
5355
}

packages/google-signin/index.android.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-nocheck
21
import { AndroidApplication, Application, AndroidActivityResultEventData, Utils } from '@nativescript/core';
32
import { colorSchemeProperty, ColorSchemeType, colorStyleProperty, ColorStyleType, Configuration, GoogleSignInButtonBase, IUser } from './common';
43
import lazy from '@nativescript/core/utils/lazy';
@@ -251,11 +250,43 @@ export class GoogleSignin {
251250
}
252251
}
253252

253+
@NativeClass
254+
@Interfaces([android.view.View.OnClickListener])
255+
class ClickListenerImpl extends java.lang.Object implements android.view.View.OnClickListener {
256+
constructor(public owner: GoogleSignInButton) {
257+
super();
258+
259+
return global.__native(this);
260+
}
261+
262+
public onClick(v: android.view.View): void {
263+
const owner = this.owner;
264+
if (owner) {
265+
owner._emit(GoogleSignInButton.tapEvent);
266+
}
267+
}
268+
}
269+
254270
export class GoogleSignInButton extends GoogleSignInButtonBase {
255271
createNativeView() {
256272
return new com.google.android.gms.common.SignInButton(this._context);
257273
}
258274

275+
initNativeView(): void {
276+
super.initNativeView();
277+
const nativeView = this.nativeViewProtected as com.google.android.gms.common.SignInButton;
278+
const clickListener = new ClickListenerImpl(this);
279+
nativeView.setOnClickListener(clickListener);
280+
(<any>nativeView).clickListener = clickListener;
281+
}
282+
283+
public disposeNativeView() {
284+
if ((<any>this.nativeViewProtected)?.clickListener) {
285+
(<any>this.nativeViewProtected).clickListener.owner = null;
286+
}
287+
super.disposeNativeView();
288+
}
289+
259290
[colorSchemeProperty.setNative](value: ColorSchemeType) {
260291
const nativeView: com.google.android.gms.common.SignInButton = this.nativeView;
261292
switch (value) {

packages/google-signin/index.ios.ts

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Utils } from '@nativescript/core';
1+
import { Application, Utils, View } from '@nativescript/core';
22
import { colorSchemeProperty, ColorSchemeType, colorStyleProperty, ColorStyleType, Configuration, GoogleSignInButtonBase, IUser } from './common';
33

44
export class GoogleError extends Error {
@@ -261,12 +261,21 @@ export class GoogleSignin {
261261
}
262262

263263
export class GoogleSignInButton extends GoogleSignInButtonBase {
264+
private _tapHandler: NSObject;
265+
264266
createNativeView() {
265267
return GIDSignInButton.new();
266268
}
267269

268-
initNativeView() {
270+
public initNativeView(): void {
269271
super.initNativeView();
272+
this._tapHandler = TapHandlerImpl.initWithOwner(new WeakRef(this));
273+
this.nativeViewProtected.addTargetActionForControlEvents(this._tapHandler, 'tap', UIControlEvents.TouchUpInside);
274+
}
275+
276+
public disposeNativeView(): void {
277+
this._tapHandler = null;
278+
super.disposeNativeView();
270279
}
271280

272281
[colorSchemeProperty.setNative](value: ColorSchemeType) {
@@ -307,12 +316,62 @@ export class GoogleSignInButton extends GoogleSignInButtonBase {
307316
}
308317
}
309318

310-
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
311-
const nativeView = this.nativeView;
319+
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
320+
const layout = Utils.layout;
321+
322+
const nativeView = this.nativeViewProtected;
323+
312324
if (nativeView) {
313-
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
314-
const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
315-
this.setMeasuredDimension(width, height);
325+
const width = layout.getMeasureSpecSize(widthMeasureSpec);
326+
const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
327+
const height = layout.getMeasureSpecSize(heightMeasureSpec);
328+
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
329+
330+
const horizontalPadding = this.effectivePaddingLeft + this.effectiveBorderLeftWidth + this.effectivePaddingRight + this.effectiveBorderRightWidth;
331+
let verticalPadding = this.effectivePaddingTop + this.effectiveBorderTopWidth + this.effectivePaddingBottom + this.effectiveBorderBottomWidth;
332+
333+
// The default button padding for UIButton - 6dip top and bottom.
334+
if (verticalPadding === 0) {
335+
verticalPadding = layout.toDevicePixels(12);
336+
}
337+
338+
const desiredSize = layout.measureNativeView(nativeView, width - horizontalPadding, widthMode, height - verticalPadding, heightMode);
339+
340+
desiredSize.width = desiredSize.width + horizontalPadding;
341+
desiredSize.height = desiredSize.height + verticalPadding;
342+
343+
const measureWidth = Math.max(desiredSize.width, this.effectiveMinWidth);
344+
const measureHeight = Math.max(desiredSize.height, this.effectiveMinHeight);
345+
346+
const widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
347+
const heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
348+
349+
this.setMeasuredDimension(widthAndState, heightAndState);
316350
}
317351
}
318352
}
353+
354+
@NativeClass
355+
class TapHandlerImpl extends NSObject {
356+
private _owner: WeakRef<GoogleSignInButton>;
357+
358+
public static initWithOwner(owner: WeakRef<GoogleSignInButton>): TapHandlerImpl {
359+
const handler = <TapHandlerImpl>TapHandlerImpl.new();
360+
handler._owner = owner;
361+
return handler;
362+
}
363+
364+
public tap(args) {
365+
// _owner is a {N} view which could get destroyed when a tap initiates (protect!)
366+
if (this._owner) {
367+
const owner = this._owner?.deref();
368+
if (owner) {
369+
owner._emit(GoogleSignInButton.tapEvent);
370+
}
371+
}
372+
}
373+
374+
public static ObjCExposedMethods = {
375+
tap: { returns: interop.types.void, params: [interop.types.id] },
376+
};
377+
}

0 commit comments

Comments
 (0)