android: one chat poller owned by the ViewModel, gated auto-scroll, Enter-to-send #70

Merged
sirius merged 2 commits from wt/t_7db68274 into main 2026-09-14 16:01:34 +00:00
Owner

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

ChatScreen had a LaunchedEffect(Unit) that fetched maxAttempts once and then called loadMessages/loadOutbox every 2s forever, fed by three suspend lambdas created in MainActivity — one SDK poller per chat composition.

The cadence now lives in WireViewModel:

  • openChat(pubkeyHex) starts one 2s loop in viewModelScope; 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), and lock() stops it before the node goes away.
  • ChatState(messages, outbox, maxAttempts, error) is exposed as a StateFlow and collected with collectAsStateWithLifecycle().

Behaviour preserved: 2s cadence, maxAttempts read 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), and SendState.QUEUED still producing the same notice string. No new dependency.

No polling LaunchedEffect remains in ChatScreen.kt — the two effects left are the send effect (keyed on pending) 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 - 2 rather 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.Send with maxLines = 4. The IME's action key sends; the soft keyboard cannot insert a manual newline. maxLines is 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):

$ ./gradlew :app:assembleDebug -Ponionwire.abis=arm64-v8a,x86_64
BUILD SUCCESSFUL in 1s
61 actionable tasks: 3 executed, 3 from cache, 55 up-to-date

$ ./gradlew :app:testDebugUnitTest --rerun
BUILD SUCCESSFUL in 1s
TESTS: 61 tests, 0 failures, 0 errors, 0 skipped

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 (sha1 of the three files: MainActivity.kt 9a9235a9…, WireViewModel.kt 47217cc4…, ChatScreen.kt 11972d24… — unchanged by the rebase onto main). 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:

START 02:19:54.073
END   02:20:04.074
09-14 02:19:54.629 19691 19691 D OnionWirePoll: tick
09-14 02:19:56.633 19691 19691 D OnionWirePoll: tick
09-14 02:19:58.636 19691 19691 D OnionWirePoll: tick
09-14 02:20:00.640 19691 19691 D OnionWirePoll: tick
09-14 02:20:02.644 19691 19691 D OnionWirePoll: tick
COUNT: 5

5 ticks in 10s, all from one pid/thread — not ~10. Navigating away and back does not stack a second poller:

back to roster: on roster: True
--- ticks while OFF the chat screen (10s) ---
0                       # the poller stops with the screen
--- re-entered the chat, 10s window ---
START 02:50:28.995
END   02:50:38.997
09-14 02:50:28.523 21677 21677 D OnionWirePoll: tick n=32 outbox=0
09-14 02:50:30.527 21677 21677 D OnionWirePoll: tick n=32 outbox=0
09-14 02:50:32.530 21677 21677 D OnionWirePoll: tick n=32 outbox=0
09-14 02:50:34.534 21677 21677 D OnionWirePoll: tick n=32 outbox=0
09-14 02:50:36.537 21677 21677 D OnionWirePoll: tick n=32 outbox=0
09-14 02:50:38.541 21677 21677 D OnionWirePoll: tick n=32 outbox=0
COUNT: 6

