Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit ce0109a

Browse files
author
Brendan Ingham
committed
Adds Button for UI and Corrects All Typings.
1 parent 855c75b commit ce0109a

File tree

11 files changed

+56
-33
lines changed

11 files changed

+56
-33
lines changed

demo/app/main-page.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<TabViewItem.view>
1919
<ScrollView>
2020
<GridLayout columns="*, *"
21-
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
21+
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
2222
horizontalAlignment="stretch"
2323
class="tab-content">
2424
<Button row="0" colSpan="2" text="init firebase - do this first" tap="{{ doWebInit }}" class="button button-positive"/>
@@ -72,6 +72,10 @@
7272

7373
<Label row="19" colSpan="2" text="{{ storageFeedback }}" class="message" textWrap="true"/>
7474

75+
<Label row="20" colSpan="2" text="Callable Functions" class="subtitle" />
76+
77+
<Button row="21" col="0" text="callable 1" tap="{{ doCallableFunction }}" class="button button-functions" />
78+
7579
</GridLayout>
7680
</ScrollView>
7781
</TabViewItem.view>

demo/app/main-view-model.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MessagingViewModel } from './messaging-view-model';
88

99
const firebaseWebApi = require("nativescript-plugin-firebase/app");
1010

11+
1112
declare const Crashlytics: any;
1213

1314
export class HelloWorldModel extends Observable {
@@ -195,6 +196,25 @@ export class HelloWorldModel extends Observable {
195196
}
196197
}
197198

