Files
tauri-plugins-workspace/plugins/nfc/src/lib.rs
T
Lucas Nogueira fe79adb5c7 feat(mobile): add NFC plugin (#830)
* feat: scaffold NFC plugin, initial iOS code

* adjust script paths (api example)

* update entitlements & plist

* update class name

* update api

* sketch api, remove desktop

* update response data

* add write fn

* remove commands

* fixes for write mode

* check nfc state before using the APIs

* fix(example): downgrade internal-ip to v7

* feat: typed iOS arguments

* update swift requirement

* android updates

* update tauri

* fix icon

* update example

* fix build

* fix notification example

* fix clipboard

* fix ios notification build

* fix info.plist

* update tauri

* add change file

* fmt

* update to new args class syntax :( [skip ci]

* add lang code handling in RTD_TEXT helper (written payload is broken without it)

* update nfc to latest tauri. use tauri from git

* update lockfile

* android: add initial nfc writer implementation

* check sdk version for pendingintent flag

* quicksaving basic ndef reading that doesn't crash

* add basic ndef reading (android)

* small cleanup

* change pending action type

* validate available state

* gradle 8.0.0

* use session class

* implement keep session alive

* fix notification crash??

* remove dox feature, fix breaking changes

* update dependencies

* fix shell tests [skip ci]

* fmt [skip ci]

* type safe args

* scan kind options

* commit .idea files

* update api

* update example

* fix app check

* alertmessage options for iOS

* default to tag on example

* fix(ios): always close session on write, remove keepsessionalive option

* add kind input to write options

* empty records if message not found

* fill tag metadata for ndef read

* add contributors

* update vite

* covector setup

* tauri/dox removed

* docs and examples

* fmt [skip ci]

---------

Co-authored-by: FabianLars-crabnebula <fabianlars@crabnebula.dev>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
2023-12-19 10:50:31 -03:00

85 lines
2.1 KiB
Rust

// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#![cfg(mobile)]
use serde::{Deserialize, Serialize};
use tauri::{
plugin::{Builder, PluginHandle, TauriPlugin},
Manager, Runtime,
};
pub use models::*;
mod error;
mod models;
pub use error::{Error, Result};
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.nfc";
#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_nfc);
/// Access to the nfc APIs.
pub struct Nfc<R: Runtime>(PluginHandle<R>);
#[derive(Deserialize)]
struct IsAvailableResponse {
available: bool,
}
#[derive(Serialize)]
struct WriteRequest {
records: Vec<NfcRecord>,
}
impl<R: Runtime> Nfc<R> {
pub fn is_available(&self) -> crate::Result<bool> {
self.0
.run_mobile_plugin::<IsAvailableResponse>("isAvailable", ())
.map(|r| r.available)
.map_err(Into::into)
}
pub fn scan(&self, payload: ScanRequest) -> crate::Result<ScanResponse> {
self.0
.run_mobile_plugin("scan", payload)
.map_err(Into::into)
}
pub fn write(&self, records: Vec<NfcRecord>) -> crate::Result<()> {
self.0
.run_mobile_plugin("write", WriteRequest { records })
.map_err(Into::into)
}
}
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the nfc APIs.
pub trait NfcExt<R: Runtime> {
fn nfc(&self) -> &Nfc<R>;
}
impl<R: Runtime, T: Manager<R>> crate::NfcExt<R> for T {
fn nfc(&self) -> &Nfc<R> {
self.state::<Nfc<R>>().inner()
}
}
/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("nfc")
.js_init_script(include_str!("api-iife.js").to_string())
.setup(|app, api| {
#[cfg(target_os = "android")]
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "NfcPlugin")?;
#[cfg(target_os = "ios")]
let handle = api.register_ios_plugin(init_plugin_nfc)?;
app.manage(Nfc(handle));
Ok(())
})
.build()
}