# Plugins & Native Modules

`zero-native` keeps the core runtime small by shipping optional native features as **plugins**. If your app doesn't include a plugin, it pays **0 bytes** — and the OTA capability manifest automatically tracks every plugin your bundle uses so older app binaries never crash from a missing native module.

---

## 1. Official Plugins

All 20 official plugins live under `plugins/` with individual READMEs for detailed API docs. (17 have native implementations on both Android and iOS. `markdown` is pure JS, `android-play-auth` answers "unavailable" on iOS, and `android-ad-id` is Android-only.)

| Plugin | Android | iOS | What it provides |
|---|:---:|:---:|---|
| `@zeronative/scanner` | ✅ | ✅ | QR & barcode scanner (`<QrScanner>`, CameraX + bundled ML Kit / AVFoundation) with stale-frame protection |
| `@zeronative/document-scanner` | ✅ | ✅ | Paper document camera scanner with edge detection & process-kill recovery |
| `@zeronative/pdf` | ✅ | ✅ | Native PDF viewer & downloader (PdfRenderer / PDFKit) |
| `@zeronative/webview` | ✅ | ✅ | Embedded web view (`<WebView>`) with navigation interception & origin-checked JS bridge |
| `@zeronative/image-viewer` | ✅ | ✅ | Full-screen zoomable photo viewer (`ImageViewer.open(...)`) |
| `@zeronative/lottie` | ✅ | ✅ | Lottie animations (`lottie-react-native` API) |
| `@zeronative/markdown` | ✅ (JS) | ✅ (JS) | Pure-JS Markdown rendered as native `<Text>` & layout components |
| `@zeronative/location` | ✅ | ✅ | GPS coordinates & geocoding |
| `@zeronative/media` | ✅ | ✅ | Photo/file picker, camera capture, native resize/compress/crop |
| `@zeronative/notifications` | ✅ | ✅ | Local notifications, scheduling, action buttons, channels/threads (Notifee-like API) |
| `@zeronative/device-info` | ✅ | ✅ | Device model, OS version, app version, build number (`react-native-device-info` style) |
| `@zeronative/app-updates` | ✅ | ✅ | Store update prompts (Google Play In-App Updates on Android; App Store link on iOS) |
| `@zeronative/android-play-auth` | ✅ | stub | Android phone number hint & SMS Retriever autofill (returns unavailable on iOS) |
| `@zeronative/android-ad-id` | ✅ | — | Google Advertising ID for attribution |
| `@zeronative/firebase-app` | ✅ | ✅ | Shared Firebase base required by Firebase plugins |
| `@zeronative/firebase-crashlytics` | ✅ | ✅ | JS & native crash reporting |
| `@zeronative/firebase-analytics` | ✅ | ✅ | Event & user property analytics |
| `@zeronative/firebase-messaging` | ✅ | ✅ | FCM / APNs push tokens, topics, data messages, native OTA-check push |
| `@zeronative/firebase-remote-config` | ✅ | ✅ | Remote Config fetch & activation |
| `@zeronative/firebase-perf` | ✅ | ✅ | Network & custom performance traces |

---

## 2. Adding an Official Plugin to Your App

While building from a local `zero-native` checkout, wire a plugin in four places:

1. **JS (`package.json`):** Add `"@zeronative/<name>": "link:../zero-native/plugins/<name>"` and run `bun install`.
2. **Android (`android/settings.gradle.kts` + `android/app/build.gradle.kts`):** In a `zn new` app, add a `dependencySubstitution` entry in `settings.gradle.kts` mapping `io.github.zero-native:plugin-<name>` to `:plugin-<name>`, then add `implementation("io.github.zero-native:plugin-<name>")` in `app/build.gradle.kts`.
3. **iOS (`ios/CMakeLists.txt`):** Add the plugin folder name to `APP_ZN_PLUGINS` (or, when using SwiftPM, select the `ZnPlugin<Name>` product: from the zero-native package, except the Firebase plugins and Lottie, which come from the `zero-native-firebase` and `zero-native-lottie` packages at the same version; in a local checkout, `ios/packages/firebase` and `ios/packages/lottie`. See `docs/releasing.md`).
4. **Register at startup:** Register the plugin in Android `MainApplication.kt` and iOS `AppDelegate.swift` (`ZnPlugins.register(...)`).

In an app on the published packages (made by `zn new` from npm, once `0.1.0` is out), a plugin goes in three places,
all at the app's zero-native version (say `0.1.0`; `zn build` stops when one differs):

1. **JS:** `bun add --exact @zeronative/<name>@0.1.0`. Without `--exact`, Bun writes `^0.1.0`, which `zn build`
   refuses: an update could then move the JS without the native side.
