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

Commit 766e606

Browse files
GeoPoint data type. #756
1 parent c343398 commit 766e606

File tree

8 files changed

+53
-25
lines changed

8 files changed

+53
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
[Firebase iOS SDK Changelog](https://firebase.google.com/support/release-notes/ios)
44
[Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)
55

6+
## 6.3.0 (2018, June 23)
7+
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/62?closed=1)
8+
9+
610
## 6.2.0 (2018, June 19)
711
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/61?closed=1)
812

demo-ng/app/tabs/firestore/firestore.component.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ export class FirestoreComponent {
113113
public firestoreSetByAutoID(): void {
114114
firebase.firestore().collection("dogs").doc()
115115
.set({name: "Woofie", last: "lastofwoofie", date: new Date()})
116-
.then(() => {
117-
console.log("Woofie set");
118-
})
116+
.then(() => console.log("Woofie set"))
119117
.catch(err => console.log("Setting Woofie failed, error: " + err));
120118
}
121119

@@ -124,11 +122,10 @@ export class FirestoreComponent {
124122
.update({
125123
name: "Woofieupdate",
126124
last: "updatedwoofie!",
127-
updateTs: firebase.firestore().FieldValue().serverTimestamp()
128-
})
129-
.then(() => {
130-
console.log("Woofie updated");
125+
updateTs: firebase.firestore().FieldValue().serverTimestamp(),
126+
lastKnownLocation: firebase.firestore().GeoPoint(4.34, 5.67)
131127
})
128+
.then(() => console.log("Woofie updated"))
132129
.catch(err => console.log("Updating Woofie failed, error: " + JSON.stringify(err)));
133130
}
134131

@@ -177,7 +174,6 @@ export class FirestoreComponent {
177174
});
178175
}
179176

