mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-27 11:56:05 +02:00
feat: Add deep link plugin for mobile (#504)
Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> Co-authored-by: Lucas Nogueira <lucas@tauri.app> Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package app.tauri.deep_link
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.webkit.WebView
|
||||
import app.tauri.Logger
|
||||
import app.tauri.annotation.Command
|
||||
import app.tauri.annotation.TauriPlugin
|
||||
import app.tauri.plugin.Channel
|
||||
import app.tauri.plugin.JSObject
|
||||
import app.tauri.plugin.Plugin
|
||||
import app.tauri.plugin.Invoke
|
||||
|
||||
@TauriPlugin
|
||||
class DeepLinkPlugin(private val activity: Activity): Plugin(activity) {
|
||||
//private val implementation = Example()
|
||||
private var webView: WebView? = null
|
||||
private var currentUrl: String? = null
|
||||
private var channel: Channel? = null
|
||||
|
||||
companion object {
|
||||
var instance: DeepLinkPlugin? = null
|
||||
}
|
||||
|
||||
@Command
|
||||
fun getCurrent(invoke: Invoke) {
|
||||
val ret = JSObject()
|
||||
ret.put("url", this.currentUrl)
|
||||
invoke.resolve(ret)
|
||||
}
|
||||
|
||||
/* @Command
|
||||
fun registerListenerRust(invoke: Invoke) {
|
||||
val value = invoke.getString("value") ?: ""
|
||||
val ret = JSObject()
|
||||
ret.put("value", this.currentUrl ?: "none")
|
||||
invoke.resolve(ret)
|
||||
} */
|
||||
|
||||
@Command
|
||||
fun setEventHandler(invoke: Invoke) {
|
||||
val channel = invoke.getChannel("handler")
|
||||
|
||||
if (channel == null) {
|
||||
invoke.reject("`handler` not provided")
|
||||
}
|
||||
this.channel = channel
|
||||
invoke.resolve()
|
||||
}
|
||||
|
||||
override fun load(webView: WebView) {
|
||||
instance = this
|
||||
|
||||
var intent = activity.intent
|
||||
|
||||
if (intent.action == Intent.ACTION_VIEW) {
|
||||
// TODO: check if it makes sense to split up init url and last url
|
||||
this.currentUrl = intent.data.toString()
|
||||
val event = JSObject()
|
||||
event.put("url", this.currentUrl)
|
||||
this.channel?.send(event)
|
||||
}
|
||||
|
||||
super.load(webView)
|
||||
this.webView = webView
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
if (intent.action == Intent.ACTION_VIEW) {
|
||||
this.currentUrl = intent.data.toString()
|
||||
val event = JSObject()
|
||||
event.put("url", this.currentUrl)
|
||||
this.channel?.send(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user