From b650880df3cf92e112799b4ae4cfdf7f6b342132 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 27 Feb 2020 20:15:20 +0100 Subject: [PATCH] feat(android): make AppRestoredResult also returns error info and success boolean (#2497) --- .../main/java/com/getcapacitor/Bridge.java | 2 +- .../java/com/getcapacitor/PluginResult.java | 30 +++++++------------ .../java/com/getcapacitor/plugin/App.java | 2 +- core/src/core-plugin-definitions.ts | 12 +++++++- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 72ecfcbc6..7368de89a 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -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); } /** diff --git a/android/capacitor/src/main/java/com/getcapacitor/PluginResult.java b/android/capacitor/src/main/java/com/getcapacitor/PluginResult.java index b50b0f594..f5060c384 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/PluginResult.java +++ b/android/capacitor/src/main/java/com/getcapacitor/PluginResult.java @@ -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; } } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java index ce505374e..5135d16d6 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/App.java @@ -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() { diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 6ae95be5e..24007f9f5 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -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; + } } //