Skip to content

Commit dfa407f

Browse files
authored
feat(mobile): add plugin config to the Plugin class (#6763)
1 parent 41f49ae commit dfa407f

File tree

16 files changed

+84
-37
lines changed

16 files changed

+84
-37
lines changed

.changes/mobile-plugin-config.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Expose plugin configuration on the Android and iOS plugin classes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Change iOS plugin init function signature to `func init_plugin() -> Plugin`.

core/tauri/mobile/android/src/main/java/app/tauri/plugin/Plugin.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ abstract class Plugin(private val activity: Activity) {
2626

2727
open fun load(webView: WebView) {}
2828

29+
fun getConfig(): JSObject {
30+
return handle!!.config
31+
}
32+
2933
/**
3034
* Start activity for result with the provided Intent and resolve calling the provided callback method name.
3135
*
@@ -74,7 +78,7 @@ abstract class Plugin(private val activity: Activity) {
7478
*/
7579
@Command
7680
@PermissionCallback
77-
fun checkPermissions(invoke: Invoke) {
81+
open fun checkPermissions(invoke: Invoke) {
7882
val permissionsResult: Map<String, PermissionState?> = getPermissionStates()
7983
if (permissionsResult.isEmpty()) {
8084
// if no permissions are defined on the plugin, resolve undefined

core/tauri/mobile/android/src/main/java/app/tauri/plugin/PluginHandle.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import app.tauri.annotation.Command
1717
import app.tauri.annotation.TauriPlugin
1818
import java.lang.reflect.Method
1919

20-
class PluginHandle(private val manager: PluginManager, val name: String, private val instance: Plugin) {
20+
class PluginHandle(private val manager: PluginManager, val name: String, private val instance: Plugin, val config: JSObject) {
2121
private val commands: HashMap<String, CommandData> = HashMap()
2222
private val permissionCallbackMethods: HashMap<String, Method> = HashMap()
2323
private val startActivityCallbackMethods: HashMap<String, Method> = HashMap()

core/tauri/mobile/android/src/main/java/app/tauri/plugin/PluginManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class PluginManager(val activity: AppCompatActivity) {
6969
}
7070

7171
@JniMethod
72-
fun load(webView: WebView?, name: String, plugin: Plugin) {
73-
val handle = PluginHandle(this, name, plugin)
72+
fun load(webView: WebView?, name: String, plugin: Plugin, config: JSObject) {
73+
val handle = PluginHandle(this, name, plugin, config)
7474
plugins[name] = handle
7575
if (webView != null) {
7676
plugin.load(webView)

core/tauri/mobile/ios-api/Sources/Tauri/Plugin/Plugin.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import os.log
77

88
open class Plugin: NSObject {
99
public let manager: PluginManager = PluginManager.shared
10+
public var config: JSObject = [:]
11+
12+
internal func setConfig(_ config: JSObject) {
13+
self.config = config
14+
}
1015

1116
@objc open func load(webview: WKWebView) {}
1217

core/tauri/mobile/ios-api/Sources/Tauri/Tauri.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public class PluginManager {
4646
}
4747
}
4848

49-
func load<P: Plugin>(webview: WKWebView?, name: String, plugin: P) {
49+
func load<P: Plugin>(name: String, plugin: P, config: JSObject, webview: WKWebView?) {
50+
plugin.setConfig(config)
5051
let handle = PluginHandle(plugin: plugin)
5152
if let webview = webview {
5253
handle.instance.load(webview: webview)
@@ -91,11 +92,13 @@ extension PluginManager: NSCopying {
9192
}
9293
}
9394

94-
public func registerPlugin<P: Plugin>(webview: WKWebView?, name: String, plugin: P) {
95+
@_cdecl("register_plugin")
96+
func registerPlugin(name: SRString, plugin: NSObject, config: NSDictionary, webview: WKWebView?) {
9597
PluginManager.shared.load(
96-
webview: webview,
97-
name: name,
98-
plugin: plugin
98+
name: name.toString(),
99+
plugin: plugin as! Plugin,
100+
config: JSTypes.coerceDictionaryToJSObject(config, formattingDatesAsStrings: true)!,
101+
webview: webview
99102
)
100103
}
101104

core/tauri/src/ios.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ swift!(pub fn run_plugin_method(
3838
data: *const c_void,
3939
callback: PluginMessageCallback
4040
));
41+
swift!(pub fn register_plugin(
42+
name: &SRString,
43+
plugin: *const c_void,
44+
config: *const c_void,
45+
webview: *const c_void
46+
));
4147
swift!(pub fn on_webview_created(webview: *const c_void, controller: *const c_void));
4248

43-
pub fn json_to_dictionary(json: JsonValue) -> id {
49+
pub fn json_to_dictionary(json: &JsonValue) -> id {
4450
if let serde_json::Value::Object(map) = json {
4551
unsafe {
4652
let dictionary: id = msg_send![class!(NSMutableDictionary), alloc];
@@ -78,14 +84,14 @@ impl NSString {
7884
}
7985
}
8086

81-
unsafe fn add_json_value_to_array(array: id, value: JsonValue) {
87+
unsafe fn add_json_value_to_array(array: id, value: &JsonValue) {
8288
match value {
8389
JsonValue::Null => {
8490
let null: id = msg_send![class!(NSNull), null];
8591
let () = msg_send![array, addObject: null];
8692
}
8793
JsonValue::Bool(val) => {
88-
let value = if val { YES } else { NO };
94+
let value = if *val { YES } else { NO };
8995
let v: id = msg_send![class!(NSNumber), numberWithBool: value];
9096
let () = msg_send![array, addObject: v];
9197
}
@@ -123,15 +129,15 @@ unsafe fn add_json_value_to_array(array: id, value: JsonValue) {
123129
}
124130
}
125131

126-
unsafe fn add_json_entry_to_dictionary(data: id, key: String, value: JsonValue) {
132+
unsafe fn add_json_entry_to_dictionary(data: id, key: &str, value: &JsonValue) {
127133
let key = NSString::new(&key);
128134
match value {
129135
JsonValue::Null => {
130136
let null: id = msg_send![class!(NSNull), null];
131137
let () = msg_send![data, setObject:null forKey: key];
132138
}
133139
JsonValue::Bool(val) => {
134-
let flag = if val { YES } else { NO };
140+
let flag = if *val { YES } else { NO };
135141
let value: id = msg_send![class!(NSNumber), numberWithBool: flag];
136142
let () = msg_send![data, setObject:value forKey: key];
137143
}

core/tauri/src/jni_helpers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ fn json_to_java<'a, R: Runtime>(
1515
env: JNIEnv<'a>,
1616
activity: JObject<'a>,
1717
runtime_handle: &R::Handle,
18-
json: JsonValue,
18+
json: &JsonValue,
1919
) -> Result<(&'static str, JValue<'a>), JniError> {
2020
let (class, v) = match json {
2121
JsonValue::Null => ("Ljava/lang/Object;", JObject::null().into()),
22-
JsonValue::Bool(val) => ("Z", val.into()),
22+
JsonValue::Bool(val) => ("Z", (*val).into()),
2323
JsonValue::Number(val) => {
2424
if let Some(v) = val.as_i64() {
2525
("J", v.into())
@@ -74,9 +74,9 @@ pub fn to_jsobject<'a, R: Runtime>(
7474
env: JNIEnv<'a>,
7575
activity: JObject<'a>,
7676
runtime_handle: &R::Handle,
77-
json: JsonValue,
77+
json: &JsonValue,
7878
) -> Result<JValue<'a>, JniError> {
79-
if let JsonValue::Object(_) = &json {
79+
if let JsonValue::Object(_) = json {
8080
json_to_java::<R>(env, activity, runtime_handle, json).map(|(_class, data)| data)
8181
} else {
8282
// currently the Kotlin lib cannot handle nulls or raw values, it must be an object

core/tauri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
#[macro_export]
158158
macro_rules! ios_plugin_binding {
159159
($fn_name: ident) => {
160-
tauri::swift_rs::swift!(fn $fn_name(name: &::tauri::swift_rs::SRString, webview: *const ::std::ffi::c_void));
160+
tauri::swift_rs::swift!(fn $fn_name() -> *const ::std::ffi::c_void);
161161
}
162162
}
163163
#[cfg(target_os = "ios")]

0 commit comments

Comments
 (0)