Skip to content

Commit

Permalink
changed callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ga committed Oct 9, 2017
1 parent a9c31a0 commit afc276f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.0.7
version: 2.0.8
apiversion: 3
architectures: armeabi-v7a x86
description: ti.goosh
Expand Down
23 changes: 14 additions & 9 deletions android/src/ti/goosh/TiGooshModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public class TiGooshModule extends KrollModule {
private KrollFunction messageCallback = null;

/* Callbacks for topics */
private KrollFunction successUnsubTopicCallback = null;
private KrollFunction successTopicCallback = null;
private KrollFunction errorSubTopicCallback = null;
private KrollFunction errorTopicCallback = null;
private KrollFunction topicCallback = null;

Expand Down Expand Up @@ -258,9 +260,6 @@ public void subscribe(final HashMap options) {
if (options.get("error") != null) {
errorTopicCallback = (KrollFunction) options.get("error");
}
if (options.get("callback") != null) {
topicCallback = (KrollFunction) options.get("callback");
}

if (topic == null || !topic.startsWith("/topics/")) {
Log.e(LCAT, "No or invalid topic specified, should start with /topics/");
Expand Down Expand Up @@ -300,7 +299,8 @@ protected Void doInBackground(Void... params) {
@Kroll.method
public void unsubscribe(final HashMap options) {
final String topic = (String) options.get("topic");
final KrollFunction callback = (KrollFunction) options.get("callback");
successUnsubTopicCallback = (KrollFunction) options.get("success");
errorSubTopicCallback = (KrollFunction) options.get("error");

if (topic == null || !topic.startsWith("/topics/")) {
Log.e(LCAT, "No or invalid topic specified, should start with /topics/");
Expand All @@ -314,19 +314,24 @@ protected Void doInBackground(Void... params) {
String token = getRemoteDeviceUUID();
if (token != null) {
GcmPubSub.getInstance(TiApplication.getInstance()).unsubscribe(token, topic);
if (callback != null) {
// send success callback
if (successUnsubTopicCallback != null) {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("success", true);
data.put("topic", topic);
data.put("token", token);
callback.callAsync(getKrollObject(), data);
successUnsubTopicCallback.callAsync(getKrollObject(), data);
}
} else {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("error", true);
data.put("topic", topic);
errorSubTopicCallback.callAsync(getKrollObject(), data);
Log.e(LCAT, "Cannot unsubscribe from topic " + topic);
}
} catch (Exception ex) {
sendError(ex);
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("error", ex.toString());
data.put("topic", topic);
errorSubTopicCallback.callAsync(getKrollObject(), data);
}
return null;
}
Expand Down

0 comments on commit afc276f

Please sign in to comment.