(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's n is the message count, so its jump timestamps the arrival:

[2s]  tick=n=26 outbox=1
       hello-self-1 (837, 497, 1022, 550)
       hello-self-1 (58, 656, 243, 709)
       history-02   (856, 767, 1022, 820)
       history-02   (58, 926, 224, 979)
       history-03   (856, 1037, 1022, 1090)
[10s] tick=n=26 outbox=1   (identical bounds)
[18s] tick=n=26 outbox=1   (identical bounds)
[26s] tick=n=28 outbox=0   (delivery landed)
       hello-self-1 (837, 497, 1022, 550)
       hello-self-1 (58, 656, 243, 709)
       history-02   (856, 767, 1022, 820)
       history-02   (58, 926, 224, 979)
       history-03   (856, 1037, 1022, 1090)

The visible items are indices 0–5 (hello-self-1 is the oldest message), i.e. the list was at the top and n - 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:

VISIBLE BEFORE own send (list at top):
    hello-self-1 (837, 497, 1022, 550)
    hello-self-1 (58, 656, 243, 709)
    history-02   (856, 767, 1022, 820)
    history-02   (58, 926, 224, 979)
    history-03   (856, 1037, 1022, 1090)
send: True
VISIBLE AFTER own send:
    queued-arrival-1 (58, 444, 324, 492)
    queued-arrival-1 (58, 550, 324, 603)
    gate-probe-1   (812, 661, 1022, 714)
    gate-probe-1   (58, 820, 268, 873)
    gate-probe-1   (58, 931, 268, 984)
    own-scroll-probe (744, 1042, 1022, 1095)
    own-scroll-probe (58, 1201, 336, 1254)

(c) Composer

With the composer focused (imeOptions=2000004, actionName=SEND → IME_ACTION_SEND, inputType=2c001 = text + sentences + multi-line):

$ adb shell dumpsys input_method | grep mInputShown
  mInputShown=true

GoogleInputMethodService.onStartInput(): onStartInput(EditorInfo{... inputType=2c001,
  inputTypeString=Normal, ... imeOptions=2000004, actionName=SEND, ...})

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:

composer-probe rows after action-key tap: [('composer-probe', (749, 1042, 1022, 1095)),
                                           ('composer-probe', (58, 1201, 331, 1254))]
composer field content: ["''"]
ime still up: True

Hardware Enter inserts a real newline and does not send (tick n unchanged at 32, no new row, field grows to two lines):

tick before: n=32 outbox=0
field content: ["'line-one
line-two'"]
tick after: n=32 outbox=0
rows with line-one: [('line-one
line-tw', (21, 1265, 829, 1496))]

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 (n 26 -> 28, two queued-arrival-1 inbound rows and one ✓✓ outbound row). Same for gate-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.yml allowlists src/**, crates/**, tests/**, Cargo.toml and itself, so an android-only change runs no job at all (by design — the comment says a missing run is not a merge trap), and android-release.yml is tag-driven. The verification is therefore the build/test output and the emulator evidence above, not a check status.

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 `ChatScreen` had a `LaunchedEffect(Unit)` that fetched `maxAttempts` once and then called `loadMessages`/`loadOutbox` every 2s forever, fed by three suspend lambdas created in `MainActivity` — one SDK poller per chat composition. The cadence now lives in `WireViewModel`: * `openChat(pubkeyHex)` starts **one** 2s loop in `viewModelScope`; 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), and `lock()` stops it before the node goes away. * `ChatState(messages, outbox, maxAttempts, error)` is exposed as a `StateFlow` and collected with `collectAsStateWithLifecycle()`. Behaviour preserved: 2s cadence, `maxAttempts` read 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), and `SendState.QUEUED` still producing the same notice string. No new dependency. **No polling `LaunchedEffect` remains in `ChatScreen.kt`** — the two effects left are the send effect (keyed on `pending`) 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 - 2` rather 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.Send` with `maxLines = 4`.** The IME's action key sends; the soft keyboard cannot insert a manual newline. `maxLines` is 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`): ``` $ ./gradlew :app:assembleDebug -Ponionwire.abis=arm64-v8a,x86_64 BUILD SUCCESSFUL in 1s 61 actionable tasks: 3 executed, 3 from cache, 55 up-to-date $ ./gradlew :app:testDebugUnitTest --rerun BUILD SUCCESSFUL in 1s TESTS: 61 tests, 0 failures, 0 errors, 0 skipped ``` 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** (`sha1` of the three files: MainActivity.kt `9a9235a9…`, WireViewModel.kt `47217cc4…`, ChatScreen.kt `11972d24…` — unchanged by the rebase onto `main`). 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: ``` START 02:19:54.073 END 02:20:04.074 09-14 02:19:54.629 19691 19691 D OnionWirePoll: tick 09-14 02:19:56.633 19691 19691 D OnionWirePoll: tick 09-14 02:19:58.636 19691 19691 D OnionWirePoll: tick 09-14 02:20:00.640 19691 19691 D OnionWirePoll: tick 09-14 02:20:02.644 19691 19691 D OnionWirePoll: tick COUNT: 5 ``` 5 ticks in 10s, all from one pid/thread — not ~10. Navigating away and back does not stack a second poller: ``` back to roster: on roster: True --- ticks while OFF the chat screen (10s) --- 0 # the poller stops with the screen --- re-entered the chat, 10s window --- START 02:50:28.995 END 02:50:38.997 09-14 02:50:28.523 21677 21677 D OnionWirePoll: tick n=32 outbox=0 09-14 02:50:30.527 21677 21677 D OnionWirePoll: tick n=32 outbox=0 09-14 02:50:32.530 21677 21677 D OnionWirePoll: tick n=32 outbox=0 09-14 02:50:34.534 21677 21677 D OnionWirePoll: tick n=32 outbox=0 09-14 02:50:36.537 21677 21677 D OnionWirePoll: tick n=32 outbox=0 09-14 02:50:38.541 21677 21677 D OnionWirePoll: tick n=32 outbox=0 COUNT: 6 ``` ### (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's `n` is the message count, so its jump timestamps the arrival: ``` [2s] tick=n=26 outbox=1 hello-self-1 (837, 497, 1022, 550) hello-self-1 (58, 656, 243, 709) history-02 (856, 767, 1022, 820) history-02 (58, 926, 224, 979) history-03 (856, 1037, 1022, 1090) [10s] tick=n=26 outbox=1 (identical bounds) [18s] tick=n=26 outbox=1 (identical bounds) [26s] tick=n=28 outbox=0 (delivery landed) hello-self-1 (837, 497, 1022, 550) hello-self-1 (58, 656, 243, 709) history-02 (856, 767, 1022, 820) history-02 (58, 926, 224, 979) history-03 (856, 1037, 1022, 1090) ``` The visible items are indices 0–5 (`hello-self-1` is the oldest message), i.e. the list was at the top and `n - 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:** ``` VISIBLE BEFORE own send (list at top): hello-self-1 (837, 497, 1022, 550) hello-self-1 (58, 656, 243, 709) history-02 (856, 767, 1022, 820) history-02 (58, 926, 224, 979) history-03 (856, 1037, 1022, 1090) send: True VISIBLE AFTER own send: queued-arrival-1 (58, 444, 324, 492) queued-arrival-1 (58, 550, 324, 603) gate-probe-1 (812, 661, 1022, 714) gate-probe-1 (58, 820, 268, 873) gate-probe-1 (58, 931, 268, 984) own-scroll-probe (744, 1042, 1022, 1095) own-scroll-probe (58, 1201, 336, 1254) ``` ### (c) Composer With the composer focused (`imeOptions=2000004, actionName=SEND` → `IME_ACTION_SEND`, `inputType=2c001` = text + sentences + multi-line): ``` $ adb shell dumpsys input_method | grep mInputShown mInputShown=true GoogleInputMethodService.onStartInput(): onStartInput(EditorInfo{... inputType=2c001, inputTypeString=Normal, ... imeOptions=2000004, actionName=SEND, ...}) ``` 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: ``` composer-probe rows after action-key tap: [('composer-probe', (749, 1042, 1022, 1095)), ('composer-probe', (58, 1201, 331, 1254))] composer field content: ["''"] ime still up: True ``` Hardware Enter inserts a real newline and does **not** send (tick `n` unchanged at 32, no new row, field grows to two lines): ``` tick before: n=32 outbox=0 field content: ["'line-one
line-two'"] tick after: n=32 outbox=0 rows with line-one: [('line-one
line-tw', (21, 1265, 829, 1496))] ``` ## 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 (`n` 26 -> 28, two `queued-arrival-1` inbound rows and one `✓✓` outbound row). Same for `gate-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.yml` allowlists `src/**`, `crates/**`, `tests/**`, `Cargo.toml` and itself, so an android-only change runs **no job at all** (by design — the comment says a missing run is not a merge trap), and `android-release.yml` is tag-driven. The verification is therefore the build/test output and the emulator evidence above, not a check status.
The chat screen owned a LaunchedEffect(Unit) that fetched maxAttempts once and
then called loadMessages/loadOutbox every 2s forever, fed by three suspend
lambdas from MainActivity — one SDK poller per composition. The cadence now
lives in WireViewModel: openChat(peer) starts one 2s loop in viewModelScope
(closeChat stops it, re-entering the same peer is a no-op), exposed as
ChatState(messages, outbox, maxAttempts, error) and collected with
collectAsStateWithLifecycle. Same 2s cadence, maxAttempts read once, messages
and outbox refreshed together, load failures sticky on ChatState.error, and
the QUEUED notice unchanged; a send clears the load error.

Auto-scroll no longer steals the reader's place: the tail is followed only when
the last visible index is >= count - 2 (or the list has not filled yet), while
the user's own send still scrolls their message into view. The first fill keeps
its one jump to the bottom. A delivery-only change (same size) still does not
re-run the effect — documented in a comment rather than changed.

The composer keeps ImeAction.Send with maxLines = 4, documented as a deliberate
wrap limit rather than a newline affordance: the action key sends (hardware
Enter still inserts a newline inside the field).

No polling LaunchedEffect remains in ChatScreen.kt.
sirius merged commit ce463e8568 into main 2026-09-14 16:01:34 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/onionwire!70
No description provided.