translate: swap can no longer destroy the input when the output is stale or blank
- Commit
3411d2760e5dd55692e4b217d2357a11bb0e7c4e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 04:21 UTC (5 days ago)
- Parents
fc4f7cd
outputText carried no provenance: nothing recorded which input + language pair produced it, yet swap() consumed it unconditionally as if it were the current input's translation. With a slow or unfired translation, Swap turned the input into "" (erasing the typed text) or, after editing past a previous result, into the previous text's stale translation — the user's current text silently discarded, never translated or recorded. The page now tracks output provenance (input + source + target, set when a translation lands from the captured request values and on history reuse, cleared on blank/failed output). Swap exchanges texts only when the output is provably the completed translation of the current input under the current pair; otherwise it swaps only the languages, keeps the user's input, drops the stale output and lets the debounced effect re-translate. Three regression tests failed pre-fix on the erasing cases and pin the exchange-still-works case. Language catalogue sync, history, camera memo and the chunk splitter were audited and cleared. Verified: :translate:testDevUnitTest 144 tests pass; lint errors are pre-existing on untouched files.
Diff
translate/src/main/java/com/vayunmathur/translate/ui/TextTranslatePage.kt
diff --git a/translate/src/main/java/com/vayunmathur/translate/ui/TextTranslatePage.kt b/translate/src/main/java/com/vayunmathur/translate/ui/TextTranslatePage.ktindex 5e8cf247f4..90b95aeaa5 100644--- a/translate/src/main/java/com/vayunmathur/translate/ui/TextTranslatePage.kt+++ b/translate/src/main/java/com/vayunmathur/translate/ui/TextTranslatePage.kt@@ -78,6 +78,18 @@ fun TextTranslatePage( var outputText by remember { mutableStateOf("") } var isTranslating by remember { mutableStateOf(false) } + // What the displayed output is a translation *of*: the input text and+ // language pair that produced it. The output is not cleared the moment+ // the input or pair changes (the previous result stays visible while the+ // new one is debounced/translated), so without this provenance the swap+ // action cannot tell a current result from a stale one — and swapping+ // with a stale (or still blank) output moves the wrong text into the+ // input field, discarding what the user has actually typed. Null when+ // there is no completed translation on show.+ var outputInput by remember { mutableStateOf<String?>(null) }+ var outputSource by remember { mutableStateOf<String?>(null) }+ var outputTarget by remember { mutableStateOf<String?>(null) }+ // A history row tapped for reuse: load its text + pair once, then clear the request. LaunchedEffect(reuseRequest) { val record = reuseRequest ?: return@LaunchedEffect@@ -85,6 +97,9 @@ fun TextTranslatePage( viewModel.setTarget(record.targetLang) inputText = record.sourceText outputText = record.translatedText+ outputInput = record.sourceText+ outputSource = record.sourceLang+ outputTarget = record.targetLang onReuseConsumed() } @@ -127,21 +142,38 @@ fun TextTranslatePage( // Live, debounced translation as input / language selection changes. LaunchedEffect(inputText, sourceLang, targetLang, translationAvailable) {- if (inputText.isBlank()) {+ // Capture the request up front: these are the values the displayed+ // result and the history row must be attributed to.+ val requestedInput = inputText+ val requestedSource = sourceLang+ val requestedTarget = targetLang+ if (requestedInput.isBlank()) { outputText = ""+ outputInput = null+ outputSource = null+ outputTarget = null isTranslating = false return@LaunchedEffect } if (!translationAvailable) return@LaunchedEffect delay(TRANSLATE_DEBOUNCE_MS) isTranslating = true- val translated = viewModel.translate(inputText).orEmpty()+ val translated = viewModel.translate(requestedInput).orEmpty() outputText = translated isTranslating = false- // Save every completed translation; the repository dedupes re-translations.- // The effect's own captured pair, not a fresh read: the language can change- // between the translate call and this write.- if (translated.isNotBlank()) viewModel.recordHistory(inputText, translated, sourceLang, targetLang)+ if (translated.isNotBlank()) {+ outputInput = requestedInput+ outputSource = requestedSource+ outputTarget = requestedTarget+ // Save every completed translation; the repository dedupes re-translations.+ // The effect's own captured pair, not a fresh read: the language can change+ // between the translate call and this write.+ viewModel.recordHistory(requestedInput, translated, requestedSource, requestedTarget)+ } else {+ outputInput = null+ outputSource = null+ outputTarget = null+ } } // Whether the current input + pair is already a favorite (for the star toggle).@@ -177,13 +209,36 @@ fun TextTranslatePage( actions = object : TextTranslateActions { override fun setSource(code: String) = viewModel.setSource(code) override fun setTarget(code: String) = viewModel.setTarget(code)- // Swap the text along with the languages, so the translation becomes the- // thing being translated. The debounced effect below then re-translates it.+ // Swap the languages, and the text along with them only when the+ // displayed output really is the completed translation of the+ // current input under the current pair — then the translation+ // becomes the thing being translated and the old input is its+ // (already correct) translation back. While a new input or pair+ // is still debounced/translating, the output is stale or blank:+ // moving it into the input would replace the user's text with+ // the previous translation (or with nothing), so in that case+ // only the languages swap, the current input stays, the stale+ // output is dropped, and the debounced effect re-translates. override fun swap() { if (sourceLang == Languages.AUTO.code) return- val previousInput = inputText- inputText = outputText- outputText = previousInput+ val outputIsCurrent = outputText.isNotBlank() &&+ outputInput == inputText &&+ outputSource == sourceLang &&+ outputTarget == targetLang+ if (outputIsCurrent) {+ val previousInput = inputText+ val previousOutput = outputText+ inputText = previousOutput+ outputText = previousInput+ outputInput = previousOutput+ outputSource = targetLang+ outputTarget = sourceLang+ } else {+ outputText = ""+ outputInput = null+ outputSource = null+ outputTarget = null+ } viewModel.swap() } override fun setInput(text: String) {
translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt
diff --git a/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt b/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.ktindex b8e741bcdd..e9eff238e8 100644--- a/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt+++ b/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt@@ -10,6 +10,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextInput+import androidx.compose.ui.test.performTextReplacement import androidx.test.core.app.ApplicationProvider import com.vayunmathur.translate.Navigation import com.vayunmathur.translate.data.PlainSqlCipherDriverShadow@@ -195,6 +196,113 @@ class DbBackedPagesTest { rule.onNodeWithText("Couldn’t load the translator. The model may not be downloaded yet, or it failed to start on this device.").assertIsDisplayed() } + /** Pumps the paused main looper until a reuse request has been consumed once. */+ private fun pumpUntilConsumed(consumed: () -> Int) {+ var steps = 0+ while (consumed() != 1 && steps++ < 6000) {+ idle()+ rule.waitForIdle()+ if (consumed() == 1) break+ try { Thread.sleep(25) } catch (_: InterruptedException) { }+ }+ assertEquals(1, consumed())+ }++ @Test+ fun `text page swap with no translation yet keeps the input and swaps only languages`() {+ val viewModel = vm()+ viewModel.setSource("en")+ viewModel.setTarget("es")+ idle()+ rule.setContent {+ TextTranslatePage(+ viewModel = viewModel,+ initialText = "hello world",+ reuseRequest = null,+ onReuseConsumed = {},+ onOpenCamera = {},+ onOpenImage = {},+ onOpenHistory = {},+ onOpenLanguagePicker = {},+ )+ }+ // No model in this sandbox, so no translation ever lands and the+ // output is blank. Swapping must not move that blank into the input+ // (discarding the typed text); only the pair reverses.+ rule.onNodeWithContentDescription("Swap languages").performClick()+ rule.waitForIdle()+ rule.onNodeWithText("hello world").assertIsDisplayed()+ assertEquals("es", viewModel.sourceLang.value)+ assertEquals("en", viewModel.targetLang.value)+ }++ @Test+ fun `text page swap after editing past a reused translation does not move stale output into the input`() {+ val (record) = seed(Triple("reuse hello", "reuse hola", "en" to "es"))+ val viewModel = vm()+ idle()+ var consumed = 0+ rule.setContent {+ TextTranslatePage(+ viewModel = viewModel,+ initialText = "",+ reuseRequest = record,+ onReuseConsumed = { consumed++ },+ onOpenCamera = {},+ onOpenImage = {},+ onOpenHistory = {},+ onOpenLanguagePicker = {},+ )+ }+ pumpUntilConsumed { consumed }+ // The reused translation is the current output for "reuse hello" —+ // until the user edits the input. From here the output on show is+ // stale (no model here re-translates the edit), so swap may only+ // reverse the pair, never promote the stale output into the input.+ rule.onNodeWithText("reuse hello").performTextReplacement("edited input")+ rule.onNodeWithContentDescription("Swap languages").performClick()+ rule.waitForIdle()+ rule.onNodeWithText("edited input").assertIsDisplayed()+ assertTrue(rule.onAllNodesWithText("reuse hola").fetchSemanticsNodes().isEmpty())+ assertEquals("es", viewModel.sourceLang.value)+ assertEquals("en", viewModel.targetLang.value)+ }++ @Test+ fun `text page swap with a current translation still exchanges the texts`() {+ val (record) = seed(Triple("reuse hello", "reuse hola", "en" to "es"))+ val viewModel = vm()+ idle()+ var consumed = 0+ rule.setContent {+ TextTranslatePage(+ viewModel = viewModel,+ initialText = "",+ reuseRequest = record,+ onReuseConsumed = { consumed++ },+ onOpenCamera = {},+ onOpenImage = {},+ onOpenHistory = {},+ onOpenLanguagePicker = {},+ )+ }+ pumpUntilConsumed { consumed }+ rule.onNodeWithContentDescription("Swap languages").performClick()+ rule.waitForIdle()+ // Input becomes the translation. (The output card itself is behind+ // the failed-load state in this no-model sandbox, so the old input+ // sitting in the output is proven by swapping back: only a current+ // output swaps back into the input.)+ rule.onNodeWithText("reuse hola").assertIsDisplayed()+ assertEquals("es", viewModel.sourceLang.value)+ assertEquals("en", viewModel.targetLang.value)+ rule.onNodeWithContentDescription("Swap languages").performClick()+ rule.waitForIdle()+ rule.onNodeWithText("reuse hello").assertIsDisplayed()+ assertEquals("en", viewModel.sourceLang.value)+ assertEquals("es", viewModel.targetLang.value)+ }+ @Test fun `text page mic tap surfaces a speech or permission error`() { // Grant RECORD_AUDIO so the tap goes straight to the recognizer,