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

Commit c60d9f7

Browse files
Firestore anytime soon? #507 (offline persistence)
1 parent 5825dc2 commit c60d9f7

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

demo-ng/app/app.css

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ refer to http://docs.nativescript.org/ui/theme.
1111
*/
1212
@import 'nativescript-theme-core/css/core.light.css';
1313

14+
label {
15+
padding: 6;
16+
font-size: 14;
17+
text-align: center;
18+
}
19+
1420
.h2 {
15-
font-size: 16;
21+
font-size: 17;
1622
font-weight: bold;
17-
text-align: center;
1823
margin: 22 0 0 0;
1924
}
2025

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<ActionBar title="My App" class="action-bar">
1+
<ActionBar title="Firestore demo" class="action-bar" flat="true">
22
</ActionBar>
33

44
<ScrollView>
55
<StackLayout class="page">
6+
<Label text="After pressing those buttons, check the console log" textWrap="true"></Label>
67
<Label text="Authentication" class="h2"></Label>
78
<Button text="login anonymously" (tap)="loginAnonymously()" class="button button-user"></Button>
89
<Label text="Firestore" class="h2"></Label>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Component, OnInit } from "@angular/core";
22
import { firestore } from "nativescript-plugin-firebase";
3-
// import { firestore } from "nativescript-plugin-firebase/app/firestore";
4-
53
const firebase = require("nativescript-plugin-firebase/app");
64

75
@Component({
@@ -15,7 +13,9 @@ export class ItemsComponent implements OnInit {
1513
}
1614

1715
ngOnInit(): void {
18-
firebase.initializeApp();
16+
firebase.initializeApp({
17+
persist: false
18+
}).then(() => console.log("Firebase initialized"));
1919
}
2020

2121
public loginAnonymously(): void {

src/app/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import { auth as firebaseAuthModule } from "./auth";
88
import { database as firebaseDatabaseModule } from "./database";
99
import { firestore as firebaseFirestoreModule } from "./firestore";
1010

11-
export function initializeApp(options?: Object, name?: string): any /* TODO App object.. doesn't look too useful though */ {
12-
firebase.init().then(
13-
() => console.log("Firebase is ready"),
14-
error => console.log("firebase.init error: " + error)
15-
);
11+
export function initializeApp(options?: firebase.InitOptions, name? /* ignored */: string): Promise<any> {
12+
return firebase.init(options);
1613
}
1714

1815
let authCache;

src/firebase.android.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { firebase, QuerySnapshot } from "./firebase-common";
22
import * as appModule from "tns-core-modules/application";
3+
import { AndroidActivityResultEventData } from "tns-core-modules/application";
34
import * as utils from "tns-core-modules/utils/utils";
45
import lazy from "tns-core-modules/utils/lazy";
56
import * as frame from "tns-core-modules/ui/frame";
67
import * as fs from "tns-core-modules/file-system";
7-
import { AndroidActivityResultEventData } from "tns-core-modules/application";
88
import { firestore } from "./firebase";
99

1010
declare const com, org: any;
@@ -200,18 +200,25 @@ firebase.init = arg => {
200200

201201
arg = arg || {};
202202

203-
firebase.ServerValue = {
204-
TIMESTAMP: firebase.toJsObject(com.google.firebase.database.ServerValue.TIMESTAMP)
205-
};
203+
if (typeof(com.google.firebase.firestore) === "undefined") {
204+
firebase.ServerValue = {
205+
TIMESTAMP: firebase.toJsObject(com.google.firebase.database.ServerValue.TIMESTAMP)
206+
};
206207

207-
const fDatabase = com.google.firebase.database.FirebaseDatabase;
208-
if (arg.persist) {
209-
fDatabase.getInstance().setPersistenceEnabled(true);
208+
const fDatabase = com.google.firebase.database.FirebaseDatabase;
209+
if (arg.persist) {
210+
fDatabase.getInstance().setPersistenceEnabled(true);
211+
}
212+
firebase.instance = fDatabase.getInstance().getReference();
213+
} else {
214+
// Firestore has offline persistence enabled by default
215+
if (!arg.persist) {
216+
com.google.firebase.firestore.FirebaseFirestore.getInstance().setFirestoreSettings(
217+
new com.google.firebase.firestore.FirebaseFirestoreSettings.Builder()
218+
.setPersistenceEnabled(false)
219+
.build());
220+
}
210221
}
211-
// the URL is picked up from google-services.json, so you can use it like this:
212-
firebase.instance = fDatabase.getInstance().getReference();
213-
// it is however still possible to pass the URL programmatically (which we'll do for now):
214-
// firebase.instance = fDatabase.getInstance().getReferenceFromUrl(arg.url);
215222

216223
const firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
217224

src/firebase.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export enum ServerValue {
7575
*/
7676
export interface InitOptions {
7777
/**
78-
* Allow disk persistence. Default false.
78+
* Allow disk persistence. Default true for Firestore, false for regular Firebase DB.
7979
*/
8080
persist?: boolean;
8181
/**

src/firebase.ios.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,19 @@ firebase.init = arg => {
632632

633633
firebase._configure();
634634

635-
// TODO something for Firestore?
636635
if (typeof(FIRDatabase) !== "undefined") {
637636
if (arg.persist) {
638637
FIRDatabase.database().persistenceEnabled = true;
639638
}
640639

641640
firebase.instance = FIRDatabase.database().reference();
641+
} else if (typeof(FIRStorage) !== "undefined") {
642+
// Firestore has offline persistence enabled by default
643+
if (arg.persist === false) {
644+
const fIRFirestoreSettings = FIRFirestoreSettings.new();
645+
fIRFirestoreSettings.persistenceEnabled = false;
646+
FIRFirestore.firestore().settings = fIRFirestoreSettings;
647+
}
642648
}
643649

644650
if (arg.iOSEmulatorFlush) {

0 commit comments

Comments
 (0)