# D10 — Codex Restore Camera Path — Integration Section 8 of the remaining-gaps brief. Adds a two-card "choose" surface and a live camera Aztec-scan path to `CodexRestoreScreen`. The PDF import path is unchanged — it just moves behind the new choose surface. Draft files in this folder: - `CodexRestoreChooseAndCamera.kt` — `CodexRestoreChoose` + `CodexCameraScan` composables (package `com.peachpasswords.ui.codex`). - `CodexAztecFrame.kt` — per-frame Aztec decode for `ImageProxy` (package `com.peachpasswords.codex`). Already drafted, used unchanged. - `CodexViewModel.camera.kt` — camera members to merge into the real `CodexViewModel`. ## 1. Gradle deps — `app/build.gradle` CameraX current stable is **1.3.4**. ZXing core (`com.google.zxing:core:3.5.3`) is already present (line 99) — no ZXing change needed. Add inside `dependencies {` (near line 99, next to the existing zxing line): ```gradle implementation "androidx.camera:camera-core:1.3.4" implementation "androidx.camera:camera-camera2:1.3.4" implementation "androidx.camera:camera-lifecycle:1.3.4" implementation "androidx.camera:camera-view:1.3.4" ``` `androidx.lifecycle.compose.LocalLifecycleOwner` (used by the preview) is provided by the already-present `androidx.lifecycle:lifecycle-runtime-compose:2.8.4`. ## 2. AndroidManifest — CAMERA permission In `app/src/main/AndroidManifest.xml`, above ``: ```xml ``` `required="false"` keeps the app installable on camera-less devices (the denied/ unavailable case already shows the settings card). ## 3. Merge camera members into `CodexViewModel.kt` Copy the commented body inside `CodexViewModelCameraMembers` (in `CodexViewModel.camera.kt`) into the `CodexViewModel` class, and add the `CameraScanState` sealed interface to that file (top level, beside `RestoreState`). Drop the `draft_d10` package — it goes in `com.peachpasswords.ui.codex`. Add imports to `CodexViewModel.kt`: ```kotlin import androidx.camera.core.ImageProxy import com.peachpasswords.codex.ChunkEnvelope import com.peachpasswords.codex.CodexAztecFrame ``` (`CodexRestore` is already imported.) New public VM surface after merge: - `val camera: StateFlow` (initial `Choosing`) - `fun chooseRestore()` — show the two cards, reset restore state - `fun startCameraScan()` — begin/restart scanning - `fun cameraTimedOut()` — record 10s no-detection - `fun onCameraFrame(image: ImageProxy): Boolean` — analyze one frame; returns true once a payload reconstructs (sets `RestoreState.Decoded`, same entry point as the PDF path, so the existing `DecodedBody` password/confirm flow works unchanged). Verified against reality: `ChunkEnvelope` has `s: Int` (stripe) and `i: Int` (index) — the dedup key `"${env.s}:${env.i}"` is correct. `CodexRestore.parseEnvelope(text): ChunkEnvelope?` and `CodexRestore.reconstructPayload(List): CodexPayload` are public and reused unchanged. `RestoreState.Decoded(payload, codesFound)` matches the existing constructor. ## 4. Wire `CodexRestoreScreen.kt` The screen currently launches the PDF picker directly from `RestoreState.Idle`. Replace that branch so `Idle` is gated by the camera state. **Add imports:** ```kotlin import androidx.compose.foundation.layout.Box ``` (plus the new composables are same-package, no import needed). **Collect the camera state** — after the existing `state` collection (around line 36): ```kotlin val cameraState by vm.camera.collectAsStateWithLifecycle() ``` **Replace the `RestoreState.Idle` branch.** Current anchor (lines 53-54): ```kotlin is RestoreState.Idle -> CodexButton("Choose Codex PDF", accent, filled = true) { pickLauncher.launch("application/pdf") } ``` Replace with: ```kotlin is RestoreState.Idle -> when (val cs = cameraState) { is CameraScanState.Choosing -> CodexRestoreChoose( accent = accent, onScanCamera = { vm.startCameraScan() }, onImportPdf = { pickLauncher.launch("application/pdf") }, ) is CameraScanState.Scanning -> CodexCameraScan( accent = accent, timedOut = false, codesFound = cs.codesFound, onFrame = { vm.onCameraFrame(it) }, onTimeout = { vm.cameraTimedOut() }, onRetry = { vm.startCameraScan() }, ) is CameraScanState.Timeout -> CodexCameraScan( accent = accent, timedOut = true, codesFound = cs.codesFound, onFrame = { vm.onCameraFrame(it) }, onTimeout = { }, onRetry = { vm.startCameraScan() }, ) } ``` When `onCameraFrame` reconstructs a payload it sets `RestoreState.Decoded`, so the `when(state)` flips to the existing `DecodedBody` automatically — the camera overlay disappears and the password prompt appears. No further wiring needed for the decode→restore handoff. **Reset on entry:** ensure the screen lands on the choose surface each visit. The VM defaults `camera` to `Choosing` and `restore` to `Idle`, so a fresh VM is already correct. If the screen can be revisited with a retained VM, call `vm.chooseRestore()` from a `LaunchedEffect(Unit)` at the top of the screen. **Error branch:** the existing `RestoreState.Error` "Choose another file" button re-launches the PDF picker. Optionally also call `vm.chooseRestore()` there so a failed camera scan returns to the two cards; the brief doesn't require it. ## 5. Notes / risks - The intro paragraph (lines 46-49) says "Choose a Codex PDF…". Suggest softening to mention camera too, e.g. "Scan the printed Aztec grid with your camera, or choose the Codex PDF…". Cosmetic; not required. - `CodexCameraScan` runs its own 10s `LaunchedEffect` timer keyed on `timedOut`; it also reports `onTimeout`. Because the VM's `Scanning -> Timeout` transition re-composes with `timedOut = true`, the timer self-cancels — no double-fire. - The analyzer closes every `ImageProxy` in a `finally`; `onCameraFrame` must not close it (it doesn't — `CodexAztecFrame.decode` explicitly does not close). - Reuses `CodexAztecFrame.decode` (scene detector, no PURE_BARCODE) for camera frames — distinct from `CodexAztec.decode` which is for cropped PDF cells. - No `RuneBackground` usage here; the screen already uses `ShellBackground`. - `FLAG_SECURE`: the restore screen does not currently set it; out of scope for D10. Flag if a security pass wants the live camera+decoded preview protected. ```