Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix(ui): the context meter updates live during a run, and fills partial segments (#71)

Durganshue4faff9568ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ca50b0e..72b2eb5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -207,6 +207,12 @@ All notable changes to Gotcha are documented here.
the ball is on or off instead of only once it is already too late.
### Fixed
+- **The context meter keeps up with the chat** (#71). The row of bars under a
+ chat's title, showing how much of the model's context the chat has used, only
+ moved when you left the chat and came back; during a reply it stayed where it
+ was. It now moves after every step of a reply, and after the chat is
+ compacted. The bar being filled now fills gradually rather than in whole
+ twelfths, so a short exchange still shows up.
- **Termux commands work when Termux is closed.** Running a command in Termux
failed with "app is in background" whenever Termux wasn't already running
(after swiping it away, say), even with Gotcha open on screen, and the
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 3c90e24..8134ee1 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -471,7 +471,9 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
// Publish live to the drawer so the running session's row updates in
// the same frame, without waiting for the disk save at end-of-round.
_liveTokenBySession.update { it + (engineId to totalTokens) }
- if (viewingEngineSession()) updateContextUsage()
+ // Pass the new count explicitly: updateContextUsage() re-applies the
+ // count already on screen, so the meter would never move mid-run (#71).
+ if (viewingEngineSession()) applyContextUsage(totalTokens)
// Best-effort disk write so a crash mid-run doesn't lose the count.
viewModelScope.launch { agentEngine.saveCurrentSession() }
}
diff --git a/app/src/main/java/com/gotcha/ui/ChatComponents.kt b/app/src/main/java/com/gotcha/ui/ChatComponents.kt
index b26bca7..e86b139 100644
--- a/app/src/main/java/com/gotcha/ui/ChatComponents.kt
+++ b/app/src/main/java/com/gotcha/ui/ChatComponents.kt
@@ -4,14 +4,17 @@ import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloat
+import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
+import androidx.compose.animation.core.snap
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
@@ -30,7 +33,6 @@ import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.dp
import com.gotcha.ui.theme.LocalAnimationsEnabled
import com.gotcha.ui.theme.WarningAmber
-import kotlin.math.roundToInt
/**
* Pieces the chat screen is assembled from. They exist here rather than inline
@@ -41,6 +43,7 @@ import kotlin.math.roundToInt
private const val METER_SEGMENTS = 12
private const val METER_WARN_AT = 0.75f
private const val METER_CRITICAL_AT = 0.9f
+private const val METER_ANIM_MS = 400
/**
* How much of the context window this chat has spent.
@@ -49,11 +52,19 @@ private const val METER_CRITICAL_AT = 0.9f
* full", which nobody acts on, while segments answer "how many turns do I have
* left", which is the question actually being asked. Colour carries the same
* information a second way, for the three-quarters of it that matters.
+ *
+ * The segment in progress fills in proportion, so a round that spends less
+ * than a whole segment still visibly moves the meter (#71).
*/
@Composable
fun ContextMeter(fraction: Float, modifier: Modifier = Modifier) {
val clamped = fraction.coerceIn(0f, 1f)
- val filled = (clamped * METER_SEGMENTS).roundToInt()
+ val shown by animateFloatAsState(
+ targetValue = clamped,
+ animationSpec = if (LocalAnimationsEnabled.current) tween(METER_ANIM_MS) else snap(),
+ label = "contextMeter"
+ )
+ val filled = shown * METER_SEGMENTS
val colors = MaterialTheme.colorScheme
val fillColor = when {
clamped >= METER_CRITICAL_AT -> colors.error
@@ -69,13 +80,23 @@ fun ContextMeter(fraction: Float, modifier: Modifier = Modifier) {
horizontalArrangement = Arrangement.spacedBy(3.dp)
) {
repeat(METER_SEGMENTS) { index ->
+ val segmentFill = (filled - index).coerceIn(0f, 1f)
Box(
Modifier
.weight(1f)
.height(3.dp)
.clip(RoundedCornerShape(2.dp))
- .background(if (index < filled) fillColor else emptyColor)
- )
+ .background(emptyColor)
+ ) {
+ if (segmentFill > 0f) {
+ Box(
+ Modifier
+ .fillMaxWidth(segmentFill)
+ .fillMaxHeight()
+ .background(fillColor)
+ )
+ }
+ }
}
}
}
diff --git a/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt b/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt
index a54ad62..f5d6343 100644
--- a/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt
+++ b/app/src/test/java/com/gotcha/agent/ChatViewModelContextUsageTest.kt
@@ -158,4 +158,35 @@ class ChatViewModelContextUsageTest {
assertEquals(555, viewModel.liveTokenBySession.value[engineId])
}
+
+ /**
+ * Regression test for #71: onTokenCount re-applied the count already on
+ * screen, so the meter stayed put for the whole run and only caught up
+ * when the chat was reopened.
+ */
+ @Test
+ fun `onTokenCount moves the meter of the viewed engine session`() {
+ viewModel.onTokenCount(7_000)
+
+ val limit = settingsRepository.load().maxContextTokens.toFloat()
+ assertEquals(7_000, viewModel.uiState.value.tokenCount)
+ assertEquals(7_000f / limit, viewModel.uiState.value.contextUsagePercent, 0.0001f)
+ }
+
+ @Test
+ fun `onTokenCount leaves the meter alone while another chat is viewed`() {
+ seedUiTokenCount(3_000)
+ val stateField = ChatViewModel::class.java.getDeclaredField("_uiState")
+ .apply { isAccessible = true }
+
+ @Suppress("UNCHECKED_CAST")
+ val flow = stateField.get(viewModel) as kotlinx.coroutines.flow.MutableStateFlow<ChatUiState>
+ flow.value = flow.value.copy(activeSessionId = "session-B")
+ val percent = viewModel.uiState.value.contextUsagePercent
+
+ viewModel.onTokenCount(9_999)
+
+ assertEquals(3_000, viewModel.uiState.value.tokenCount)
+ assertEquals(percent, viewModel.uiState.value.contextUsagePercent, 0.0001f)
+ }
}