From 2a5175a8f8f318aac9a6434271f2cc065e5989ae Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sun, 23 Apr 2023 13:06:31 -0700 Subject: [PATCH] feat(android): enhance JSObject return types (#6779) --- .changes/enhance-jsobject-return-types.md | 5 +++ .../main/java/app/tauri/plugin/JSObject.kt | 41 ++++++++++--------- 2 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 .changes/enhance-jsobject-return-types.md diff --git a/.changes/enhance-jsobject-return-types.md b/.changes/enhance-jsobject-return-types.md new file mode 100644 index 00000000000..47717614ec5 --- /dev/null +++ b/.changes/enhance-jsobject-return-types.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Enhance Android's `JSObject` return types. diff --git a/core/tauri/mobile/android/src/main/java/app/tauri/plugin/JSObject.kt b/core/tauri/mobile/android/src/main/java/app/tauri/plugin/JSObject.kt index 0a01a7db6e3..f09fefd2cc6 100644 --- a/core/tauri/mobile/android/src/main/java/app/tauri/plugin/JSObject.kt +++ b/core/tauri/mobile/android/src/main/java/app/tauri/plugin/JSObject.kt @@ -28,10 +28,14 @@ class JSObject : JSONObject { } fun getInteger(key: String): Int? { - return getInteger(key, null) + return getIntegerInternal(key, null) } - fun getInteger(key: String, defaultValue: Int?): Int? { + fun getInteger(key: String, defaultValue: Int): Int { + return getIntegerInternal(key, defaultValue)!! + } + + private fun getIntegerInternal(key: String, defaultValue: Int?): Int? { try { return super.getInt(key) } catch (_: JSONException) { @@ -39,7 +43,15 @@ class JSObject : JSONObject { return defaultValue } - fun getBoolean(key: String, defaultValue: Boolean?): Boolean? { + override fun getBoolean(key: String): Boolean { + return getBooleanInternal(key, false)!! + } + + fun getBoolean(key: String, defaultValue: Boolean?): Boolean { + return getBooleanInternal(key, defaultValue)!! + } + + private fun getBooleanInternal(key: String, defaultValue: Boolean?): Boolean? { try { return super.getBoolean(key) } catch (_: JSONException) { @@ -47,23 +59,19 @@ class JSObject : JSONObject { return defaultValue } - /** - * Fetch boolean from jsonObject - */ - fun getBool(key: String): Boolean? { - return getBoolean(key, null) - } - fun getJSObject(name: String): JSObject? { try { - return getJSObject(name, null) - } catch (e: JSONException) { + return getJSObjectInternal(name, null) + } catch (_: JSONException) { } return null } - @Throws(JSONException::class) - fun getJSObject(name: String, defaultValue: JSObject?): JSObject? { + fun getJSObject(name: String, defaultValue: JSObject): JSObject { + return getJSObjectInternal(name, defaultValue)!! + } + + private fun getJSObjectInternal(name: String, defaultValue: JSObject?): JSObject? { try { val obj = get(name) if (obj is JSONObject) { @@ -127,11 +135,6 @@ class JSObject : JSONObject { return this } - @Throws(JSONException::class) - fun putSafe(key: String, value: Any?): JSObject { - return super.put(key, value) as JSObject - } - companion object { /** * Convert a pathetic JSONObject into a JSObject