Skip to content

Commit f1bea9a

Browse files
committed
WIP: Android not triggering callbacks
1 parent 0fcbadd commit f1bea9a

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ private void initRefreshTokenHandler() {
8383
public void onReceive(Context context, Intent intent) {
8484
WritableMap params = Arguments.createMap();
8585
params.putString("token", intent.getStringExtra("token"));
86-
FirestackUtils.sendEvent(mReactContext, EVENT_NAME_TOKEN, params);
87-
86+
ReactContext ctx = getReactApplicationContext();
87+
Log.d(TAG, "initRefreshTokenHandler received event " + EVENT_NAME_TOKEN);
88+
FirestackUtils.sendEvent(ctx, EVENT_NAME_TOKEN, params);
8889
}
8990

9091
;
@@ -149,7 +150,8 @@ public void onReceive(Context context, Intent intent) {
149150
} else {
150151
params.putNull("notification");
151152
}
152-
FirestackUtils.sendEvent(mReactContext, EVENT_NAME_NOTIFICATION, params);
153+
ReactContext ctx = getReactApplicationContext();
154+
FirestackUtils.sendEvent(ctx, EVENT_NAME_NOTIFICATION, params);
153155
}
154156
}, mReceiveNotificationIntentFilter);
155157
}
@@ -197,7 +199,8 @@ public void onReceive(Context context, Intent intent) {
197199
} else {
198200
params.putNull("err");
199201
}
200-
FirestackUtils.sendEvent(mReactContext, EVENT_NAME_SEND, params);
202+
ReactContext ctx = getReactApplicationContext();
203+
FirestackUtils.sendEvent(ctx, EVENT_NAME_SEND, params);
201204
}
202205
}, mReceiveSendIntentFilter);
203206
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ public void addValueEventListener(final String name, final ReadableArray modifie
9797
mValueListener = new ValueEventListener() {
9898
@Override
9999
public void onDataChange(DataSnapshot dataSnapshot) {
100+
Log.d(TAG, "onDataChange called for " + name);
100101
self.handleDatabaseEvent("value", dataSnapshot);
101102
}
102103

103104
@Override
104105
public void onCancelled(DatabaseError error) {
106+
Log.d(TAG, "onDataChange onCancelled called");
105107
self.handleDatabaseError("value", error);
106108
}
107109
};
@@ -157,14 +159,17 @@ public void removeValueEventListener() {
157159
}
158160

159161
private void handleDatabaseEvent(final String name, final DataSnapshot dataSnapshot) {
162+
Log.d(TAG, "handleDatabaseEvent called: " + name);
160163
WritableMap data = FirestackUtils.dataSnapshotToMap(name, dataSnapshot);
161164
WritableMap evt = Arguments.createMap();
162165
evt.putString("eventName", name);
163166
evt.putMap("body", data);
167+
164168
FirestackUtils.sendEvent(mReactContext, "database_event", evt);
165169
}
166170

167171
private void handleDatabaseError(final String name, final DatabaseError error) {
172+
Log.d(TAG, "handleDatabaseError called: " + name);
168173
WritableMap err = Arguments.createMap();
169174
err.putInt("errorCode", error.getCode());
170175
err.putString("errorDetails", error.getDetails());
@@ -173,6 +178,7 @@ private void handleDatabaseError(final String name, final DatabaseError error) {
173178
WritableMap evt = Arguments.createMap();
174179
evt.putString("eventName", name);
175180
evt.putMap("body", err);
181+
176182
FirestackUtils.sendEvent(mReactContext, "database_error", evt);
177183
}
178184

@@ -540,7 +546,8 @@ private void handleCallback(
540546

541547
private FirestackDBReference getDBHandle(final String path) {
542548
if (!mDBListeners.containsKey(path)) {
543-
mDBListeners.put(path, new FirestackDBReference(mReactContext, path));
549+
ReactContext ctx = getReactApplicationContext();
550+
mDBListeners.put(path, new FirestackDBReference(ctx, path));
544551
}
545552

546553
return mDBListeners.get(path);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ public static void todoNote(final String tag, final String name, final Callback
3636
* send a JS event
3737
**/
3838
public static void sendEvent(final ReactContext context,
39-
String eventName,
40-
WritableMap params) {
39+
final String eventName,
40+
final WritableMap params) {
41+
if (context.hasActiveCatalystInstance()) {
42+
Log.d(TAG, "Sending event " + eventName);
4143
context
4244
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
4345
.emit(eventName, params);
46+
} else {
47+
Log.d(TAG, "Waiting for CatalystInstance before sending event");
48+
}
4449
}
4550

4651
// snapshot

lib/modules/presence.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ class PresenceRef extends ReferenceBase {
1212
this._onConnect = [];
1313
}
1414

15-
setOnlineFor(path) {
16-
const connectedDeviceRef = this.ref.child(path)
15+
setOnline(path) {
16+
const connectedDeviceRef = path ? this.ref.child(path) : this.ref;
1717
this._lastOnlineRef = connectedDeviceRef.child('lastOnline');
1818

19+
this.connectedDeviceRef = connectedDeviceRef;
1920
this._connectedRef.on('value', (snapshot) => {
2021
const val = snapshot.val();
2122
if (val) {
@@ -25,13 +26,7 @@ class PresenceRef extends ReferenceBase {
2526
online: true
2627
})
2728
.then(() => {
28-
connectedDeviceRef.onDisconnect()
29-
.setValue({
30-
online: false
31-
});
32-
33-
this._lastOnlineRef.onDisconnect()
34-
.setValue(this.firestack.ServerValue.TIMESTAMP)
29+
this._disconnect(true);
3530

3631
this._onConnect.forEach(fn => {
3732
if (fn && typeof fn === 'function') {
@@ -40,11 +35,32 @@ class PresenceRef extends ReferenceBase {
4035
})
4136
})
4237
}
43-
})
38+
});
39+
return this;
40+
}
41+
42+
setOffline() {
43+
this._disconnect();
44+
}
45+
46+
_disconnect(onDisconnect) {
47+
if (onDisconnect) {
48+
this.connectedDeviceRef.onDisconnect()
49+
.setValue({online: false});
50+
// set last online time
51+
this._lastOnlineRef.onDisconnect()
52+
.setValue(this.firestack.ServerValue.TIMESTAMP)
53+
} else {
54+
this.connectedDeviceRef
55+
.setValue({online: false});
56+
this._lastOnlineRef
57+
.setValue(this.firestack.ServerValue.TIMESTAMP)
58+
}
4459
}
4560

4661
onConnect(cb) {
4762
this._onConnect.push(cb);
63+
return this;
4864
}
4965

5066
}

0 commit comments

Comments
 (0)