INTEGRATION.md

6.8 KB · raw

rendered · source

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:

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):

    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 <application>:

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera.any" android:required="false" />

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:

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:

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<ChunkEnvelope>): 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:

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):

    val cameraState by vm.camera.collectAsStateWithLifecycle()

Replace the RestoreState.Idle branch. Current anchor (lines 53-54):

                is RestoreState.Idle ->
                    CodexButton("Choose Codex PDF", accent, filled = true) { pickLauncher.launch("application/pdf") }

Replace with:

                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