tauri-plugin-healthkit 0.1.3

A Tauri plugin for accessing HealthKit (iOS) and Health Connect (Android)
use serde::de::DeserializeOwned;
use tauri::{
    plugin::{PluginApi, PluginHandle},
    AppHandle, Runtime,
};

use crate::models::*;

#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_healthkit);

// initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>(
    _app: &AppHandle<R>,
    api: PluginApi<R, C>,
) -> crate::Result<Healthkit<R>> {
    #[cfg(target_os = "android")]
    let handle = api.register_android_plugin("life.thephage.healthkit", "HealthConnectPlugin")?;
    #[cfg(target_os = "ios")]
    let handle = api.register_ios_plugin(init_plugin_healthkit)?;
    Ok(Healthkit(handle))
}

/// Access to the healthkit APIs.
pub struct Healthkit<R: Runtime>(PluginHandle<R>);

impl<R: Runtime> Healthkit<R> {
    // ========================================================================
    // Status & Permissions
    // ========================================================================

    pub fn get_status(&self) -> crate::Result<StatusResponse> {
        self.0
            .run_mobile_plugin("getHealthStatus", ())
            .map_err(Into::into)
    }

    pub fn check_permissions(&self) -> crate::Result<PermissionsResponse> {
        self.0
            .run_mobile_plugin("checkPermissions", ())
            .map_err(Into::into)
    }

    pub fn request_permissions(
        &self,
        payload: PermissionsRequest,
    ) -> crate::Result<PermissionsResponse> {
        self.0
            .run_mobile_plugin("requestPermissions", payload)
            .map_err(Into::into)
    }

    // ========================================================================
    // Settings & Store
    // ========================================================================

    /// Open OS health settings screen
    pub fn open_settings(&self) -> crate::Result<()> {
        self.0
            .run_mobile_plugin::<()>("openSettings", ())
            .map_err(Into::into)
    }

    /// Open Play Store for Health Connect (Android only, no-op on iOS)
    pub fn open_play_store(&self) -> crate::Result<()> {
        self.0
            .run_mobile_plugin::<()>("openPlayStore", ())
            .map_err(Into::into)
    }

    // ========================================================================
    // Step Count Data
    // ========================================================================

    /// Get raw step data with 5-minute intervals
    pub fn get_step(
        &self,
        payload: StepDataRequest,
    ) -> crate::Result<StepResponse> {
        self.0
            .run_mobile_plugin("getStep", payload)
            .map_err(Into::into)
    }

    // ========================================================================
    // Sleep Data
    // ========================================================================

    /// Get sleep data within time range
    pub fn get_sleep(&self, payload: SleepDataRequest) -> crate::Result<SleepResponse> {
        self.0
            .run_mobile_plugin("getSleep", payload)
            .map_err(Into::into)
    }

    // ========================================================================
    // Weight Data
    // ========================================================================

    /// Get weight data within time range
    pub fn get_weight(&self, payload: WeightDataRequest) -> crate::Result<WeightResponse> {
        self.0
            .run_mobile_plugin("getWeight", payload)
            .map_err(Into::into)
    }
}