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

Commit

Permalink
GeoPoint data type. #756
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Jun 23, 2018
1 parent c343398 commit 766e606
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
[Firebase iOS SDK Changelog](https://firebase.google.com/support/release-notes/ios)
[Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)

## 6.3.0 (2018, June 23)
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/62?closed=1)


## 6.2.0 (2018, June 19)
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/61?closed=1)

Expand Down
12 changes: 4 additions & 8 deletions demo-ng/app/tabs/firestore/firestore.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ export class FirestoreComponent {
public firestoreSetByAutoID(): void {
firebase.firestore().collection("dogs").doc()
.set({name: "Woofie", last: "lastofwoofie", date: new Date()})
.then(() => {
console.log("Woofie set");
})
.then(() => console.log("Woofie set"))
.catch(err => console.log("Setting Woofie failed, error: " + err));
}

Expand All @@ -124,11 +122,10 @@ export class FirestoreComponent {
.update({
name: "Woofieupdate",
last: "updatedwoofie!",
updateTs: firebase.firestore().FieldValue().serverTimestamp()
})
.then(() => {
console.log("Woofie updated");
updateTs: firebase.firestore().FieldValue().serverTimestamp(),
lastKnownLocation: firebase.firestore().GeoPoint(4.34, 5.67)
})
.then(() => console.log("Woofie updated"))
.catch(err => console.log("Updating Woofie failed, error: " + JSON.stringify(err)));
}

Expand Down Expand Up @@ -177,7 +174,6 @@ export class FirestoreComponent {
});
}


