Skip to content

Commit 50ce5de

Browse files
IWhatsskillSolvely-Colinsteipete
authored
fix(android): improve Wear thread follow behavior (#111459)
* improve(android): move Wear agent, session, and model pickers to Home * fix(android): improve Wear thread follow behavior * fix(android): stabilize Wear thread follow lifecycle * fix(android): keep streamed Wear tail visible Co-authored-by: JARVIS-Glasses <[email protected]> --------- Co-authored-by: Colin <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 8e0c503 commit 50ce5de

3 files changed

Lines changed: 296 additions & 7 deletions

File tree

‎apps/android/wear/src/main/java/ai/openclaw/wear/WearScreens.kt‎

Lines changed: 180 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import androidx.compose.runtime.mutableStateOf
4444
import androidx.compose.runtime.remember
4545
import androidx.compose.runtime.rememberCoroutineScope
4646
import androidx.compose.runtime.setValue
47+
import androidx.compose.runtime.snapshotFlow
4748
import androidx.compose.ui.Alignment
4849
import androidx.compose.ui.Modifier
4950
import androidx.compose.ui.geometry.CornerRadius
@@ -73,6 +74,7 @@ import androidx.wear.compose.material3.HorizontalPagerScaffold
7374
import androidx.wear.compose.material3.ScreenScaffold
7475
import androidx.wear.compose.material3.Text
7576
import kotlinx.coroutines.delay
77+
import kotlinx.coroutines.flow.collect
7678
import kotlinx.coroutines.isActive
7779
import kotlinx.coroutines.launch
7880

@@ -430,6 +432,8 @@ private fun VoicePage(
430432
else ->
431433
ThreadVoiceMode(
432434
conversation = realtimeTalk.conversation,
435+
thinking =
436+
realtimeThinkingOverride || realtimeTalk.status == WearRealtimeTalkStatus.THINKING,
433437
realtimeActive = realtimeTalk.active || realtimeCapturing,
434438
actionBusy = actionBusy,
435439
inputEnabled = inputEnabled,
@@ -695,6 +699,7 @@ private fun VoiceGestureLabel(
695699
@Composable
696700
private fun ThreadVoiceMode(
697701
conversation: List<WearRealtimeTalkEntry>,
702+
thinking: Boolean,
698703
realtimeActive: Boolean,
699704
actionBusy: Boolean,
700705
inputEnabled: Boolean,
@@ -703,6 +708,40 @@ private fun ThreadVoiceMode(
703708
) {
704709
val colors = OpenClawWearTheme.colors
705710
val listState = rememberTransformingLazyColumnState()
711+
val coroutineScope = rememberCoroutineScope()
712+
val visibleConversation = conversation.takeLast(VISIBLE_REALTIME_ENTRY_COUNT)
713+
val contentRevision = wearThreadContentRevision(visibleConversation, thinking)
714+
val latestAnchorIndex = wearThreadLatestAnchorIndex(visibleConversation.size, thinking)
715+
var followState by remember { mutableStateOf(WearThreadFollowState()) }
716+
717+
LaunchedEffect(listState) {
718+
snapshotFlow {
719+
WearThreadViewport(
720+
atLatest = !listState.canScrollForward,
721+
scrollingBackward = listState.isScrollInProgress && listState.lastScrolledBackward,
722+
)
723+
}.collect { viewport ->
724+
followState =
725+
nextWearThreadFollowForViewport(
726+
state = followState,
727+
atLatest = viewport.atLatest,
728+
scrollingBackward = viewport.scrollingBackward,
729+
)
730+
}
731+
}
732+
LaunchedEffect(realtimeActive, contentRevision) {
733+
val update =
734+
nextWearThreadFollowForContent(
735+
state = followState,
736+
contentRevision = contentRevision,
737+
realtimeActive = realtimeActive,
738+
)
739+
followState = update.state
740+
if (update.scrollToLatest && latestAnchorIndex >= 0) {
741+
listState.requestScrollToItem(latestAnchorIndex)
742+
}
743+
}
744+
706745
Box(modifier = Modifier.fillMaxSize()) {
707746
TransformingLazyColumn(
708747
modifier =
@@ -714,7 +753,7 @@ private fun ThreadVoiceMode(
714753
horizontalAlignment = Alignment.CenterHorizontally,
715754
verticalArrangement = Arrangement.spacedBy(7.dp),
716755
) {
717-
if (conversation.isEmpty()) {
756+
if (visibleConversation.isEmpty() && !thinking) {
718757
item {
719758
Text(
720759
text = stringResource(R.string.no_live_conversation),
@@ -726,13 +765,46 @@ private fun ThreadVoiceMode(
726765
)
727766
}
728767
} else {
729-
conversation
730-
.takeLast(VISIBLE_REALTIME_ENTRY_COUNT)
731-
.forEach { entry ->
732-
item(key = entry.id) {
733-
RealtimeTalkBubble(entry)
734-
}
768+
visibleConversation.forEach { entry ->
769+
item(key = entry.id) {
770+
RealtimeTalkBubble(entry)
771+
}
772+
}
773+
if (thinking) {
774+
item(key = "realtime-thinking") {
775+
WearThreadThinking()
735776
}
777+
}
778+
// Follow a trailing anchor: centering a growing bubble can hide its newly streamed tail.
779+
item(key = "realtime-thread-end") {
780+
Spacer(modifier = Modifier.height(1.dp))
781+
}
782+
}
783+
}
784+
if (followState.hasNewContent) {
785+
Box(
786+
modifier =
787+
Modifier
788+
.align(Alignment.BottomCenter)
789+
.padding(bottom = 44.dp)
790+
.background(colors.voiceAccentSoft, RoundedCornerShape(14.dp))
791+
.border(1.dp, colors.voiceAccent, RoundedCornerShape(14.dp))
792+
.clickable(role = Role.Button) {
793+
followState = wearThreadFollowLatest(followState)
794+
if (latestAnchorIndex >= 0) {
795+
coroutineScope.launch {
796+
listState.animateScrollToItem(latestAnchorIndex)
797+
}
798+
}
799+
}.padding(horizontal = 10.dp, vertical = 5.dp),
800+
contentAlignment = Alignment.Center,
801+
) {
802+
Text(
803+
text = "${stringResource(R.string.new_messages)} ↓",
804+
color = colors.voiceAccent,
805+
fontSize = 9.sp,
806+
fontWeight = FontWeight.Bold,
807+
)
736808
}
737809
}
738810
Row(
@@ -796,6 +868,107 @@ private fun ThreadVoiceMode(
796868
}
797869
}
798870

871+
@Composable
872+
private fun WearThreadThinking() {
873+
val colors = OpenClawWearTheme.colors
874+
Box(
875+
modifier =
876+
Modifier
877+
.fillMaxWidth()
878+
.padding(start = 12.dp, end = 28.dp)
879+
.background(colors.surfaceRaised, RoundedCornerShape(14.dp))
880+
.border(1.dp, colors.borderStrong, RoundedCornerShape(14.dp))
881+
.padding(horizontal = 12.dp, vertical = 8.dp),
882+
) {
883+
Text(
884+
text = "${stringResource(R.string.thinking)}…",
885+
color = colors.textMuted,
886+
fontSize = 10.sp,
887+
fontWeight = FontWeight.SemiBold,
888+
)
889+
}
890+
}
891+
892+
internal data class WearThreadContentRevision(
893+
val entryCount: Int,
894+
val latestEntryId: String?,
895+
val latestText: String?,
896+
val latestStreaming: Boolean,
897+
val thinking: Boolean,
898+
)
899+
900+
internal data class WearThreadFollowState(
901+
val contentRevision: WearThreadContentRevision? = null,
902+
val followingLatest: Boolean = true,
903+
val hasNewContent: Boolean = false,
904+
)
905+
906+
internal data class WearThreadFollowUpdate(
907+
val state: WearThreadFollowState,
908+
val scrollToLatest: Boolean,
909+
)
910+
911+
private data class WearThreadViewport(
912+
val atLatest: Boolean,
913+
val scrollingBackward: Boolean,
914+
)
915+
916+
internal fun wearThreadContentRevision(
917+
conversation: List<WearRealtimeTalkEntry>,
918+
thinking: Boolean,
919+
): WearThreadContentRevision {
920+
val latest = conversation.lastOrNull()
921+
return WearThreadContentRevision(
922+
entryCount = conversation.size,
923+
latestEntryId = latest?.id,
924+
latestText = latest?.text,
925+
latestStreaming = latest?.streaming == true,
926+
thinking = thinking,
927+
)
928+
}
929+
930+
internal fun wearThreadLatestAnchorIndex(
931+
entryCount: Int,
932+
thinking: Boolean,
933+
): Int = if (entryCount == 0 && !thinking) -1 else entryCount + if (thinking) 1 else 0
934+
935+
internal fun nextWearThreadFollowForContent(
936+
state: WearThreadFollowState,
937+
contentRevision: WearThreadContentRevision,
938+
realtimeActive: Boolean = true,
939+
): WearThreadFollowUpdate {
940+
if (!realtimeActive) {
941+
return WearThreadFollowUpdate(
942+
state = WearThreadFollowState(),
943+
scrollToLatest = false,
944+
)
945+
}
946+
if (state.contentRevision == contentRevision) {
947+
return WearThreadFollowUpdate(state = state, scrollToLatest = false)
948+
}
949+
return WearThreadFollowUpdate(
950+
state =
951+
state.copy(
952+
contentRevision = contentRevision,
953+
hasNewContent = !state.followingLatest,
954+
),
955+
scrollToLatest = state.followingLatest,
956+
)
957+
}
958+
959+
internal fun nextWearThreadFollowForViewport(
960+
state: WearThreadFollowState,
961+
atLatest: Boolean,
962+
scrollingBackward: Boolean,
963+
): WearThreadFollowState =
964+
when {
965+
atLatest -> state.copy(followingLatest = true, hasNewContent = false)
966+
scrollingBackward -> state.copy(followingLatest = false)
967+
else -> state
968+
}
969+
970+
internal fun wearThreadFollowLatest(state: WearThreadFollowState): WearThreadFollowState = state.copy(followingLatest = true, hasNewContent = false)
971+
799972
@Composable
800973
private fun VoiceOrb(
801974
accent: Color,

‎apps/android/wear/src/main/res/values/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<string name="tap">Tap</string>
1818
<string name="double_tap">Double tap</string>
1919
<string name="no_live_conversation">Start Live to see the conversation here.</string>
20+
<string name="new_messages">New</string>
2021
<string name="type">Type</string>
2122
<string name="message">Message</string>
2223
<string name="message_agent">Message agent</string>

‎apps/android/wear/src/test/java/ai/openclaw/wear/MainActivityTest.kt‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import ai.openclaw.wear.shared.WearRealtimeTalkEntry
44
import ai.openclaw.wear.shared.WearRealtimeTalkRole
55
import ai.openclaw.wear.shared.WearRealtimeTalkSnapshot
66
import org.junit.Assert.assertEquals
7+
import org.junit.Assert.assertFalse
78
import org.junit.Assert.assertNull
9+
import org.junit.Assert.assertTrue
810
import org.junit.Test
911

1012
class MainActivityTest {
@@ -53,6 +55,119 @@ class MainActivityTest {
5355
)
5456
}
5557

58+
@Test
59+
fun threadFollowKeepsStreamingContentVisibleAtLatest() {
60+
val first =
61+
nextWearThreadFollowForContent(
62+
state = WearThreadFollowState(),
63+
contentRevision = threadRevision(text = "Hel", streaming = true),
64+
)
65+
val continued =
66+
nextWearThreadFollowForContent(
67+
state = first.state,
68+
contentRevision = threadRevision(text = "Hello", streaming = true),
69+
)
70+
71+
assertTrue(first.scrollToLatest)
72+
assertTrue(continued.scrollToLatest)
73+
assertTrue(continued.state.followingLatest)
74+
assertFalse(continued.state.hasNewContent)
75+
}
76+
77+
@Test
78+
fun threadFollowTargetsTrailingAnchorAfterLatestContent() {
79+
assertEquals(-1, wearThreadLatestAnchorIndex(entryCount = 0, thinking = false))
80+
assertEquals(1, wearThreadLatestAnchorIndex(entryCount = 1, thinking = false))
81+
assertEquals(3, wearThreadLatestAnchorIndex(entryCount = 2, thinking = true))
82+
}
83+
84+
@Test
85+
fun threadFollowPreservesManualScrollUntilLatestIsRequested() {
86+
val initial =
87+
nextWearThreadFollowForContent(
88+
state = WearThreadFollowState(),
89+
contentRevision = threadRevision(text = "First", streaming = false),
90+
)
91+
val scrolledBack =
92+
nextWearThreadFollowForViewport(
93+
state = initial.state,
94+
atLatest = false,
95+
scrollingBackward = true,
96+
)
97+
val newContent =
98+
nextWearThreadFollowForContent(
99+
state = scrolledBack,
100+
contentRevision = threadRevision(text = "Second", streaming = false),
101+
)
102+
103+
assertFalse(newContent.scrollToLatest)
104+
assertFalse(newContent.state.followingLatest)
105+
assertTrue(newContent.state.hasNewContent)
106+
107+
val latest = wearThreadFollowLatest(newContent.state)
108+
assertTrue(latest.followingLatest)
109+
assertFalse(latest.hasNewContent)
110+
}
111+
112+
@Test
113+
fun threadFollowClearsNewContentWhenUserScrollsToLatest() {
114+
val away =
115+
WearThreadFollowState(
116+
followingLatest = false,
117+
hasNewContent = true,
118+
)
119+
120+
val latest =
121+
nextWearThreadFollowForViewport(
122+
state = away,
123+
atLatest = true,
124+
scrollingBackward = false,
125+
)
126+
127+
assertTrue(latest.followingLatest)
128+
assertFalse(latest.hasNewContent)
129+
}
130+
131+
@Test
132+
fun threadFollowResetsWhenRealtimeStops() {
133+
val revision = threadRevision(text = "Old", streaming = false)
134+
val away =
135+
WearThreadFollowState(
136+
contentRevision = revision,
137+
followingLatest = false,
138+
hasNewContent = true,
139+
)
140+
val stopped =
141+
nextWearThreadFollowForContent(
142+
state = away,
143+
contentRevision = revision,
144+
realtimeActive = false,
145+
)
146+
147+
assertFalse(stopped.scrollToLatest)
148+
assertTrue(stopped.state.followingLatest)
149+
assertFalse(stopped.state.hasNewContent)
150+
151+
val restarted =
152+
nextWearThreadFollowForContent(
153+
state = stopped.state,
154+
contentRevision = revision,
155+
)
156+
assertTrue(restarted.scrollToLatest)
157+
}
158+
159+
private fun threadRevision(
160+
text: String,
161+
streaming: Boolean,
162+
): WearThreadContentRevision =
163+
WearThreadContentRevision(
164+
entryCount = 1,
165+
latestEntryId = "entry-1",
166+
latestText = text,
167+
latestStreaming = streaming,
168+
thinking = false,
169+
)
170+
56171
private fun realtimeSnapshot(entryStreaming: Boolean): WearConversationSnapshot =
57172
WearConversationSnapshot(
58173
gatewayState = WearGatewayState.CONNECTED,

0 commit comments

Comments
 (0)