CodexViewModel.camera.kt

4.3 KB · raw

1// D10 — Camera-scan additions for CodexViewModel.2//3// These members must be MERGED INTO the existing CodexViewModel class4// (ui/codex/CodexViewModel.kt). They are presented here as a standalone object so5// the draft compiles in isolation for review; the orchestrator pastes the marked6// members into the real class (it already has `viewModelScope`, `context`, the7// `_restore` MutableStateFlow, and `resetRestore()`).8//9// New imports the real CodexViewModel.kt needs:10//   import com.peachpasswords.codex.ChunkEnvelope11//   import com.peachpasswords.codex.CodexRestore12//   import com.peachpasswords.codex.CodexException13//   import androidx.camera.core.ImageProxy14//15// CodexRestore.parseEnvelope(text) and CodexRestore.reconstructPayload(envelopes)16// already exist and are public — reused unchanged.1718package com.peachpasswords.ui.codex.draft_d101920import androidx.camera.core.ImageProxy21import com.peachpasswords.codex.ChunkEnvelope22import com.peachpasswords.codex.CodexAztecFrame23import com.peachpasswords.codex.CodexRestore24import kotlinx.coroutines.flow.MutableStateFlow25import kotlinx.coroutines.flow.StateFlow26import kotlinx.coroutines.flow.asStateFlow2728/**29 * Drives the camera phase. `Choosing` is the new initial surface (two option30 * cards); `Scanning` runs while the camera analyzes frames; `Timeout` shows the31 * no-detection overlay after 10s. The decode handoff reuses the existing32 * RestoreState path: once enough codes reconstruct a payload, the VM sets33 * RestoreState.Decoded exactly as the PDF path does.34 */35sealed interface CameraScanState {36    data object Choosing : CameraScanState37    data class Scanning(val codesFound: Int) : CameraScanState38    data class Timeout(val codesFound: Int) : CameraScanState39}4041// ---------------------------------------------------------------------------42// MERGE THE FOLLOWING INTO CodexViewModel (not this draft object).43// ---------------------------------------------------------------------------44object CodexViewModelCameraMembers {45    /*46    private val _camera = MutableStateFlow<CameraScanState>(CameraScanState.Choosing)47    val camera: StateFlow<CameraScanState> = _camera.asStateFlow()4849    // Envelopes collected so far this camera session, deduped by stripe:index —50    // mirrors CodexRestore.decodeFromUri's LinkedHashMap dedup.51    private val cameraEnvelopes = LinkedHashMap<String, ChunkEnvelope>()5253    /** Show the two-card choose surface (default landing for restore now). */54    fun chooseRestore() {55        cameraEnvelopes.clear()56        _camera.value = CameraScanState.Choosing57        _restore.value = RestoreState.Idle58    }5960    /** Begin (or restart after a timeout) live camera scanning. */61    fun startCameraScan() {62        cameraEnvelopes.clear()63        _camera.value = CameraScanState.Scanning(0)64    }6566    /** UI reports 10s elapsed with no successful reconstruction. */67    fun cameraTimedOut() {68        val s = _camera.value69        if (s is CameraScanState.Scanning) _camera.value = CameraScanState.Timeout(s.codesFound)70    }7172    /**73     * Called from the ImageAnalysis.Analyzer for every frame. Decodes one Aztec,74     * accumulates it, and once the collected set reconstructs a valid payload,75     * transitions to RestoreState.Decoded — the exact entry point the PDF path76     * uses. The analyzer must close the ImageProxy in its finally block.77     *78     * Returns true once a payload has been decoded (caller stops analysis).79     */80    fun onCameraFrame(image: ImageProxy): Boolean {81        if (_camera.value !is CameraScanState.Scanning) return _restore.value is RestoreState.Decoded82        val text = CodexAztecFrame.decode(image) ?: return false83        val env = CodexRestore.parseEnvelope(text) ?: return false84        cameraEnvelopes.putIfAbsent("${env.s}:${env.i}", env)85        _camera.value = CameraScanState.Scanning(cameraEnvelopes.size)8687        // Best-effort reconstruct on each new code: Reed-Solomon succeeds (and88        // the fingerprint validates) only once enough stripes are present, so a89        // thrown CodexException just means "keep scanning".90        return try {91            val payload = CodexRestore.reconstructPayload(cameraEnvelopes.values.toList())92            _restore.value = RestoreState.Decoded(payload, cameraEnvelopes.size)93            true94        } catch (_: Exception) {95            false96        }97    }98    */99}