android: one chat poller owned by the ViewModel, gated auto-scroll, Enter-to-send #70
Loading…
Reference in a new issue
No description provided.
Delete branch "wt/t_7db68274"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-ups deferred by #67 (the composer/IME work). All three lived in the same two files, so they ship together.
1. One poll loop, owned by the ViewModel
ChatScreenhad aLaunchedEffect(Unit)that fetchedmaxAttemptsonce and then calledloadMessages/loadOutboxevery 2s forever, fed by three suspend lambdas created inMainActivity— one SDK poller per chat composition.The cadence now lives in
WireViewModel:openChat(pubkeyHex)starts one 2s loop inviewModelScope; re-entering the peer while its poller is alive is a no-op, so walking the nav stack cannot stack a second loop.closeChat(pubkeyHex)stops it (ignored if another peer has taken over), andlock()stops it before the node goes away.ChatState(messages, outbox, maxAttempts, error)is exposed as aStateFlowand collected withcollectAsStateWithLifecycle().Behaviour preserved: 2s cadence,
maxAttemptsread once per open chat, messages and outbox refreshed together, load failures surfaced in the same error slot (sticky on a successful tick, cleared by a send), andSendState.QUEUEDstill producing the same notice string. No new dependency.No polling
LaunchedEffectremains inChatScreen.kt— the two effects left are the send effect (keyed onpending) and the auto-scroll effect.2. Auto-scroll no longer steals the reader's place
The tail is followed only when the reader is already there:
lastVisible >= n - 2(n - 2rather than!listState.canScrollForward, because the append has already made the list scrollable by the time the effect runs). The user's own send always scrolls their message into view. The first fill keeps its one jump to the bottom (scrollToItem, no animation).Known and accepted, now documented in a comment instead of silently changed: the effect is keyed on the message count, so a delivery-only change (same size, different marker) does not re-run it. That has always been the behaviour and it is the safe one — the list never moves for a marker.
3. Composer: Enter-to-send, deliberately
Decision: keep
ImeAction.SendwithmaxLines = 4. The IME's action key sends; the soft keyboard cannot insert a manual newline.maxLinesis a wrap limit (a long message grows to four lines, then scrolls inside the field), not a newline affordance, so the two are not in conflict. Chat convention wins; the Send button is on the same row. Nothing is unreachable: a hardware keyboard's Enter never goes through the IME action, so it still inserts a real newline in the field. This is written in a code comment at the field.Verification
Build and unit tests (Gradle 8.14.5,
PATH=$HOME/.cargo/bin:$PATH,CARGO_TARGET_DIR=/var/tmp/ow-t_7db68274):The assembled APK carries both ABI slices (
lib/arm64-v8a/libonionwire_sdk.so,lib/x86_64/libonionwire_sdk.so); the temporary poll-counter log used for the measurement below is not in the final dex (grep OnionWirePoll classes*.dex→ nothing).Emulator run: AVD
owT7db6(x86_64, API 35), debug APK built from the exact bytes in this PR (sha1of the three files: MainActivity.kt9a9235a9…, WireViewModel.kt47217cc4…, ChatScreen.kt11972d24…— unchanged by the rebase ontomain). Peer reached with the self-invite trick (Invite → Copy → Add → paste → Verify → Approve & add), so sends complete against the running node.(a) One poller
Chat open, logcat cleared, 10.0s window, one temporary
Log.d("OnionWirePoll", "tick")in the poller:5 ticks in 10s, all from one pid/thread — not ~10. Navigating away and back does not stack a second poller:
(b) Auto-scroll gating
Inbound message while scrolled up in history — the list does not move. The incoming message was produced by sending while the device was offline (
svc wifi/data disable), which queues it (queued (try 1/8, retry 29s)), then scrolling to the top and bringing the network back. The poll tick'snis the message count, so its jump timestamps the arrival:The visible items are indices 0–5 (
hello-self-1is the oldest message), i.e. the list was at the top andn - 2 = 24, so the gate correctly declined to scroll. Identical bounds before and after the arrival.Sending your own message still scrolls — from the same scrolled-up state:
(c) Composer
With the composer focused (
imeOptions=2000004, actionName=SEND→IME_ACTION_SEND,inputType=2c001= text + sentences + multi-line):The composer sits above the keyboard:
EditText (21,1328)-(829,1496),Send (913,1407)-(996,1460), screen 1080x2400. Gboard relabels its bottom-right key to the send icon (blue circular key, white right-pointing triangle, centre ~ (995, 2270)); tapping it sent the message and left the field empty with the IME still up:Hardware Enter inserts a real newline and does not send (tick
nunchanged at 32, no new row, field grows to two lines):Out of scope, flagged for a separate rust-dev card
While measuring, a queued message that delivered on retry appended two inbound copies for one send (
n26 -> 28, twoqueued-arrival-1inbound rows and one✓✓outbound row). Same forgate-probe-1. That is core delivery behaviour, not the Android UI, so it is not touched here — it is worth a rust-dev look.CI
No job covers this PR:
.forgejo/workflows/ci.ymlallowlistssrc/**,crates/**,tests/**,Cargo.tomland itself, so an android-only change runs no job at all (by design — the comment says a missing run is not a merge trap), andandroid-release.ymlis tag-driven. The verification is therefore the build/test output and the emulator evidence above, not a check status.