// D10 — Camera-scan additions for CodexViewModel. // // These members must be MERGED INTO the existing CodexViewModel class // (ui/codex/CodexViewModel.kt). They are presented here as a standalone object so // the draft compiles in isolation for review; the orchestrator pastes the marked // members into the real class (it already has `viewModelScope`, `context`, the // `_restore` MutableStateFlow, and `resetRestore()`). // // New imports the real CodexViewModel.kt needs: // import com.peachpasswords.codex.ChunkEnvelope // import com.peachpasswords.codex.CodexRestore // import com.peachpasswords.codex.CodexException // import androidx.camera.core.ImageProxy // // CodexRestore.parseEnvelope(text) and CodexRestore.reconstructPayload(envelopes) // already exist and are public — reused unchanged. package com.peachpasswords.ui.codex.draft_d10 import androidx.camera.core.ImageProxy import com.peachpasswords.codex.ChunkEnvelope import com.peachpasswords.codex.CodexAztecFrame import com.peachpasswords.codex.CodexRestore import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow /** * Drives the camera phase. `Choosing` is the new initial surface (two option * cards); `Scanning` runs while the camera analyzes frames; `Timeout` shows the * no-detection overlay after 10s. The decode handoff reuses the existing * RestoreState path: once enough codes reconstruct a payload, the VM sets * RestoreState.Decoded exactly as the PDF path does. */ sealed interface CameraScanState { data object Choosing : CameraScanState data class Scanning(val codesFound: Int) : CameraScanState data class Timeout(val codesFound: Int) : CameraScanState } // --------------------------------------------------------------------------- // MERGE THE FOLLOWING INTO CodexViewModel (not this draft object). // --------------------------------------------------------------------------- object CodexViewModelCameraMembers { /* private val _camera = MutableStateFlow(CameraScanState.Choosing) val camera: StateFlow = _camera.asStateFlow() // Envelopes collected so far this camera session, deduped by stripe:index — // mirrors CodexRestore.decodeFromUri's LinkedHashMap dedup. private val cameraEnvelopes = LinkedHashMap() /** Show the two-card choose surface (default landing for restore now). */ fun chooseRestore() { cameraEnvelopes.clear() _camera.value = CameraScanState.Choosing _restore.value = RestoreState.Idle } /** Begin (or restart after a timeout) live camera scanning. */ fun startCameraScan() { cameraEnvelopes.clear() _camera.value = CameraScanState.Scanning(0) } /** UI reports 10s elapsed with no successful reconstruction. */ fun cameraTimedOut() { val s = _camera.value if (s is CameraScanState.Scanning) _camera.value = CameraScanState.Timeout(s.codesFound) } /** * Called from the ImageAnalysis.Analyzer for every frame. Decodes one Aztec, * accumulates it, and once the collected set reconstructs a valid payload, * transitions to RestoreState.Decoded — the exact entry point the PDF path * uses. The analyzer must close the ImageProxy in its finally block. * * Returns true once a payload has been decoded (caller stops analysis). */ fun onCameraFrame(image: ImageProxy): Boolean { if (_camera.value !is CameraScanState.Scanning) return _restore.value is RestoreState.Decoded val text = CodexAztecFrame.decode(image) ?: return false val env = CodexRestore.parseEnvelope(text) ?: return false cameraEnvelopes.putIfAbsent("${env.s}:${env.i}", env) _camera.value = CameraScanState.Scanning(cameraEnvelopes.size) // Best-effort reconstruct on each new code: Reed-Solomon succeeds (and // the fingerprint validates) only once enough stripes are present, so a // thrown CodexException just means "keep scanning". return try { val payload = CodexRestore.reconstructPayload(cameraEnvelopes.values.toList()) _restore.value = RestoreState.Decoded(payload, cameraEnvelopes.size) true } catch (_: Exception) { false } } */ }