2. **Android (`android/app/build.gradle.kts`):** `implementation("io.github.zero-native:plugin-<name>:0.1.0")`, from
   Maven Central. No `settings.gradle.kts` change. Like the runtime, each plugin has a debug and a release build, so
   a build type of your own needs `matchingFallbacks` ([Getting Started](https://zero-native.github.io/zero-native/get-started/)). A Firebase plugin also applies the Gradle file from npm, at the end
   of that file: `apply(from = "../../node_modules/@zeronative/firebase-app/firebase.gradle.kts")`
   ([its README](https://zero-native.github.io/zero-native/plugins/firebase-app/)). That file is only in `@zeronative/firebase-app`, which the
   other Firebase plugins don't bring on npm, so add it too: `bun add --exact @zeronative/firebase-app@0.1.0`.
3. **iOS:** in Xcode, the plugin's `ZnPlugin<Name>` product of the zero-native package on the app target. The Firebase
   plugins and Lottie come from `https://github.com/zero-native/zero-native-firebase` and
   `https://github.com/zero-native/zero-native-lottie`, added at the same exact version.

Then register it at startup, as in step 4.

---

## 3. Core Plumbing Every Plugin Shares

Common OS primitives are built directly into `@zeronative/core`:

- **Permissions:** `Permissions.status(["camera"])` and `Permissions.request(["location"])` (both take arrays of permission names and return a map of `"granted" | "denied" | "blocked"`).
- **Opening external apps for a result:** `launches` (survives Android process death).
- **Links & sharing:** `Linking.openURL(...)`, `Share.share(...)`.
- **Clipboard, haptics, vibration, OS info:** `Clipboard`, `Os`, `Vibration`, `Platform`, `Dimensions`.
- **Background JS tasks:** `defineTask("my.task", async (data) => ...)` runs headless JS in your bundle with a time limit without a screen mounted. *(For FCM push messages, use `Push.onMessage` from `@zeronative/firebase-messaging`, which wires the `push.message` task automatically.)*

---

## 4. Writing Your Own Plugin

A plugin is a package with a `zeroNative` block in `package.json` (declaring `platforms` and `android.plugin`) and native code on Android and/or iOS (views and screens are registered natively inside `register(r)`). Module names should start with the package's first word, and module versions follow **add-only versioning** (increment `version` whenever you add a method; never break existing methods).

### A. Native Modules (JS ↔ Kotlin / Swift)
Declare a typed proxy handle in TypeScript using `nativeModule`:

```ts
import { nativeModule } from "@zeronative/core"

interface MyPaymentsModule {
  startUpi(args: { uri: string }): Promise<{ status: string }>
  events: { statusChanged: { status: string } }
}

// The compiler records "MyPayments" version 1 in zn-bundle.json automatically
export const MyPayments = nativeModule<MyPaymentsModule>("MyPayments", { version: 1 })

// Call methods directly on the proxy:
const res = await MyPayments.startUpi({ uri: "upi://pay?..." })
```

- **Android:** Subclass `ZnModuleBase("MyPayments", version = 1)` and register it from your `ZnPlugin`'s `register(r)` (`r.module(...)`), then register the plugin in `MainApplication.kt`.
- **iOS:** Conform to `ZnPlugin`, implement `register(_ r: ZnPluginRegistry)` calling `r.module(...)` / `r.view(...)` / `r.screen(...)`, and register the plugin via `ZnPlugins.register(...)` in `AppDelegate.swift`.

### B. Native Views (`<NativeView>`)
Plugins can expose custom native views (Compose composables, Android Views, or UIKit/SwiftUI views) via `r.view("plugin.viewName", ...)` and wrap them in a clean TSX component (for example, `@zeronative/scanner` wraps `<NativeView name="scanner.qr" />` in `<QrScanner onScan={...} />`):

```tsx
import { NativeView } from "@zeronative/ui"

<NativeView
  name="scanner.qr"
  style={{ width: "100%", height: 300 }}
  props={{ torch: torchOn() }}
  onEvent={(e) => {
    if (e.name === "scanned") handleQr(e.data.value)
  }}
/>
```

- `name` must be a string literal so the compiler verifies the plugin is installed and records it in the OTA capability manifest. Always give `<NativeView>` an explicit size in `style`.

### C. Native Screens (Routes)
Plugins can register full native screens via `r.screen("myplugin.picker", ...)` and expose them as routes:

```ts
export const routes = defineRoutes({
  picker: { path: "/picker", native: "myplugin.picker" },
})
```

For screens that return a result, use `nav.pushForResult(...)` / `onNavResult(...)`. *(For official plugins with helper APIs — such as `ImageViewer.open({ images, imageIndex })` — use the plugin's API rather than pushing its internal route directly.)*
