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

Commit 14bec6e

Browse files
Fix on fix for issue #947 #952 (null check)
1 parent 4c9f810 commit 14bec6e

File tree

1 file changed

+116
-108
lines changed

1 file changed

+116
-108
lines changed

src/firebase.ios.ts

Lines changed: 116 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {
2-
firebase,
32
DocumentSnapshot as DocumentSnapshotBase,
3+
FieldValue,
4+
firebase,
45
GeoPoint,
5-
isDocumentReference, FieldValue
6+
isDocumentReference
67
} from "./firebase-common";
78
import * as firebaseMessaging from "./messaging/messaging";
89
import * as application from "tns-core-modules/application/application";
@@ -112,17 +113,17 @@ firebase.addAppDelegateMethods = appDelegate => {
112113
let result = false;
113114
if (typeof (FBSDKApplicationDelegate) !== "undefined") {
114115
result = FBSDKApplicationDelegate.sharedInstance().applicationOpenURLSourceApplicationAnnotation(
115-
application,
116-
url,
117-
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
118-
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
116+
application,
117+
url,
118+
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
119+
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
119120
}
120121

121122
if (typeof (GIDSignIn) !== "undefined") {
122123
result = result || GIDSignIn.sharedInstance().handleURLSourceApplicationAnnotation(
123-
url,
124-
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
125-
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
124+
url,
125+
options.valueForKey(UIApplicationOpenURLOptionsSourceApplicationKey),
126+
options.valueForKey(UIApplicationOpenURLOptionsAnnotationKey));
126127
}
127128

128129
if (typeof (FIRDynamicLink) !== "undefined") {
@@ -475,7 +476,7 @@ firebase.getRemoteConfig = arg => {
475476

476477
const onCompletion = (remoteConfigFetchStatus, error) => {
477478
if (remoteConfigFetchStatus === FIRRemoteConfigFetchStatus.Success ||
478-
remoteConfigFetchStatus === FIRRemoteConfigFetchStatus.Throttled) {
479+
remoteConfigFetchStatus === FIRRemoteConfigFetchStatus.Throttled) {
479480

480481
const activated = firebaseRemoteConfig.activateFetched();
481482

@@ -742,22 +743,22 @@ firebase.login = arg => {
742743
firActionCodeSettings.handleCodeInApp = true;
743744
firActionCodeSettings.setIOSBundleID(arg.emailLinkOptions.iOS ? arg.emailLinkOptions.iOS.bundleId : NSBundle.mainBundle.bundleIdentifier);
744745
firActionCodeSettings.setAndroidPackageNameInstallIfNotAvailableMinimumVersion(
745-
arg.emailLinkOptions.android ? arg.emailLinkOptions.android.packageName : NSBundle.mainBundle.bundleIdentifier,
746-
arg.emailLinkOptions.android ? arg.emailLinkOptions.android.installApp || false : false,
747-
arg.emailLinkOptions.android ? arg.emailLinkOptions.android.minimumVersion || "1" : "1");
746+
arg.emailLinkOptions.android ? arg.emailLinkOptions.android.packageName : NSBundle.mainBundle.bundleIdentifier,
747+
arg.emailLinkOptions.android ? arg.emailLinkOptions.android.installApp || false : false,
748+
arg.emailLinkOptions.android ? arg.emailLinkOptions.android.minimumVersion || "1" : "1");
748749
fAuth.sendSignInLinkToEmailActionCodeSettingsCompletion(
749-
arg.emailLinkOptions.email,
750-
firActionCodeSettings,
751-
(error: NSError) => {
752-
if (error) {
753-
reject(error.localizedDescription);
754-
return;
750+
arg.emailLinkOptions.email,
751+
firActionCodeSettings,
752+
(error: NSError) => {
753+
if (error) {
754+
reject(error.localizedDescription);
755+
return;
756+
}
757+
// The link was successfully sent.
758+
// Save the email locally so you don't need to ask the user for it again if they open the link on the same device.
759+
firebase.rememberEmailForEmailLinkLogin(arg.emailLinkOptions.email);
760+
resolve();
755761
}
756-
// The link was successfully sent.
757-
// Save the email locally so you don't need to ask the user for it again if they open the link on the same device.
758-
firebase.rememberEmailForEmailLinkLogin(arg.emailLinkOptions.email);
759-
resolve();
760-
}
761762
);
762763

763764
} else if (arg.type === firebase.LoginType.PHONE) {
@@ -801,14 +802,14 @@ firebase.login = arg => {
801802
fAuth.signInAndRetrieveDataWithCustomTokenCompletion(arg.customOptions.token, onCompletionWithAuthResult);
802803
} else if (arg.customOptions.tokenProviderFn) {
803804
arg.customOptions.tokenProviderFn()
804-
.then(
805-
token => {
806-
fAuth.signInAndRetrieveDataWithCustomTokenCompletion(token, onCompletionWithAuthResult);
807-
},
808-
error => {
809-
reject(error);
810-
}
811-
);
805+
.then(
806+
token => {
807+
fAuth.signInAndRetrieveDataWithCustomTokenCompletion(token, onCompletionWithAuthResult);
808+
},
809+
error => {
810+
reject(error);
811+
}
812+
);
812813
}
813814

814815
} else if (arg.type === firebase.LoginType.FACEBOOK) {
@@ -856,9 +857,9 @@ firebase.login = arg => {
856857
}
857858

858859
fbSDKLoginManager.logInWithReadPermissionsFromViewControllerHandler(
859-
scope,
860-
null, // the viewcontroller param can be null since by default topmost is taken
861-
onFacebookCompletion);
860+
scope,
861+
null, // the viewcontroller param can be null since by default topmost is taken
862+
onFacebookCompletion);
862863

863864
} else if (arg.type === firebase.LoginType.GOOGLE) {
864865
if (typeof (GIDSignIn) === "undefined") {
@@ -1194,15 +1195,15 @@ firebase.addValueEventListener = (updateCallback, path) => {
11941195
try {
11951196
const where = path === undefined ? FIRDatabase.database().reference() : FIRDatabase.database().reference().childByAppendingPath(path);
11961197
const listener = where.observeEventTypeWithBlockWithCancelBlock(
1197-
FIRDataEventType.Value,
1198-
snapshot => {
1199-
updateCallback(firebase.getCallbackData('ValueChanged', snapshot));
1200-
},
1201-
firebaseError => {
1202-
updateCallback({
1203-
error: firebaseError.localizedDescription
1198+
FIRDataEventType.Value,
1199+
snapshot => {
1200+
updateCallback(firebase.getCallbackData('ValueChanged', snapshot));
1201+
},
1202+
firebaseError => {
1203+
updateCallback({
1204+
error: firebaseError.localizedDescription
1205+
});
12041206
});
1205-
});
12061207
resolve({
12071208
path: path,
12081209
listeners: [listener]
@@ -1219,13 +1220,13 @@ firebase.getValue = path => {
12191220
try {
12201221
const where = path === undefined ? FIRDatabase.database().reference() : FIRDatabase.database().reference().childByAppendingPath(path);
12211222
const listener = where.observeSingleEventOfTypeWithBlockWithCancelBlock(
1222-
FIRDataEventType.Value,
1223-
snapshot => {
1224-
resolve(firebase.getCallbackData('ValueChanged', snapshot));
1225-
},
1226-
firebaseError => {
1227-
reject(firebaseError.localizedDescription);
1228-
});
1223+
FIRDataEventType.Value,
1224+
snapshot => {
1225+
resolve(firebase.getCallbackData('ValueChanged', snapshot));
1226+
},
1227+
firebaseError => {
1228+
reject(firebaseError.localizedDescription);
1229+
});
12291230
} catch (ex) {
12301231
console.log("Error in firebase.getValue: " + ex);
12311232
reject(ex);
@@ -1549,18 +1550,18 @@ firebase.firestore.WriteBatch = (nativeWriteBatch: FIRWriteBatch): firestore.Wri
15491550
fixSpecialFields(data);
15501551
nativeWriteBatch.setDataForDocumentMerge(<any>data, documentRef.ios, options && options.merge);
15511552
return this;
1552-
}
1553+
};
15531554

15541555
public update = (documentRef: firestore.DocumentReference, data: firestore.UpdateData): firestore.WriteBatch => {
15551556
fixSpecialFields(data);
15561557
nativeWriteBatch.updateDataForDocument(<any>data, documentRef.ios);
15571558
return this;
1558-
}
1559+
};
15591560

15601561
public delete = (documentRef: firestore.DocumentReference): firestore.WriteBatch => {
15611562
nativeWriteBatch.deleteDocument(documentRef.ios);
15621563
return this;
1563-
}
1564+
};
15641565

15651566
commit(): Promise<void> {
15661567
return new Promise<void>((resolve, reject) => {
@@ -1612,11 +1613,11 @@ firebase.firestore.Transaction = (nativeTransaction: FIRTransaction): firestore.
16121613
firebase.firestore.runTransaction = (updateFunction: (transaction: firestore.Transaction) => Promise<any>): Promise<void> => {
16131614
return new Promise<void>((resolve, reject) => {
16141615
FIRFirestore.firestore().runTransactionWithBlockCompletion(
1615-
(nativeTransaction: FIRTransaction, err: any) => {
1616-
const tx = new firebase.firestore.Transaction(nativeTransaction);
1617-
return updateFunction(tx);
1618-
},
1619-
(result, error: NSError) => error ? reject(error.localizedDescription) : resolve());
1616+
(nativeTransaction: FIRTransaction, err: any) => {
1617+
const tx = new firebase.firestore.Transaction(nativeTransaction);
1618+
return updateFunction(tx);
1619+
},
1620+
(result, error: NSError) => error ? reject(error.localizedDescription) : resolve());
16201621
});
16211622
};
16221623

@@ -1740,24 +1741,24 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
17401741
fixSpecialFields(document);
17411742
const defaultFirestore = FIRFirestore.firestore();
17421743
const fIRDocumentReference = defaultFirestore
1743-
.collectionWithPath(collectionPath)
1744-
.addDocumentWithDataCompletion(document, (error: NSError) => {
1745-
if (error) {
1746-
reject(error.localizedDescription);
1747-
} else {
1748-
resolve({
1749-
discriminator: "docRef",
1750-
id: fIRDocumentReference.documentID,
1751-
path: fIRDocumentReference.path,
1752-
collection: cp => firebase.firestore.collection(cp),
1753-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
1754-
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
1755-
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
1756-
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
1757-
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback)
1758-
});
1759-
}
1760-
});
1744+
.collectionWithPath(collectionPath)
1745+
.addDocumentWithDataCompletion(document, (error: NSError) => {
1746+
if (error) {
1747+
reject(error.localizedDescription);
1748+
} else {
1749+
resolve({
1750+
discriminator: "docRef",
1751+
id: fIRDocumentReference.documentID,
1752+
path: fIRDocumentReference.path,
1753+
collection: cp => firebase.firestore.collection(cp),
1754+
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
1755+
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
1756+
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
1757+
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
1758+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback)
1759+
});
1760+
}
1761+
});
17611762

17621763
} catch (ex) {
17631764
console.log("Error in firebase.firestore.add: " + ex);
@@ -1777,8 +1778,8 @@ firebase.firestore.set = (collectionPath: string, documentPath: string, document
17771778
fixSpecialFields(document);
17781779

17791780
const docRef: FIRDocumentReference = FIRFirestore.firestore()
1780-
.collectionWithPath(collectionPath)
1781-
.documentWithPath(documentPath);
1781+
.collectionWithPath(collectionPath)
1782+
.documentWithPath(documentPath);
17821783

17831784
if (options && options.merge) {
17841785
docRef.setDataMergeCompletion(document, true, (error: NSError) => {
@@ -1816,7 +1817,9 @@ function fixSpecialFields(item) {
18161817
}
18171818

18181819
function fixSpecialField(item): any {
1819-
if (item === "SERVER_TIMESTAMP") {
1820+
if (item === null) {
1821+
return null;
1822+
} else if (item === "SERVER_TIMESTAMP") {
18201823
return FIRFieldValue.fieldValueForServerTimestamp();
18211824
} else if (item instanceof FieldValue) {
18221825
const fieldValue: FieldValue = item;
@@ -1853,8 +1856,8 @@ firebase.firestore.update = (collectionPath: string, documentPath: string, docum
18531856
fixSpecialFields(document);
18541857

18551858
const docRef: FIRDocumentReference = FIRFirestore.firestore()
1856-
.collectionWithPath(collectionPath)
1857-
.documentWithPath(documentPath);
1859+
.collectionWithPath(collectionPath)
1860+
.documentWithPath(documentPath);
18581861

18591862
docRef.updateDataCompletion(document, (error: NSError) => {
18601863
if (error) {
@@ -1879,8 +1882,8 @@ firebase.firestore.delete = (collectionPath: string, documentPath: string): Prom
18791882
}
18801883

18811884
const docRef: FIRDocumentReference = FIRFirestore.firestore()
1882-
.collectionWithPath(collectionPath)
1883-
.documentWithPath(documentPath);
1885+
.collectionWithPath(collectionPath)
1886+
.documentWithPath(documentPath);
18841887

18851888
docRef.deleteDocumentWithCompletion((error: NSError) => {
18861889
if (error) {
@@ -1907,14 +1910,14 @@ firebase.firestore.getCollection = (collectionPath: string): Promise<firestore.Q
19071910

19081911
const defaultFirestore = FIRFirestore.firestore();
19091912
defaultFirestore
1910-
.collectionWithPath(collectionPath)
1911-
.getDocumentsWithCompletion((snapshot: FIRQuerySnapshot, error: NSError) => {
1912-
if (error) {
1913-
reject(error.localizedDescription);
1914-
} else {
1915-
resolve(new QuerySnapshot(snapshot));
1916-
}
1917-
});
1913+
.collectionWithPath(collectionPath)
1914+
.getDocumentsWithCompletion((snapshot: FIRQuerySnapshot, error: NSError) => {
1915+
if (error) {
1916+
reject(error.localizedDescription);
1917+
} else {
1918+
resolve(new QuerySnapshot(snapshot));
1919+
}
1920+
});
19181921

19191922
} catch (ex) {
19201923
console.log("Error in firebase.firestore.getCollection: " + ex);
@@ -1936,15 +1939,15 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
19361939
}
19371940

19381941
FIRFirestore.firestore()
1939-
.collectionWithPath(collectionPath)
1940-
.documentWithPath(documentPath)
1941-
.getDocumentWithCompletion((snapshot: FIRDocumentSnapshot, error: NSError) => {
1942-
if (error) {
1943-
reject(error.localizedDescription);
1944-
} else {
1945-
resolve(new DocumentSnapshot(snapshot));
1946-
}
1947-
});
1942+
.collectionWithPath(collectionPath)
1943+
.documentWithPath(documentPath)
1944+
.getDocumentWithCompletion((snapshot: FIRDocumentSnapshot, error: NSError) => {
1945+
if (error) {
1946+
reject(error.localizedDescription);
1947+
} else {
1948+
resolve(new DocumentSnapshot(snapshot));
1949+
}
1950+
});
19481951

19491952
} catch (ex) {
19501953
console.log("Error in firebase.firestore.getDocument: " + ex);
@@ -2099,10 +2102,14 @@ function convertDocRef(docRef: FIRDocumentReference): firestore.DocumentReferenc
20992102

21002103
function convertDocChangeType(type: FIRDocumentChangeType) {
21012104
switch (type) {
2102-
case FIRDocumentChangeType.Added: return 'added';
2103-
case FIRDocumentChangeType.Modified: return 'modified';
2104-
case FIRDocumentChangeType.Removed: return 'removed';
2105-
default: throw new Error('Unknown DocumentChangeType');
2105+
case FIRDocumentChangeType.Added:
2106+
return 'added';
2107+
case FIRDocumentChangeType.Modified:
2108+
return 'modified';
2109+
case FIRDocumentChangeType.Removed:
2110+
return 'removed';
2111+
default:
2112+
throw new Error('Unknown DocumentChangeType');
21062113
}
21072114
}
21082115

@@ -2113,9 +2120,8 @@ function convertDocument(qDoc: FIRQueryDocumentSnapshot): firestore.QueryDocumen
21132120
export class QuerySnapshot implements firestore.QuerySnapshot {
21142121
private _docSnapshots: firestore.QueryDocumentSnapshot[];
21152122

2116-
constructor(
2117-
private snapshot: FIRQuerySnapshot,
2118-
) { }
2123+
constructor(private snapshot: FIRQuerySnapshot) {
2124+
}
21192125

21202126
get docs(): firestore.QueryDocumentSnapshot[] {
21212127
const getSnapshots = () => {
@@ -2139,7 +2145,9 @@ export class QuerySnapshot implements firestore.QuerySnapshot {
21392145
docSnapshots = this.docs as firestore.DocumentSnapshot[]; // It's safe to cast, as our document snapshot already has the data() method
21402146

21412147
docChanges(options?: firestore.SnapshotListenOptions): firestore.DocumentChange[] {
2142-
if (options) { console.info('No options support yet, for docChanges()'); }
2148+
if (options) {
2149+
console.info('No options support yet, for docChanges()');
2150+
}
21432151
const docChanges: firestore.DocumentChange[] = [];
21442152
const jChanges: NSArray<FIRDocumentChange> = this.snapshot.documentChanges;
21452153
for (let i = 0; i < jChanges.count; i++) {

0 commit comments

Comments
 (0)