Skip to content

Commit

Permalink
feat(android): make AppRestoredResult also returns error info and suc…
Browse files Browse the repository at this point in the history
…cess boolean (#2497)
  • Loading branch information
jcesarmobile authored Feb 27, 2020
1 parent d120021 commit b650880
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ private JSInjector getJSInjector() {
protected void storeDanglingPluginResult(PluginCall call, PluginResult result) {
PluginHandle appHandle = getPlugin("App");
App appPlugin = (App) appHandle.getInstance();
appPlugin.fireRestoredResult(result.getWrappedResult(call));
appPlugin.fireRestoredResult(result);
}

/**
Expand Down
30 changes: 10 additions & 20 deletions android/capacitor/src/main/java/com/getcapacitor/PluginResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,18 @@ public String toString() {
return this.json.toString();
}

public JSObject getData() {
try {
return this.json.getJSObject("data", new JSObject());
} catch (JSONException ex) {
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
* Return plugin metadata and information about the result, if it succeeded the data, or error information if it didn't.
* This is used for appRestoredResult, as it's technically a raw data response from a plugin.
* @return the raw data response from the plugin.
*/
public PluginResult getWrappedResult(PluginCall call) {
public JSObject getWrappedResult() {
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);
ret.put("pluginId", this.json.getString("pluginId"));
ret.put("methodName", this.json.getString("methodName"));
ret.put("success", this.json.getBoolean("success", false));
ret.put("data", this.json.getJSObject("data"));
ret.put("error", this.json.getJSObject("error"));
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void fireChange(boolean isActive) {

public void fireRestoredResult(PluginResult result) {
Log.d(getLogTag(), "Firing restored result");
notifyListeners(EVENT_RESTORED_RESULT, result.getData(), true);
notifyListeners(EVENT_RESTORED_RESULT, result.getWrappedResult(), true);
}

public void fireBackButton() {
Expand Down
12 changes: 11 additions & 1 deletion core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,17 @@ export interface AppRestoredResult {
* The result data passed from the plugin. This would be the result you'd
* expect from normally calling the plugin method. For example, `CameraPhoto`
*/
data: any;
data?: any;
/**
* Boolean indicating if the plugin call succeeded
*/
success: boolean;
/**
* If the plugin call didn't succeed, it will contain the error message
*/
error?: {
message: string;
}
}

//
Expand Down

0 comments on commit b650880

Please sign in to comment.