Skip to content

Commit

Permalink
Android: Property resume and send restored data. Closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed Jan 15, 2018
1 parent 89c3f47 commit 048e894
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ private JSInjector getJSInjector() {
return null;
}

protected void storeDanglingPluginResult(PluginResult result) {
protected void storeDanglingPluginResult(PluginCall call, PluginResult result) {
PluginHandle appHandle = getPlugin("App");
App appPlugin = (App) appHandle.getInstance();
appPlugin.fireRestoredResult(result);
appPlugin.fireRestoredResult(result.getWrappedResult(call));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ private void callPluginMethod(String callbackId, String pluginId, String methodN
bridge.callPluginMethod(pluginId, methodName, call);
}

public void sendResponseMessage(String callbackId, String pluginId, String methodName, PluginResult successResult, PluginResult errorResult) {
public void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult) {
try {
PluginResult data = new PluginResult();
data.put("callbackId", callbackId);
data.put("pluginId", pluginId);
data.put("methodName", methodName);
data.put("callbackId", call.getCallbackId());
data.put("pluginId", call.getPluginId());
data.put("methodName", call.getMethodName());

if (errorResult != null) {
data.put("success", false);
Expand All @@ -68,7 +68,7 @@ public void sendResponseMessage(String callbackId, String pluginId, String metho
}

// Only eval the JS code if this is a valid callback id
if (!callbackId.equals(PluginCall.CALLBACK_ID_DANGLING)) {
if (!call.getCallbackId().equals(PluginCall.CALLBACK_ID_DANGLING)) {
final String runScript = "window.Capacitor.fromNative(" + data.toString() + ")";

final WebView webView = this.webView;
Expand All @@ -79,7 +79,7 @@ public void run() {
}
});
} else {
bridge.storeDanglingPluginResult(data);
bridge.storeDanglingPluginResult(call, data);
}

} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public void successCallback(PluginResult successResult) {
return;
}

this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, successResult, null);
this.msgHandler.sendResponseMessage(this, successResult, null);
}


public void success(JSObject data) {
PluginResult result = new PluginResult(data);
this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, result, null);
this.msgHandler.sendResponseMessage(this, result, null);
}

public void success() {
Expand All @@ -69,7 +69,7 @@ public void errorCallback(String msg) {
Log.e(Bridge.TAG, jsonEx.toString());
}

this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, null, errorResult);
this.msgHandler.sendResponseMessage(this, null, errorResult);
}

public void error(String msg, Exception ex) {
Expand All @@ -85,7 +85,7 @@ public void error(String msg, Exception ex) {
Log.e(Bridge.TAG, jsonEx.toString());
}

this.msgHandler.sendResponseMessage(this.callbackId, this.pluginId, this.methodName, null, errorResult);
this.msgHandler.sendResponseMessage(this, null, errorResult);
}

public void error(String msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,21 @@ public JSObject getData() {
return null;
}
}

/**
* Return a new data object with the actual payload data
* along side additional metadata about the plugin. This is used
* for appRestoredResult, as it's technically a raw data response
* from a plugin, but with metadata about the plugin.
* @return
*/
public PluginResult getWrappedResult(PluginCall call) {
JSObject ret = new JSObject();
JSObject data = new JSObject();
data.put("pluginId", call.getPluginId());
data.put("methodName", call.getMethodName());
data.put("data", getData());
ret.put("data", data);
return new PluginResult(ret);
}
}
2 changes: 1 addition & 1 deletion example/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class MyApp {

Plugins.App.addListener('appRestoredResult', (err: any, data: any) => {
alert('Got restored result');
console.log(data);
console.log('Restored result:', data);
});

this.getLaunchUrl();
Expand Down

0 comments on commit 048e894

Please sign in to comment.