firestoreDocumentObservable(): void {
this.myCity$ = Observable.create(subscriber => {
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
Expand Down
6 changes: 4 additions & 2 deletions docs/FIRESTORE.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ citiesCollection.add({
state: "CA",
country: "USA",
capital: false,
population: 860000
population: 860000,
location: firebase.firestore().GeoPoint(4.34, 5.67)
}).then(documentRef => {
console.log(`San Francisco added with auto-generated ID: ${documentRef.id}`);
});
Expand Down Expand Up @@ -162,7 +163,8 @@ const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF")

sanFranciscoDocument.update({
population: 860001,
updateTimestamp: firebase.firestore().FieldValue().serverTimestamp()
updateTimestamp: firebase.firestore().FieldValue().serverTimestamp(),
location: firebase.firestore().GeoPoint(4.34, 5.67)
}).then(() => {
console.log("SF population updated");
});
Expand Down
5 changes: 5 additions & 0 deletions src/app/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ export module firestore {
collection(collectionPath: string): firebase.firestore.CollectionReference {
return firebase.firestore.collection(collectionPath);
}

FieldValue(): firebase.firestore.FieldValue {
return {
serverTimestamp: () => "SERVER_TIMESTAMP"
}
}

GeoPoint(latitude: number, longitude: number): firebase.firestore.GeoPoint {
return firebase.firestore.GeoPoint(latitude, longitude);
}
}
}
8 changes: 7 additions & 1 deletion src/firebase-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export class FieldValue {
serverTimestamp = () => "SERVER_TIMESTAMP";
}

export class GeoPoint {
constructor(public latitude: number, public longitude: number) {
}
}

export const firebase: any = {
initialized: false,
instance: null,
Expand All @@ -21,7 +26,8 @@ export const firebase: any = {
storage,
mlkit,
firestore: {
FieldValue
FieldValue,
GeoPoint: (latitude: number, longitude: number) => new GeoPoint(latitude, longitude)
},
invites: {
MATCH_TYPE: {
Expand Down
7 changes: 4 additions & 3 deletions src/firebase.android.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { DocumentSnapshot, firebase, QuerySnapshot } from "./firebase-common";
import { DocumentSnapshot, firebase, GeoPoint, QuerySnapshot } from "./firebase-common";
import * as appModule from "tns-core-modules/application";
import { AndroidActivityResultEventData } from "tns-core-modules/application";
import { ad as AndroidUtils, layout } from "tns-core-modules/utils/utils";
import lazy from "tns-core-modules/utils/lazy";
import { topmost } from "tns-core-modules/ui/frame";
import { File } from "tns-core-modules/file-system";
import { firestore, User } from "./firebase";

declare const android, com, org: any;
Expand Down Expand Up @@ -130,11 +129,13 @@ firebase.toHashMap = obj => {
node.put(property, com.google.firebase.firestore.FieldValue.serverTimestamp());
} else if (obj[property] instanceof Date) {
node.put(property, new java.util.Date(obj[property].getTime()));
} else if (obj[property] instanceof GeoPoint) {
const geo = <GeoPoint>obj[property];
node.put(property, new com.google.firebase.firestore.GeoPoint(geo.latitude, geo.longitude));
} else if (Array.isArray(obj[property])) {
node.put(property, firebase.toJavaArray(obj[property]));
} else {
switch (typeof obj[property]) {
case 'object':
case 'object':
node.put(property, firebase.toHashMap(obj[property], node));
break;
Expand Down
7 changes: 7 additions & 0 deletions src/firebase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ export namespace firestore {
export type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>';
export type OrderByDirection = 'desc' | 'asc';

export interface GeoPoint {
longitude: number;
latitude: number;
}

export function GeoPoint(latitude: number, longitude: number): GeoPoint;

export interface SetOptions {
merge?: boolean;
}
Expand Down
29 changes: 18 additions & 11 deletions src/firebase.ios.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { firebase, DocumentSnapshot, QuerySnapshot } from "./firebase-common";
import { firebase, DocumentSnapshot, QuerySnapshot, GeoPoint } from "./firebase-common";
import * as application from "tns-core-modules/application";
import { ios as iOSUtils } from "tns-core-modules/utils/utils";
import { getClass } from "tns-core-modules/utils/types";
Expand Down Expand Up @@ -645,10 +645,10 @@ firebase.toJsObject = objCObj => {
node[key] = firebase.firestore._getDocumentReference(val, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1));
break;
case 'FIRGeoPoint':
node[key] = {
latitude: (<FIRGeoPoint>val).latitude,
longitude: (<FIRGeoPoint>val).longitude
};
node[key] = firestore.GeoPoint(
(<FIRGeoPoint>val).latitude,
(<FIRGeoPoint>val).longitude
);
break;
default:
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.");
Expand Down Expand Up @@ -2246,7 +2246,7 @@ firebase.firestore.set = (collectionPath: string, documentPath: string, document
return;
}

fixServerTimestamp(document);
fixSpecialFields(document);

const docRef: FIRDocumentReference = FIRFirestore.firestore()
.collectionWithPath(collectionPath)
Expand Down Expand Up @@ -2278,10 +2278,18 @@ firebase.firestore.set = (collectionPath: string, documentPath: string, document
});
};

function fixServerTimestamp(item) {
function fixSpecialFields(item) {
for (let k in item) {
if (item.hasOwnProperty(k) && item[k] === "SERVER_TIMESTAMP") {
item[k] = FIRFieldValue.fieldValueForServerTimestamp();
if (item.hasOwnProperty(k)) {
if (item[k] === "SERVER_TIMESTAMP") {
item[k] = FIRFieldValue.fieldValueForServerTimestamp();
} else if (item[k] instanceof GeoPoint) {
const geo = <GeoPoint>item[k];
item[k] = new FIRGeoPoint({
latitude: geo.latitude,
longitude: geo.longitude
});
}
}
}
}
Expand All @@ -2294,7 +2302,7 @@ firebase.firestore.update = (collectionPath: string, documentPath: string, docum
return;
}

fixServerTimestamp(document);
fixSpecialFields(document);

const docRef: FIRDocumentReference = FIRFirestore.firestore()
.collectionWithPath(collectionPath)
Expand All @@ -2307,7 +2315,6 @@ firebase.firestore.update = (collectionPath: string, documentPath: string, docum
resolve();
}
});

} catch (ex) {
console.log("Error in firebase.firestore.update: " + ex);
reject(ex);
Expand Down

0 comments on commit 766e606

Please sign in to comment.