Skip to content

Commit 226bce3

Browse files
committed
Merge branch 'master' of github.com:fullstackreact/react-native-firestack
* 'master' of github.com:fullstackreact/react-native-firestack: Fixed provider google on Android (was missing) Implement android ServerValue.TIMESTAMP Cast firebase long's to doubles Update README.md Update Android status Allow removal of specific callbacks from query listeners
2 parents e7a9c8f + c5092b9 commit 226bce3

File tree

6 files changed

+69
-38
lines changed

6 files changed

+69
-38
lines changed

README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -592,23 +592,33 @@ storageRef.downloadUrl()
592592

593593
### Realtime Database
594594

595-
#### database attribute
596-
597595
The native Firebase JavaScript library provides a featureful realtime database that works out of the box. Firestack provides an attribute to interact with the database without needing to configure the JS library.
598596

597+
#### DatabaseRef
598+
599+
Firestack attempts to provide the same API as the JS Firebase library for both Android and iOS platforms. [Check out the firebase guide](https://firebase.google.com/docs/database/web/read-and-write) for more information on how to use the JS library.
600+
601+
#### Example
602+
599603
```javascript
600-
firestack.database
601-
.ref(LIST_KEY)
602-
.on('value', snapshot => {
603-
if (snapshot.val()) {
604-
console.log('The list was updated');
605-
}
606-
});
607-
```
608604

609-
#### DatabaseRef
605+
function handleValueChange(snapshot) {
606+
if (snapshot.val()) {
607+
console.log('The list was updated');
608+
}
609+
}
610+
611+
const LIST_KEY = 'path/to/data';
612+
firestack.database.ref(LIST_KEY).on('value', handleValueChange);
610613

611-
Firestack attempts to provide the same API as the JS Firebase library for both Android and iOS platforms.
614+
// Calling `.off` with a reference to the callback function will only remove that specific listener.
615+
// This is useful if multiple components are listening and unlistening to the same ref path.
616+
firestack.database.ref(LIST_KEY).off('value', handleValueChange);
617+
618+
// Calling `.off` without passing the callback function will remove *all* 'value' listeners for that ref
619+
firestack.database.ref(LIST_KEY).off('value');
620+
621+
```
612622

613623
// TODO: Finish documenting
614624

@@ -774,8 +784,8 @@ The following is left to be done:
774784

775785
- [x] Complete FirebaseModule functionality
776786
- [ ] Document FirebaseModule
777-
- [ ] Add Android support
778-
- in progress
787+
- [X] Add Android support
788+
- auth/analytics/database/storage/presence are feature-complete. remoteconfig/messaging are mostly-there.
779789
- [x] Add Cloud Messaging
780790
- [ ] Add JS api
781791
- [ ] Move to use swift (cleaner syntax)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public void onComplete(@NonNull Task<AuthResult> task) {
138138
public void signInWithProvider(final String provider, final String authToken, final String authSecret, final Callback callback) {
139139
if (provider.equals("facebook")) {
140140
this.facebookLogin(authToken,callback);
141+
} else if (provider.equals("google")) {
142+
this.googleLogin(authToken,callback);
141143
} else
142144
// TODO
143145
FirestackUtils.todoNote(TAG, "signInWithProvider", callback);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,8 @@ private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
682682
case "java.lang.Boolean":
683683
data.putBoolean(child.getKey(), (Boolean) castedChild);
684684
break;
685-
case "java.lang.Integer":
686-
data.putInt(child.getKey(), (Integer) castedChild);
685+
case "java.lang.Long":
686+
data.putDouble(child.getKey(), (Long) castedChild);
687687
break;
688688
case "java.lang.Double":
689689
data.putDouble(child.getKey(), (Double) castedChild);
@@ -704,7 +704,7 @@ private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
704704
case "java.lang.Boolean":
705705
return (Any)((Boolean) snapshot.getValue());
706706
case "java.lang.Long":
707-
return (Any)((Integer)(((Long) snapshot.getValue()).intValue()));
707+
return (Any) ((Long) snapshot.getValue());
708708
case "java.lang.Double":
709709
return (Any)((Double) snapshot.getValue());
710710
case "java.lang.String":

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ public String setKeyOrDefault(
178178

179179
@ReactMethod
180180
public void serverValue(@Nullable final Callback onComplete) {
181+
WritableMap timestampMap = Arguments.createMap();
182+
for (Map.Entry<String, String> entry : ServerValue.TIMESTAMP.entrySet()) {
183+
timestampMap.putString(entry.getKey(), entry.getValue());
184+
}
185+
181186
WritableMap map = Arguments.createMap();
182-
// TODO
183-
map.putString("TIMESTAMP", "ServerValue.TIMESTAMP");
187+
map.putMap("TIMESTAMP", timestampMap);
184188
onComplete.invoke(null, map);
185189
}
186190

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,27 @@ public static WritableMap dataSnapshotToMap(String name,
5858
data.putBoolean("hasChildren", dataSnapshot.hasChildren());
5959

6060
data.putDouble("childrenCount", dataSnapshot.getChildrenCount());
61-
if (!dataSnapshot.hasChildren() && dataSnapshot.getValue() != null) {
62-
String type = dataSnapshot.getValue().getClass().getName();
61+
if (!dataSnapshot.hasChildren()) {
62+
Object value = dataSnapshot.getValue();
63+
String type = value!=null ? value.getClass().getName() : "";
6364
switch (type) {
6465
case "java.lang.Boolean":
65-
data.putBoolean("value", (Boolean) dataSnapshot.getValue());
66+
data.putBoolean("value", (Boolean)value);
6667
break;
6768
case "java.lang.Long":
68-
data.putInt("value",(Integer)(((Long) dataSnapshot.getValue()).intValue()));
69+
Long longVal = (Long) value;
70+
data.putDouble("value", (double)longVal);
6971
break;
7072
case "java.lang.Double":
71-
data.putDouble("value",(Double) dataSnapshot.getValue());
73+
data.putDouble("value", (Double) value);
7274
break;
7375
case "java.lang.String":
74-
data.putString("value",(String) dataSnapshot.getValue());
76+
data.putString("value",(String) value);
7577
break;
7678
default:
7779
data.putString("value", null);
7880
}
79-
}else{
81+
} else{
8082
WritableMap valueMap = FirestackUtils.castSnapshotValue(dataSnapshot);
8183
data.putMap("value", valueMap);
8284
}
@@ -104,8 +106,9 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
104106
case "java.lang.Boolean":
105107
data.putBoolean(child.getKey(), (Boolean) castedChild);
106108
break;
107-
case "java.lang.Integer":
108-
data.putInt(child.getKey(), (Integer) castedChild);
109+
case "java.lang.Long":
110+
Long longVal = (Long) castedChild;
111+
data.putDouble(child.getKey(), (double)longVal);
109112
break;
110113
case "java.lang.Double":
111114
data.putDouble(child.getKey(), (Double) castedChild);
@@ -116,6 +119,9 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
116119
case "com.facebook.react.bridge.WritableNativeMap":
117120
data.putMap(child.getKey(), (WritableMap) castedChild);
118121
break;
122+
default:
123+
Log.w(TAG, "Invalid type: "+type);
124+
break;
119125
}
120126
}
121127
return (Any) data;
@@ -124,19 +130,19 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
124130
String type = snapshot.getValue().getClass().getName();
125131
switch (type) {
126132
case "java.lang.Boolean":
127-
return (Any)((Boolean) snapshot.getValue());
133+
return (Any)(snapshot.getValue());
128134
case "java.lang.Long":
129-
return (Any)((Integer)(((Long) snapshot.getValue()).intValue()));
135+
return (Any)(snapshot.getValue());
130136
case "java.lang.Double":
131-
return (Any)((Double) snapshot.getValue());
137+
return (Any)(snapshot.getValue());
132138
case "java.lang.String":
133-
return (Any)((String) snapshot.getValue());
139+
return (Any)(snapshot.getValue());
134140
default:
141+
Log.w(TAG, "Invalid type: "+type);
135142
return (Any) null;
136143
}
137-
} else {
138-
return (Any) null;
139144
}
145+
return (Any) null;
140146
}
141147
}
142148

lib/modules/database.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,14 @@ class DatabaseRef extends ReferenceBase {
211211
})
212212
}
213213

214-
off(evt='') {
214+
off(evt='', origCB) {
215215
const path = this.dbPath();
216-
return this.db.off(path, evt)
216+
return this.db.off(path, evt, origCB)
217217
.then(({callback, subscriptions}) => {
218+
if (dbSubscriptions[path][evt].length > 0) {
219+
return subscriptions;
220+
}
221+
218222
return promisify('off', FirestackDatabase)(path, evt)
219223
.then(() => {
220224
// subscriptions.forEach(sub => sub.remove());
@@ -432,15 +436,20 @@ export class Database extends Base {
432436
return Promise.resolve({callback, subscriptions});
433437
}
434438

435-
off(path, evt) {
439+
off(path, evt, origCB) {
436440
const key = this._pathKey(path);
437441
// Remove subscription
438442
if (dbSubscriptions[key]) {
439443
if (!evt || evt === "") {
440444
dbSubscriptions[key] = {};
441445
} else if (dbSubscriptions[key][evt]) {
442-
delete dbSubscriptions[key][evt];
446+
if (origCB) {
447+
dbSubscriptions[key][evt].splice(dbSubscriptions[key][evt].indexOf(origCB), 1);
448+
} else {
449+
delete dbSubscriptions[key][evt];
450+
}
443451
}
452+
444453
if (Object.keys(dbSubscriptions[key]).length <= 0) {
445454
// there are no more subscriptions
446455
// so we can unwatch

0 commit comments

Comments
 (0)