180-
181177
firestoreDocumentObservable(): void {
182178
this.myCity$ = Observable.create(subscriber => {
183179
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");

docs/FIRESTORE.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ citiesCollection.add({
122122
state: "CA",
123123
country: "USA",
124124
capital: false,
125-
population: 860000
125+
population: 860000,
126+
location: firebase.firestore().GeoPoint(4.34, 5.67)
126127
}).then(documentRef => {
127128
console.log(`San Francisco added with auto-generated ID: ${documentRef.id}`);
128129
});
@@ -162,7 +163,8 @@ const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF")
162163

163164
sanFranciscoDocument.update({
164165
population: 860001,
165-
updateTimestamp: firebase.firestore().FieldValue().serverTimestamp()
166+
updateTimestamp: firebase.firestore().FieldValue().serverTimestamp(),
167+
location: firebase.firestore().GeoPoint(4.34, 5.67)
166168
}).then(() => {
167169
console.log("SF population updated");
168170
});

src/app/firestore/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ export module firestore {
55
collection(collectionPath: string): firebase.firestore.CollectionReference {
66
return firebase.firestore.collection(collectionPath);
77
}
8+
89
FieldValue(): firebase.firestore.FieldValue {
910
return {
1011
serverTimestamp: () => "SERVER_TIMESTAMP"
1112
}
1213
}
14+
15+
GeoPoint(latitude: number, longitude: number): firebase.firestore.GeoPoint {
16+
return firebase.firestore.GeoPoint(latitude, longitude);
17+
}
1318
}
1419
}

src/firebase-common.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export class FieldValue {
1010
serverTimestamp = () => "SERVER_TIMESTAMP";
1111
}
1212

13+
export class GeoPoint {
14+
constructor(public latitude: number, public longitude: number) {
15+
}
16+
}
17+
1318
export const firebase: any = {
1419
initialized: false,
1520
instance: null,
@@ -21,7 +26,8 @@ export const firebase: any = {
2126
storage,
2227
mlkit,
2328
firestore: {
24-
FieldValue
29+
FieldValue,
30+
GeoPoint: (latitude: number, longitude: number) => new GeoPoint(latitude, longitude)
2531
},
2632
invites: {
2733
MATCH_TYPE: {

src/firebase.android.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { DocumentSnapshot, firebase, QuerySnapshot } from "./firebase-common";
1+
import { DocumentSnapshot, firebase, GeoPoint, QuerySnapshot } from "./firebase-common";
22
import * as appModule from "tns-core-modules/application";
33
import { AndroidActivityResultEventData } from "tns-core-modules/application";
44
import { ad as AndroidUtils, layout } from "tns-core-modules/utils/utils";
55
import lazy from "tns-core-modules/utils/lazy";
66
import { topmost } from "tns-core-modules/ui/frame";
7-
import { File } from "tns-core-modules/file-system";
87
import { firestore, User } from "./firebase";
98

109
declare const android, com, org: any;
@@ -130,11 +129,13 @@ firebase.toHashMap = obj => {
130129
node.put(property, com.google.firebase.firestore.FieldValue.serverTimestamp());
131130
} else if (obj[property] instanceof Date) {
132131
node.put(property, new java.util.Date(obj[property].getTime()));
132+
} else if (obj[property] instanceof GeoPoint) {
133+
const geo = <GeoPoint>obj[property];
134+
node.put(property, new com.google.firebase.firestore.GeoPoint(geo.latitude, geo.longitude));
133135
} else if (Array.isArray(obj[property])) {
134136
node.put(property, firebase.toJavaArray(obj[property]));
135137
} else {
136138
switch (typeof obj[property]) {
137-
case 'object':
138139
case 'object':
139140
node.put(property, firebase.toHashMap(obj[property], node));
140141
break;

src/firebase.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,13 @@ export namespace firestore {
708708
export type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>';
709709
export type OrderByDirection = 'desc' | 'asc';
710710

711+
export interface GeoPoint {
712+
longitude: number;
713+
latitude: number;
714+
}
715+
716+
export function GeoPoint(latitude: number, longitude: number): GeoPoint;
717+
711718
export interface SetOptions {
712719
merge?: boolean;
713720
}

src/firebase.ios.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { firebase, DocumentSnapshot, QuerySnapshot } from "./firebase-common";
1+
import { firebase, DocumentSnapshot, QuerySnapshot, GeoPoint } from "./firebase-common";
22
import * as application from "tns-core-modules/application";
33
import { ios as iOSUtils } from "tns-core-modules/utils/utils";
44
import { getClass } from "tns-core-modules/utils/types";
@@ -645,10 +645,10 @@ firebase.toJsObject = objCObj => {
645645
node[key] = firebase.firestore._getDocumentReference(val, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1));
646646
break;
647647
case 'FIRGeoPoint':
648-
node[key] = {
649-
latitude: (<FIRGeoPoint>val).latitude,
650-
longitude: (<FIRGeoPoint>val).longitude
651-
};
648+
node[key] = firestore.GeoPoint(
649+
(<FIRGeoPoint>val).latitude,
650+
(<FIRGeoPoint>val).longitude
651+
);
652652
break;
653653
default:
654654
console.log("Please report this at https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues: iOS toJsObject is missing a converter for class '" + getClass(val) + "'. Casting to String as a fallback.");
@@ -2246,7 +2246,7 @@ firebase.firestore.set = (collectionPath: string, documentPath: string, document
22462246
return;
22472247
}
22482248

2249-
fixServerTimestamp(document);
2249+
fixSpecialFields(document);
22502250

22512251
const docRef: FIRDocumentReference = FIRFirestore.firestore()
22522252
.collectionWithPath(collectionPath)
@@ -2278,10 +2278,18 @@ firebase.firestore.set = (collectionPath: string, documentPath: string, document
22782278
});
22792279
};
22802280

2281-
function fixServerTimestamp(item) {
2281+
function fixSpecialFields(item) {
22822282
for (let k in item) {
2283-
if (item.hasOwnProperty(k) && item[k] === "SERVER_TIMESTAMP") {
2284-
item[k] = FIRFieldValue.fieldValueForServerTimestamp();
2283+
if (item.hasOwnProperty(k)) {
2284+
if (item[k] === "SERVER_TIMESTAMP") {
2285+
item[k] = FIRFieldValue.fieldValueForServerTimestamp();
2286+
} else if (item[k] instanceof GeoPoint) {
2287+
const geo = <GeoPoint>item[k];
2288+
item[k] = new FIRGeoPoint({
2289+
latitude: geo.latitude,
2290+
longitude: geo.longitude
2291+
});
2292+
}
22852293
}
22862294
}
22872295
}
@@ -2294,7 +2302,7 @@ firebase.firestore.update = (collectionPath: string, documentPath: string, docum
22942302
return;
22952303
}
22962304

2297-
fixServerTimestamp(document);
2305+
fixSpecialFields(document);
22982306

22992307
const docRef: FIRDocumentReference = FIRFirestore.firestore()
23002308
.collectionWithPath(collectionPath)
@@ -2307,7 +2315,6 @@ firebase.firestore.update = (collectionPath: string, documentPath: string, docum
23072315
resolve();
23082316
}
23092317
});
2310-
23112318
} catch (ex) {
23122319
console.log("Error in firebase.firestore.update: " + ex);
23132320
reject(ex);

0 commit comments

Comments
 (0)