Modern Apps git

← Modern-Apps.git

calculator: cap widget expression length so taps stay fast (#641)

Commit
3ac846970c7362677a73e8043b6db5e152ec9fa9
Author
Vayun Mathur <[email protected]>
When
2026-11-06 04:49 UTC (1 month ago)
Parents
4da2fd6
The keypad widget stored its in-progress expression in Glance
preferences with an unbounded append on every tap. Each tap then
re-read a longer blob from DataStore, re-parsed a longer string for
the live preview and pushed longer RemoteViews text to the host, so
per-tap cost grew with total taps: slower and slower until updates
stalled on every device. The in-app keypad keeps input in RAM with
no per-keystroke serialization, which is why only the widget died.

Cap the stored expression at 200 chars (far beyond the single-line
display) via a pure appendWidgetInput helper, and clamp on read so an
already-oversized value left behind from before the cap heals on the
next tap of any key. Add CalculatorWidgetInputCapTest covering
normal typing, the bound, prefix preservation and legacy shrink.

Verified: :calculator:compileDevKotlin and the new unit test pass.

Diff

calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorKeyAction.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorKeyAction.kt b/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorKeyAction.ktindex d7bda91c8e..aa377100e1 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorKeyAction.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/widget/CalculatorKeyAction.kt@@ -39,6 +39,20 @@ internal const val COMMAND_EVALUATE = "evaluate" /** Prefixed rather than bare so an appended "=" or "clear" could never be mistaken for a command. */ internal const val COMMAND_APPEND_PREFIX = "append:" +/**+ * Longest expression the widget keeps. Every tap appends to the stored input, which is then+ * re-read from DataStore, re-parsed for the live preview and shipped to the host as RemoteViews+ * text — so an unbounded string made each tap slower than the last until updates stalled (#641).+ * A few hundred chars is far beyond what the single-line display fits; anything longer only ever+ * came from unbounded growth. `take` (not `takeLast`) keeps the start of what was typed, and also+ * shrinks an already-oversized value left behind from before the cap on the next tap.+ */+internal const val MAX_WIDGET_INPUT_LENGTH = 200++/** Appends [text] to the widget [input], keeping the stored expression bounded. Pure for tests. */+internal fun appendWidgetInput(input: String, text: String): String =+    (input + text).take(MAX_WIDGET_INPUT_LENGTH)+ /**  * A result of the app's evaluator, in both the pretty form for the display and the ASCII  * token form that re-parses so it can seed the next calculation.@@ -90,7 +104,11 @@ class CalculatorKeyAction : ActionCallback {         // predefined effect to its Q+ API, with a legacy fallback beneath it.         emitKeyTick(context)         updateAppWidgetState(context, glanceId) { prefs ->-            val input = prefs[CalculatorInputKey].orEmpty()+            // Clamp first: shrinks an already-oversized value left behind from before the+            // cap (#641) so every tap heals, not just appends.+            val rawInput = prefs[CalculatorInputKey].orEmpty()+            val input = rawInput.take(MAX_WIDGET_INPUT_LENGTH)+            if (input.length < rawInput.length) prefs[CalculatorInputKey] = input             when {                 command == COMMAND_CLEAR -> prefs[CalculatorInputKey] = ""                 command == COMMAND_BACKSPACE -> prefs[CalculatorInputKey] = input.dropLast(1)@@ -102,7 +120,10 @@ class CalculatorKeyAction : ActionCallback {                     }                 }                 command.startsWith(COMMAND_APPEND_PREFIX) ->-                    prefs[CalculatorInputKey] = input + command.removePrefix(COMMAND_APPEND_PREFIX)+                    prefs[CalculatorInputKey] = appendWidgetInput(+                        input,+                        command.removePrefix(COMMAND_APPEND_PREFIX),+                    )             }         }         // Writing state alone does not reliably recompose; the explicit update does.
calculator/src/test/java/com/vayunmathur/calculator/widget/CalculatorWidgetInputCapTest.kt
diff --git a/calculator/src/test/java/com/vayunmathur/calculator/widget/CalculatorWidgetInputCapTest.kt b/calculator/src/test/java/com/vayunmathur/calculator/widget/CalculatorWidgetInputCapTest.ktnew file mode 100644index 0000000000..73a2d602bb--- /dev/null+++ b/calculator/src/test/java/com/vayunmathur/calculator/widget/CalculatorWidgetInputCapTest.kt@@ -0,0 +1,43 @@+package com.vayunmathur.calculator.widget++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * The widget input lives in Glance preferences and is re-read, re-parsed and re-rendered on+ * every tap, so it must stay bounded: without a cap each tap made the next one slower until+ * updates stalled (#641).+ */+class CalculatorWidgetInputCapTest {++    @Test+    fun normalTypingIsUntouched() {+        assertEquals("12+8", appendWidgetInput("12+", "8"))+        assertEquals("", appendWidgetInput("", ""))+        assertEquals("7", appendWidgetInput("", "7"))+    }++    @Test+    fun longInputStaysBounded() {+        val grown = "1".repeat(MAX_WIDGET_INPUT_LENGTH) + "2+3"+        val capped = appendWidgetInput(grown, "4")+        assertTrue(+            capped.length <= MAX_WIDGET_INPUT_LENGTH,+            "widget input grew to ${capped.length}",+        )+    }++    @Test+    fun capKeepsTheStartOfWhatWasTyped() {+        val capped = appendWidgetInput("9*9+", "8".repeat(MAX_WIDGET_INPUT_LENGTH))+        assertTrue(capped.startsWith("9*9+"))+        assertEquals(MAX_WIDGET_INPUT_LENGTH, capped.length)+    }++    @Test+    fun alreadyOversizedInputShrinksOnNextTap() {+        val legacy = "5".repeat(MAX_WIDGET_INPUT_LENGTH + 50)+        assertEquals(MAX_WIDGET_INPUT_LENGTH, appendWidgetInput(legacy, "").length)+    }+}