199+
public doCallableFunction(): void {
200+
const fn = firebaseWebApi.functions().httpsCallable('helloWorldJson');
201+
202+
fn( 'Hello' ).then((DataCue) => {
203+
alert({
204+
title: "Callable Function Result",
205+
message: JSON.stringify(DataCue),
206+
okButtonText: "Nice!"
207+
});
208+
})
209+
.catch((e) => {
210+
alert({
211+
title: "An Error Occurred",
212+
message: e.message,
213+
okButtonText: "OK, thanks"
214+
});
215+
});
216+
}
217+
198218
public doWebAddValueEventListenerForCompanies(): void {
199219
const path = "/companies";
200220
const onValueEvent = result => {

src/app/functions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as firebase from "../../firebase";
22

33
export namespace functions {
44
// tslint:disable-next-line:class-name
5-
export class functions {
5+
export class Functions {
66
httpsCallable<I, O>( functionName: string ) {
77
return firebase.functions.httpsCallable<I, O>(functionName);
88
}

src/app/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { auth as firebaseAuthModule } from "./auth";
88
import { database as firebaseDatabaseModule } from "./database";
99
import { firestore as firebaseFirestoreModule } from "./firestore";
1010
import { storage as firebaseStorageModule } from "./storage";
11+
import { functions as firebaseFunctionsModule } from './functions';
1112

1213
export function initializeApp(options?: firebase.InitOptions, name? /* ignored */: string): Promise<any> {
1314
return firebase.init(options);
@@ -46,6 +47,17 @@ export function firestore(app?: any): firebaseFirestoreModule.Firestore {
4647
return firestoreCache;
4748
}
4849

50+
let functionsCache;
51+
export function functions(app?: any): firebaseFunctionsModule.Functions {
52+
if (app) {
53+
console.log("The 'app' param is ignored at the moment.");
54+
}
55+
if ( !functionsCache ) {
56+
functionsCache = new firebaseFunctionsModule.Functions();
57+
}
58+
return functionsCache;
59+
}
60+
4961
let storageCache;
5062
export function storage(app?: any): firebaseStorageModule.Storage {
5163
if (app) {

src/firebase.android.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isDocumentReference
77
} from "./firebase-common";
88
import * as firebaseMessaging from "./messaging/messaging";
9+
import * as firebaseFunctions from "./functions/functions";
910
import * as appModule from "tns-core-modules/application";
1011
import { AndroidActivityResultEventData } from "tns-core-modules/application";
1112
import { ad as AndroidUtils, layout } from "tns-core-modules/utils/utils";
@@ -448,6 +449,8 @@ firebase.subscribeToTopic = firebaseMessaging.subscribeToTopic;
448449
firebase.unsubscribeFromTopic = firebaseMessaging.unsubscribeFromTopic;
449450
firebase.areNotificationsEnabled = firebaseMessaging.areNotificationsEnabled;
450451

452+
firebase.functions = firebaseFunctions;
453+
451454
firebase.addOnDynamicLinkReceivedCallback = callback => {
452455
return new Promise((resolve, reject) => {
453456
try {

src/firebase.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,8 @@ export namespace firestore {
847847
}
848848

849849
export namespace functions {
850-
declare type HttpsCallable<I, O> = ( callableData: I ) => Promise<O>;
851-
declare function httpsCallable<I, O>( callableFunctionName: string ): HttpsCallable<I, O>;
850+
export type HttpsCallable<I, O> = ( callableData: I ) => Promise<O>;
851+
export function httpsCallable<I, O>( callableFunctionName: string ): HttpsCallable<I, O>;
852852
}
853853
// Auth
854854
export function login(options: LoginOptions): Promise<User>;

src/firebase.ios.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as application from "tns-core-modules/application/application";
1010
import { ios as iOSUtils } from "tns-core-modules/utils/utils";
1111
import { device } from "tns-core-modules/platform/platform";
1212
import { DeviceType } from "tns-core-modules/ui/enums/enums";
13+
import * as firebaseFunctions from './functions/functions';
1314
import { firestore, User } from "./firebase";
1415
import { firebaseUtils } from "./utils";
1516

@@ -42,6 +43,8 @@ firebase.subscribeToTopic = firebaseMessaging.subscribeToTopic;
4243
firebase.unsubscribeFromTopic = firebaseMessaging.unsubscribeFromTopic;
4344
firebase.areNotificationsEnabled = firebaseMessaging.areNotificationsEnabled;
4445

46+
firebase.functions = firebaseFunctions;
47+
4548
firebase.addAppDelegateMethods = appDelegate => {
4649
// we need the launchOptions for this one so it's a bit hard to use the UIApplicationDidFinishLaunchingNotification pattern we're using for other things
4750
appDelegate.prototype.applicationDidFinishLaunchingWithOptions = (application, launchOptions) => {

src/functions/functions.android.ts

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {HttpsCallable} from '.';
1+
import {HttpsCallable} from './functions';
2+
import { firebase } from '../firebase-common';
23

34
export function httpsCallable<I = {}, O = {}>(functionName: string): HttpsCallable<I, O> {
45
const instance = com.google.firebase.functions.FirebaseFunctions.getInstance();
56

67
return (data: I) => new Promise<O>((resolve, reject) => {
78

8-
let actData = convertToNative(data);
9+
let actData = firebase.toValue(data);
910

1011
return instance.getHttpsCallable(functionName)
1112
.call(actData)
@@ -18,6 +19,7 @@ export function httpsCallable<I = {}, O = {}>(functionName: string): HttpsCallab
1819

1920
resolve( resultData );
2021
} catch (e) {
22+
console.log('Error Caught:', e);
2123
reject( e.message );
2224
}
2325

@@ -27,27 +29,4 @@ export function httpsCallable<I = {}, O = {}>(functionName: string): HttpsCallab
2729
});
2830

2931

30-
}
31-
32-
function convertToNative(input: any) {
33-
34-
if ( typeof input === "string" ) {
35-
return new java.lang.String(input);
36-
} else if (typeof input === "number") {
37-
if ( input % 1 === 0) {
38-
return new java.lang.Integer(input);
39-
} else {
40-
return new java.lang.Float(input);
41-
}
42-
} else if ( typeof input === "boolean" ) {
43-
return new java.lang.Boolean(input);
44-
} else if ( Array.isArray(input) ) {
45-
const hashset = new java.util.ArrayList();
46-
input.forEach( (currentItem) => hashset.add(convertToNative(currentItem)));
47-
return hashset;
48-
} else {
49-
const hashmap = new java.util.HashMap<string, any>();
50-
Object.keys(input).forEach((currentItem ) => hashmap.put( currentItem, convertToNative(input[currentItem])));
51-
return hashmap;
52-
}
5332
}

src/functions/functions.ios.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {HttpsCallable} from '.';
1+
import {HttpsCallable} from './functions';
22

33

44
export function httpsCallable< I = {}, O = {} >( functionName: string ): HttpsCallable<I, O> {
55

66
const functions = FIRFunctions.functions();
77

88
return (data: I ) => new Promise((resolve, reject) => {
9-
functions.HTTPSCallableWithName(functionName).callWithObjectCompletion(data, (result, err: NSError) => {
9+
functions.HTTPSCallableWithName(functionName).callWithObjectCompletion(data, (result: FIRHTTPSCallableResult, err: NSError) => {
1010
if ( err ) {
1111
if ( err.domain === FIRFunctionsErrorDomain ) {
1212
const message = err.localizedDescription;

src/functions/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './functions';
1+
import * as callableFns from "./functions";
2+
export declare const functions: typeof callableFns;

0 commit comments

Comments
 (0)