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

Commit f697243

Browse files
#594 Android - Background notification causing app to crash after upgrade to 5.1.1
#595 Creating Observables from Firestore Documents/Collections
1 parent b4f43e7 commit f697243

File tree

10 files changed

+168
-34
lines changed

10 files changed

+168
-34
lines changed

CHANGELOG.md

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

66

7+
## 5.1.2 (2018, January 8)
8+
9+
### Fixes
10+
- [#594](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/594) Android - Background notification causing app to crash after upgrade to 5.1.1
11+
12+
### New
13+
- [#595](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/595) Creating Observables from Firestore Documents/Collections
14+
15+
716
## 5.1.1 (2017, December 30)
817

918
### Fixes

demo-ng/app/item/items.component.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,26 @@
1111
<Label text="Realtime DB" class="h2"></Label>
1212
<Button text="Get companies" (tap)="doWebGetValueForCompanies()" class="button"></Button>
1313

14+
15+
16+
1417
<Label text="Firestore" class="h2"></Label>
1518
<Button text="Add" (tap)="firestoreAdd()" class="button"></Button>
1619
<Button text="Set" (tap)="firestoreSet()" class="button"></Button>
1720
<Button text="Set (auto id)" (tap)="firestoreSetByAutoID()" class="button"></Button>
1821
<Button text="Update" (tap)="firestoreUpdate()" class="button"></Button>
1922
<Button text="Get" (tap)="firestoreGet()" class="button"></Button>
2023
<Button text="Get nested" (tap)="firestoreGetNested()" class="button"></Button>
24+
<Button text="Document Observable (SF)" (tap)="firestoreDocumentObservable()" class="button"></Button>
25+
<Label [text]="(myCity$ | async).name" height="32" *ngIf="myCity$"></Label>
26+
<Button text="Collection Observable (cities)" (tap)="firestoreCollectionObservable()" class="button"></Button>
27+
<ListView [items]="myCities$ | async" height="200" *ngIf="myCities$">
28+
<template let-item="item">
29+
<StackLayout>
30+
<Label style="color: red" [text]='item.name'></Label>
31+
</StackLayout>
32+
</template>
33+
</ListView>
2134
<Button text="Listen to changes in SF" (tap)="firestoreListen()" class="button"></Button>
2235
<Button text="Stop listening" (tap)="firestoreStopListening()" class="button"></Button>
2336
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>

demo-ng/app/item/items.component.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { Component, OnInit } from "@angular/core";
1+
import { Component, NgZone, OnInit } from "@angular/core";
22
import { firestore } from "nativescript-plugin-firebase";
3+
import { Observable } from "rxjs/Observable";
4+
import { City } from "../model/City";
5+
36
const firebase = require("nativescript-plugin-firebase/app");
47
const firebaseWebApi = require("nativescript-plugin-firebase/app");
8+
59
// import { AngularFireModule } from 'angularfire2';
610

711
@Component({
@@ -13,14 +17,21 @@ export class ItemsComponent implements OnInit {
1317

1418
private listenerUnsubscribe: () => void;
1519

16-
constructor() {
20+
public myCity$: Observable<City>;
21+
public myCities$: Observable<Array<City>>;
22+
private city: City;
23+
private cities: Array<City> = [];
24+
25+
constructor(private zone: NgZone) {
1726
// AngularFireModule.initializeApp({});
1827
}
1928

2029
ngOnInit(): void {
2130
firebase.initializeApp({
2231
persist: false
23-
}).then(() => console.log("Firebase initialized"));
32+
}).then(() => {
33+
console.log("Firebase initialized");
34+
});
2435
}
2536

2637
public loginAnonymously(): void {
@@ -147,7 +158,7 @@ export class ItemsComponent implements OnInit {
147158
.doc("SF")
148159
.collection("streets")
149160
.doc("QZNrg22tkN8W71YC3qCb"); // id of 'main st.'
150-
// .doc("doesntexist");
161+
// .doc("doesntexist");
151162

152163
mainStreetInSFDocRef.get().then((doc: firestore.DocumentSnapshot) => {
153164
if (doc.exists) {
@@ -160,6 +171,32 @@ export class ItemsComponent implements OnInit {
160171
});
161172
}
162173

174+
175+
firestoreDocumentObservable(): void {
176+
this.myCity$ = Observable.create(subscriber => {
177+
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
178+
docRef.onSnapshot((doc: firestore.DocumentSnapshot) => {
179+
this.zone.run(() => {
180+
this.city = <City>doc.data();
181+
subscriber.next(this.city);
182+
});
183+
});
184+
});
185+
}
186+
187+
firestoreCollectionObservable(): void {
188+
this.myCities$ = Observable.create(subscriber => {
189+
const colRef: firestore.CollectionReference = firebase.firestore().collection("cities");
190+
colRef.onSnapshot((snapshot: firestore.QuerySnapshot) => {
191+
this.zone.run(() => {
192+
this.cities = [];
193+
snapshot.forEach(docSnap => this.cities.push(<City>docSnap.data()));
194+
subscriber.next(this.cities);
195+
});
196+
});
197+
});
198+
}
199+
163200
public firestoreListen(): void {
164201
if (this.listenerUnsubscribe !== undefined) {
165202
console.log("Already listening ;)");

demo-ng/app/model/City.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface City {
2+
country: string;
3+
name: string;
4+
population: number;
5+
}

demo-ng/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"repository": "<fill-your-repository-here>",
66
"nativescript": {
77
"id": "org.nativescript.firebasedemo.firestore",
8-
"tns-android": {
8+
"tns-ios": {
99
"version": "3.4.0"
1010
},
11-
"tns-ios": {
11+
"tns-android": {
1212
"version": "3.4.0"
1313
}
1414
},
@@ -24,7 +24,7 @@
2424
"angularfire2": "^5.0.0-rc.4",
2525
"firebase": "^4.6.2",
2626
"nativescript-angular": "~4.4.0",
27-
"nativescript-plugin-firebase": "5.1.1",
27+
"nativescript-plugin-firebase": "5.1.2",
2828
"nativescript-theme-core": "~1.0.2",
2929
"reflect-metadata": "~0.1.8",
3030
"rxjs": "~5.4.2",

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
},
1111
"dependencies": {
12-
"nativescript-plugin-firebase": "5.1.1",
12+
"nativescript-plugin-firebase": "5.1.2",
1313
"nativescript-theme-core": "^1.0.4",
1414
"nativescript-unit-test-runner": "^0.3.4",
1515
"tns-core-modules": "~3.4.0"

src/firebase.android.ts

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ firebase.toHashMap = obj => {
9898
if (obj[property] === null) {
9999
node.put(property, null);
100100
} else {
101-
if(obj[property] instanceof Date) {
102-
node.put(property, new java.util.Date(obj[property].getTime()));
103-
} else if (Array.isArray(obj[property])){
104-
node.put(property, firebase.toJavaArray(obj[property]));
101+
if (obj[property] instanceof Date) {
102+
node.put(property, new java.util.Date(obj[property].getTime()));
103+
} else if (Array.isArray(obj[property])) {
104+
node.put(property, firebase.toJavaArray(obj[property]));
105105
} else {
106106
switch (typeof obj[property]) {
107107
case 'object':
@@ -128,8 +128,8 @@ firebase.toHashMap = obj => {
128128
};
129129

130130
firebase.toJavaArray = val => {
131-
var javaArray = new java.util.ArrayList();
132-
for(var i = 0; i < val.length; i++) {
131+
const javaArray = new java.util.ArrayList();
132+
for (let i = 0; i < val.length; i++) {
133133
javaArray.add(firebase.toValue(val[i]));
134134
}
135135
return javaArray;
@@ -139,10 +139,10 @@ firebase.toValue = val => {
139139
let returnVal = null;
140140
if (val !== null) {
141141

142-
if(val instanceof Date) {
142+
if (val instanceof Date) {
143143
return new java.util.Date(val.getTime());
144144
}
145-
if(Array.isArray(val)) {
145+
if (Array.isArray(val)) {
146146
return firebase.toJavaArray(val);
147147
}
148148

@@ -220,6 +220,11 @@ firebase.authStateListener = null;
220220

221221
firebase.init = arg => {
222222
return new Promise((resolve, reject) => {
223+
if (firebase.initialized) {
224+
reject("Firebase already initialized");
225+
}
226+
firebase.initialized = true;
227+
223228
const runInit = () => {
224229
arg = arg || {};
225230

@@ -230,18 +235,24 @@ firebase.init = arg => {
230235

231236
const fDatabase = com.google.firebase.database.FirebaseDatabase;
232237
if (arg.persist) {
233-
fDatabase.getInstance().setPersistenceEnabled(true);
238+
try {
239+
fDatabase.getInstance().setPersistenceEnabled(true);
240+
} catch (ignore) {
241+
}
234242
}
235243
firebase.instance = fDatabase.getInstance().getReference();
236244
}
237245

238246
if (typeof(com.google.firebase.firestore) !== "undefined") {
239247
// Firestore has offline persistence enabled by default
240248
if (!arg.persist) {
241-
com.google.firebase.firestore.FirebaseFirestore.getInstance().setFirestoreSettings(
242-
new com.google.firebase.firestore.FirebaseFirestoreSettings.Builder()
243-
.setPersistenceEnabled(false)
244-
.build());
249+
try {
250+
com.google.firebase.firestore.FirebaseFirestore.getInstance().setFirestoreSettings(
251+
new com.google.firebase.firestore.FirebaseFirestoreSettings.Builder()
252+
.setPersistenceEnabled(false)
253+
.build());
254+
} catch (ignore) {
255+
}
245256
}
246257
}
247258

@@ -2230,7 +2241,8 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22302241
get: () => firebase.firestore.get(collectionPath),
22312242
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
22322243
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, collectionRef),
2233-
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef)
2244+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef),
2245+
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, callback)
22342246
};
22352247

22362248
} catch (ex) {
@@ -2239,7 +2251,7 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22392251
}
22402252
};
22412253

2242-
firebase.firestore.onSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, callback: (doc: DocumentSnapshot) => void): ()=> void => {
2254+
firebase.firestore.onDocumentSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, callback: (doc: DocumentSnapshot) => void): () => void => {
22432255
const listener = docRef.addSnapshotListener(new com.google.firebase.firestore.EventListener({
22442256
onEvent: ((snapshot: com.google.firebase.firestore.DocumentSnapshot, exception) => {
22452257
if (exception !== null) {
@@ -2256,6 +2268,28 @@ firebase.firestore.onSnapshot = (docRef: com.google.firebase.firestore.DocumentR
22562268
return () => listener.remove();
22572269
};
22582270

2271+
firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore.CollectionReference, callback: (snapshot: QuerySnapshot) => void): () => void => {
2272+
const listener = colRef.addSnapshotListener(new com.google.firebase.firestore.EventListener({
2273+
onEvent: ((snapshot: com.google.firebase.firestore.QuerySnapshot, exception) => {
2274+
if (exception !== null) {
2275+
return;
2276+
}
2277+
2278+
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
2279+
for (let i = 0; i < snapshot.size(); i++) {
2280+
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = snapshot.getDocuments().get(i);
2281+
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, () => firebase.toJsObject(documentSnapshot.getData())));
2282+
}
2283+
const snap = new QuerySnapshot();
2284+
snap.docSnapshots = docSnapshots;
2285+
callback(snap);
2286+
})
2287+
})
2288+
);
2289+
2290+
return () => listener.remove();
2291+
};
2292+
22592293
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
22602294
try {
22612295

@@ -2275,7 +2309,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
22752309
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
22762310
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
22772311
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2278-
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onSnapshot(docRef, callback)
2312+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, callback)
22792313
};
22802314

22812315
} catch (ex) {
@@ -2304,7 +2338,7 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
23042338
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
23052339
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
23062340
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2307-
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onSnapshot(docRef, callback)
2341+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, callback)
23082342
});
23092343
}
23102344
});
@@ -2548,7 +2582,8 @@ firebase.firestore._getQuery = (collectionPath: string, query: com.google.fireba
25482582
}),
25492583
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query),
25502584
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
2551-
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query)
2585+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
2586+
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, callback)
25522587
};
25532588
};
25542589

src/firebase.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ export namespace firestore {
818818
orderBy(fieldPath: string, directionStr: firestore.OrderByDirection): Query;
819819

820820
limit(limit: number): Query;
821+
822+
onSnapshot(callback: (snapshot: QuerySnapshot) => void): () => void;
821823
}
822824

823825
export interface CollectionReference extends Query {

0 commit comments

Comments
 (0)