Skip to content

Commit 8394be0

Browse files
committed
Added enablePersistence methods in Database and databaseRef
1 parent ac60127 commit 8394be0

File tree

5 files changed

+170
-35
lines changed

5 files changed

+170
-35
lines changed

android/src/main/java/io/fullstack/firestack/FirestackDatabase.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,34 @@ public String getName() {
265265
return TAG;
266266
}
267267

268+
// Persistence
269+
@ReactMethod
270+
public void enablePersistence(
271+
final Boolean enable,
272+
final Callback callback) {
273+
FirebaseDatabase.getInstance()
274+
.setPersistenceEnabled(enable);
275+
276+
WritableMap res = Arguments.createMap();
277+
res.putString("status", "success");
278+
callback.invoke(null, res);
279+
}
280+
281+
@ReactMethod
282+
public void keepSynced(
283+
final String path,
284+
final Boolean enable,
285+
final Callback callback) {
286+
DatabaseReference ref = this.getDatabaseReferenceAtPath(path);
287+
ref.keepSynced(enable);
288+
289+
WritableMap res = Arguments.createMap();
290+
res.putString("status", "success");
291+
res.putString("path", path);
292+
callback.invoke(null, res);
293+
}
294+
295+
// Database
268296
@ReactMethod
269297
public void set(
270298
final String path,

android/src/main/java/io/fullstack/firestack/FirestackModule.java

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
import com.google.firebase.FirebaseApp;
2323
import com.google.firebase.FirebaseOptions;
2424

25+
interface KeySetterFn {
26+
String setKeyOrDefault(String a, String b);
27+
}
28+
2529
class FirestackModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
2630
private static final String TAG = "Firestack";
2731
private Context context;
@@ -42,47 +46,109 @@ public String getName() {
4246
}
4347

4448
@ReactMethod
45-
public void configureWithOptions(ReadableMap params, @Nullable final Callback onComplete) {
49+
public void configureWithOptions(final ReadableMap params, @Nullable final Callback onComplete) {
4650
Log.i(TAG, "configureWithOptions");
4751

4852
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
53+
FirebaseOptions defaultOptions = FirebaseOptions.fromResource(this.context);
54+
55+
KeySetterFn fn = new KeySetterFn() {
56+
public String setKeyOrDefault(
57+
final String key,
58+
final String defaultValue) {
59+
if (params.hasKey(key)) {
60+
// User-set key
61+
final String val = params.getString(key);
62+
Log.d(TAG, "Setting " + key + " from params to: " + val);
63+
return val;
64+
} else if (defaultValue != null && defaultValue != "") {
65+
Log.d(TAG, "Setting " + key + " from params to: " + defaultValue);
66+
return defaultValue;
67+
} else {
68+
return null;
69+
}
70+
}
71+
};
4972

50-
if (params.hasKey("applicationId")) {
51-
final String applicationId = params.getString("applicationId");
52-
Log.d(TAG, "Setting applicationId from params " + applicationId);
53-
builder.setApplicationId(applicationId);
73+
String val = fn.setKeyOrDefault("applicationId",
74+
defaultOptions.getApplicationId());
75+
if (val != null) {
76+
builder.setApplicationId(val);
5477
}
55-
if (params.hasKey("apiKey")) {
56-
final String apiKey = params.getString("apiKey");
57-
Log.d(TAG, "Setting API key from params " + apiKey);
58-
builder.setApiKey(apiKey);
78+
79+
val = fn.setKeyOrDefault("apiKey",
80+
defaultOptions.getApiKey());
81+
if (val != null) {
82+
builder.setApiKey(val);
5983
}
60-
if (params.hasKey("APIKey")) {
61-
final String apiKey = params.getString("APIKey");
62-
Log.d(TAG, "Setting API key from params " + apiKey);
63-
builder.setApiKey(apiKey);
84+
85+
val = fn.setKeyOrDefault("gcmSenderID",
86+
defaultOptions.getGcmSenderId());
87+
if (val != null) {
88+
builder.setGcmSenderId(val);
6489
}
65-
if (params.hasKey("gcmSenderID")) {
66-
final String gcmSenderID = params.getString("gcmSenderID");
67-
Log.d(TAG, "Setting gcmSenderID from params " + gcmSenderID );
68-
builder.setGcmSenderId(gcmSenderID);
90+
91+
val = fn.setKeyOrDefault("storageBucket",
92+
defaultOptions.getStorageBucket());
93+
if (val != null) {
94+
builder.setStorageBucket(val);
6995
}
70-
if (params.hasKey("storageBucket")) {
71-
final String storageBucket = params.getString("storageBucket");
72-
Log.d(TAG, "Setting storageBucket from params " + storageBucket);
73-
builder.setStorageBucket(storageBucket);
96+
97+
val = fn.setKeyOrDefault("databaseURL",
98+
defaultOptions.getDatabaseUrl());
99+
if (val != null) {
100+
builder.setDatabaseUrl(val);
74101
}
75-
if (params.hasKey("databaseURL")) {
76-
final String databaseURL = params.getString("databaseURL");
77-
Log.d(TAG, "Setting databaseURL from params " + databaseURL);
78-
builder.setDatabaseUrl(databaseURL);
102+
103+
val = fn.setKeyOrDefault("databaseUrl",
104+
defaultOptions.getDatabaseUrl());
105+
if (val != null) {
106+
builder.setDatabaseUrl(val);
79107
}
80-
if (params.hasKey("clientID")) {
81-
final String clientID = params.getString("clientID");
82-
Log.d(TAG, "Setting clientID from params " + clientID);
83-
builder.setApplicationId(clientID);
108+
109+
val = fn.setKeyOrDefault("clientId",
110+
defaultOptions.getApplicationId());
111+
if (val != null) {
112+
builder.setApplicationId(val);
84113
}
85114

115+
116+
// if (params.hasKey("applicationId")) {
117+
// final String applicationId = params.getString("applicationId");
118+
// Log.d(TAG, "Setting applicationId from params " + applicationId);
119+
// builder.setApplicationId(applicationId);
120+
// }
121+
// if (params.hasKey("apiKey")) {
122+
// final String apiKey = params.getString("apiKey");
123+
// Log.d(TAG, "Setting API key from params " + apiKey);
124+
// builder.setApiKey(apiKey);
125+
// }
126+
// if (params.hasKey("APIKey")) {
127+
// final String apiKey = params.getString("APIKey");
128+
// Log.d(TAG, "Setting API key from params " + apiKey);
129+
// builder.setApiKey(apiKey);
130+
// }
131+
// if (params.hasKey("gcmSenderID")) {
132+
// final String gcmSenderID = params.getString("gcmSenderID");
133+
// Log.d(TAG, "Setting gcmSenderID from params " + gcmSenderID );
134+
// builder.setGcmSenderId(gcmSenderID);
135+
// }
136+
// if (params.hasKey("storageBucket")) {
137+
// final String storageBucket = params.getString("storageBucket");
138+
// Log.d(TAG, "Setting storageBucket from params " + storageBucket);
139+
// builder.setStorageBucket(storageBucket);
140+
// }
141+
// if (params.hasKey("databaseURL")) {
142+
// final String databaseURL = params.getString("databaseURL");
143+
// Log.d(TAG, "Setting databaseURL from params " + databaseURL);
144+
// builder.setDatabaseUrl(databaseURL);
145+
// }
146+
// if (params.hasKey("clientID")) {
147+
// final String clientID = params.getString("clientID");
148+
// Log.d(TAG, "Setting clientID from params " + clientID);
149+
// builder.setApplicationId(clientID);
150+
// }
151+
86152
try {
87153
Log.i(TAG, "Configuring app");
88154
if (app == null) {

ios/Firestack/FirestackDatabase.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,27 @@ @implementation FirestackDatabase
235235

236236
RCT_EXPORT_MODULE(FirestackDatabase);
237237

238+
RCT_EXPORT_METHOD(enablePersistence:(BOOL) enable
239+
callback:(RCTResponseSenderBlock) callback)
240+
{
241+
[FIRDatabase database].persistenceEnabled = enable;
242+
callback(@[[NSNull null], @{
243+
@"result": @"success"
244+
}]);
245+
}
246+
247+
RCT_EXPORT_METHOD(keepSynced:(NSString *) path
248+
withEnable:(BOOL) enable
249+
callback:(RCTResponseSenderBlock) callback)
250+
{
251+
FIRDatabaseReference *ref = [self getRefAtPath:path];
252+
[ref keepSynced:enable];
253+
callback(@[[NSNull null], @{
254+
@"result": @"success",
255+
@"path": path
256+
}]);
257+
}
258+
238259
RCT_EXPORT_METHOD(set:(NSString *) path
239260
value:(NSDictionary *)value
240261
callback:(RCTResponseSenderBlock) callback)

lib/modules/database.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ class DatabaseRef extends ReferenceBase {
145145
return new DatabaseRef(this.db, this.path.concat(paths));
146146
}
147147

148+
keepSynced(bool) {
149+
const path = this.dbPath();
150+
return promisify('keepSynced', FirestackDatabase)(path, bool);
151+
}
152+
148153
// Get the value of a ref either with a key
149154
getAt() {
150155
const path = this.dbPath();
@@ -283,6 +288,7 @@ export class Database extends Base {
283288
super(firestack, options);
284289
this.log.debug('Created new Database instance', this.options);
285290

291+
this.persistenceEnabled = false;
286292
this.listener = null;
287293
this.refs = {};
288294
}
@@ -296,6 +302,19 @@ export class Database extends Base {
296302
return this.refs[key];
297303
}
298304

305+
setPersistence(enable=true) {
306+
this.persistenceEnabled = enable;
307+
let promise;
308+
if (this.persistenceEnabled !== enable) {
309+
this.log.debug(`${enable ? 'Enabling' : 'Disabling'} persistence`);
310+
promise = promisify('enablePersistence', FirestackDatabase)(enable);
311+
} else {
312+
promise = Promise.resolve({status: "Already enabled"})
313+
}
314+
315+
return promise;
316+
}
317+
299318
handleDatabaseEvent(evt) {
300319
const body = evt.body;
301320
const path = body.path;

lib/utils/log.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// document hack
22
import root from './window-or-global'
3-
(function (global) {
4-
if (!global.document) {
5-
global.document = { documentElement: { style: { WebkitAppearance: true } } }
3+
(function (base) {
4+
window = base || window
5+
if (!window.document) {
6+
window.document = { documentElement: { style: { WebkitAppearance: true } } }
67
}
7-
if(!global.localStorage) global.localStorage = {};
8+
if(!window.localStorage) window.localStorage = {};
89
})(root);
910

1011
let debug = () => {};
@@ -19,12 +20,12 @@ export class Log {
1920
}
2021

2122
enable(booleanOrStringDebug) {
22-
root.localStorage.debug =
23+
window.localStorage.debug =
2324
typeof booleanOrStringDebug === 'string' ?
2425
booleanOrStringDebug :
2526
(booleanOrStringDebug ? '*' : booleanOrStringDebug);
2627

27-
this.enabled = !!root.localStorage.debug;
28+
this.enabled = !!window.localStorage.debug;
2829
}
2930

3031
info(...args) {

0 commit comments

Comments
 (0)