feat(android): chat row renders queued (try 3/8, retry 45s) from the SDK's outbox rows
Consumes the SDK surface from PR #58 (`Wire.outbox(pubkeyHex)` → `OutboxRow { messageId, status, attempts, nextAttemptAt, lastError }` and `outboxMaxAttempts()`): a queued chat row now says *why* it is still queued instead of a flat `queued`. * `OutboxDetail.kt` — `retryInText` mirrors `src/tui.rs::retry_in_text` (`<= 0` → `retry now`; `< 60` → `retry {n}s`; else `retry {ceil-minutes}m`), and `outboxDetails` joins the SDK's rows onto the chat rows. The denominator is the core's `outboxMaxAttempts()`/`Rust` budget, fetched once — never 8. * A terminal `"failed"` row keeps the marker's own `failed: <reason>` and gets **no** countdown: `nextAttemptAt` on a failed row is the last time it was scheduled for, so a countdown there would promise an attempt that never comes. `"sending"` stays bare, matching the TUI. * `ChatScreen` polls the outbox rows on the same 2s cadence as the bodies and renders the countdown against an injectable `nowSeconds` (the store holds an absolute instant, the app computes the age). * The join is positional — `ChatMessage` carries no `messages.id` — and fail-closed: mismatched row counts, an unknown status, or a status that contradicts the one the SDK resolved for that message drops every detail and leaves the flat markers. `ChatMessage.id` would remove the positional part; filed as follow-up. * `OutboxDetailTest` (17 tests) asserts the three `retry_in_text` buckets, the `queued (try 3/8, retry 45s)` wording, that a failed row shows no countdown, that the denominator comes from the caller, and the three fail-closed cases. 32 unit tests, 0 failures. Android base: PR #56 (`feat/android-outbox-state`, t_f46a9863) — the Android half of the #52/#56 duplicate pair, which is not merged here. SDK base: PR #58 on PR #51.
This commit is contained in:
parent
7b68cb4a9f
commit
fc2b2197c7
5 changed files with 494 additions and 10 deletions
|
|
@ -143,6 +143,8 @@ private fun ReadyGraph(vm: WireViewModel, ready: Phase.Ready, keepAliveNote: Str
|
|||
fingerprint = friend?.fingerprint ?: pk.take(16),
|
||||
onion = friend?.onion.orEmpty(),
|
||||
loadMessages = { vm.messages(pk) },
|
||||
loadOutbox = { vm.outbox(pk) },
|
||||
loadMaxAttempts = { vm.outboxMaxAttempts() },
|
||||
onSend = { body -> vm.send(pk, body) },
|
||||
onBack = { nav.popBackStack() },
|
||||
)
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import kotlinx.coroutines.withContext
|
|||
import uniffi.onionwire_sdk.ChatMessage
|
||||
import uniffi.onionwire_sdk.FriendInfo
|
||||
import uniffi.onionwire_sdk.InviteInfo
|
||||
import uniffi.onionwire_sdk.OutboxRow
|
||||
import uniffi.onionwire_sdk.RotateOutcome
|
||||
import uniffi.onionwire_sdk.SendState
|
||||
import uniffi.onionwire_sdk.Wire
|
||||
|
|
@ -362,6 +363,23 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
suspend fun messages(pubkeyHex: String): List<ChatMessage> =
|
||||
requireWire().messages(pubkeyHex)
|
||||
|
||||
/**
|
||||
* The core's outbox rows for one friend: the `"queued"` / `"sending"` /
|
||||
* `"failed"` rows behind [ChatMessage.delivery], each with the attempt count
|
||||
* and absolute next-attempt time the store holds. Empty means nothing is
|
||||
* outstanding for them. Read this instead of guessing a retry schedule in
|
||||
* the app.
|
||||
*/
|
||||
suspend fun outbox(pubkeyHex: String): List<OutboxRow> =
|
||||
requireWire().outbox(pubkeyHex)
|
||||
|
||||
/**
|
||||
* The core's retry budget — the denominator in `try n/8`. A constant of the
|
||||
* outbox, not per-node state, so it is fetched from the SDK rather than
|
||||
* duplicated here; the app must not hardcode it.
|
||||
*/
|
||||
suspend fun outboxMaxAttempts(): UInt = uniffi.onionwire_sdk.outboxMaxAttempts()
|
||||
|
||||
/**
|
||||
* Sends one chat message and reports what the core did with it.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import androidx.compose.foundation.layout.padding
|
|||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Button
|
||||
|
|
@ -41,6 +41,7 @@ import androidx.compose.ui.text.font.FontFamily
|
|||
import androidx.compose.ui.unit.dp
|
||||
import kotlinx.coroutines.delay
|
||||
import uniffi.onionwire_sdk.ChatMessage
|
||||
import uniffi.onionwire_sdk.OutboxRow
|
||||
import uniffi.onionwire_sdk.SendState
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
|
|
@ -50,10 +51,22 @@ fun ChatScreen(
|
|||
fingerprint: String,
|
||||
onion: String,
|
||||
loadMessages: suspend () -> List<ChatMessage>,
|
||||
loadOutbox: suspend () -> List<OutboxRow>,
|
||||
loadMaxAttempts: suspend () -> UInt,
|
||||
onSend: suspend (String) -> SendState,
|
||||
onBack: () -> Unit,
|
||||
/**
|
||||
* Wall clock in unix seconds. `next_attempt_at` is absolute, so the
|
||||
* countdown is computed here rather than baked into the store — injectable
|
||||
* so a test can pin "now" instead of racing the clock.
|
||||
*/
|
||||
nowSeconds: () -> Long = { System.currentTimeMillis() / 1_000L },
|
||||
) {
|
||||
var messages by remember { mutableStateOf<List<ChatMessage>?>(null) }
|
||||
var outbox by remember { mutableStateOf<List<OutboxRow>>(emptyList()) }
|
||||
// The core's retry budget, fetched once: `try 3/8` renders against this and
|
||||
// never against a constant in the app.
|
||||
var maxAttempts by remember { mutableStateOf<UInt?>(null) }
|
||||
var draft by remember { mutableStateOf("") }
|
||||
var pending by remember { mutableStateOf<String?>(null) }
|
||||
var error by remember { mutableStateOf<String?>(null) }
|
||||
|
|
@ -63,11 +76,18 @@ fun ChatScreen(
|
|||
// Poll: the node appends inbound messages in the background and the retry
|
||||
// worker moves queued rows along; there is no push channel into the UI in
|
||||
// v1, so the delivery markers refresh on the same cadence as the bodies.
|
||||
// The outbox rows are read on that cadence too — their `next_attempt_at` is
|
||||
// a fixed instant, so a row saying "retry 45s" turns into "retry now" here
|
||||
// without the store having to change.
|
||||
LaunchedEffect(Unit) {
|
||||
runCatching { loadMaxAttempts() }.onSuccess { maxAttempts = it }
|
||||
while (true) {
|
||||
runCatching { loadMessages() }
|
||||
.onSuccess { messages = it }
|
||||
.onFailure { error = it.message ?: it.toString() }
|
||||
runCatching { loadOutbox() }
|
||||
.onSuccess { outbox = it }
|
||||
.onFailure { error = it.message ?: it.toString() }
|
||||
delay(2_000)
|
||||
}
|
||||
}
|
||||
|
|
@ -156,13 +176,36 @@ fun ChatScreen(
|
|||
}
|
||||
|
||||
else -> {
|
||||
// The outbox rows behind the pending sends: `queued (try
|
||||
// 3/8, retry 45s)` needs the store's own counters and the
|
||||
// core's retry budget. With either missing, or with a join
|
||||
// that cannot prove itself, the rows keep the flat state
|
||||
// marker — wrong numbers would be worse than none.
|
||||
val max = maxAttempts
|
||||
val details = if (max == null) {
|
||||
List(list.size) { null }
|
||||
} else {
|
||||
outboxDetails(
|
||||
rows = list.map { ChatRowInput(it.direction, it.delivery) },
|
||||
outbox = outbox.map {
|
||||
OutboxRowInput(
|
||||
messageId = it.messageId,
|
||||
status = it.status,
|
||||
attempts = it.attempts,
|
||||
nextAttemptAt = it.nextAttemptAt,
|
||||
)
|
||||
},
|
||||
maxAttempts = max,
|
||||
now = nowSeconds(),
|
||||
)
|
||||
}
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
modifier = Modifier.weight(1f).fillMaxWidth(),
|
||||
contentPadding = PaddingValues(12.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||
) {
|
||||
items(list) { m ->
|
||||
itemsIndexed(list) { i, m ->
|
||||
val mine = m.direction == "out"
|
||||
Row(
|
||||
Modifier.fillMaxWidth(),
|
||||
|
|
@ -187,7 +230,7 @@ fun ChatScreen(
|
|||
.padding(horizontal = 10.dp, vertical = 8.dp),
|
||||
)
|
||||
parseDelivery(m.direction, m.delivery)?.let { d ->
|
||||
DeliveryMarker(d)
|
||||
DeliveryMarker(d, details.getOrNull(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -262,14 +305,16 @@ fun ChatScreen(
|
|||
/**
|
||||
* One message's delivery marker, straight from the core's outbox state.
|
||||
*
|
||||
* The row carries an explicit [Delivery.contentDescription] so a screen reader
|
||||
* announces "Queued, not delivered yet" instead of reading a clock glyph, and a
|
||||
* [Delivery.testTag] so a UI test can find the state without depending on the
|
||||
* glyph. `send` shows a small indeterminate spinner while the worker has an
|
||||
* The row carries an explicit announcement so a screen reader says
|
||||
* "Queued, not delivered yet: retrying their onion (try 3/8, retry 45s)"
|
||||
* instead of reading a clock glyph, and a [Delivery.testTag] so a UI test can
|
||||
* find the state without depending on the glyph. [detail] is the outbox row's
|
||||
* own counters when the join landed; when it is `null` the row keeps the flat
|
||||
* marker. `send` shows a small indeterminate spinner while the worker has an
|
||||
* attempt in flight.
|
||||
*/
|
||||
@Composable
|
||||
private fun DeliveryMarker(d: Delivery) {
|
||||
private fun DeliveryMarker(d: Delivery, detail: QueuedRowDetail?) {
|
||||
val tint = when (d) {
|
||||
is Delivery.Failed -> MaterialTheme.colorScheme.error
|
||||
Delivery.Sent -> MaterialTheme.colorScheme.onSurfaceVariant
|
||||
|
|
@ -281,7 +326,7 @@ private fun DeliveryMarker(d: Delivery) {
|
|||
.padding(horizontal = 4.dp, vertical = 1.dp)
|
||||
.testTag(d.testTag)
|
||||
.semantics(mergeDescendants = true) {
|
||||
contentDescription = d.contentDescription
|
||||
contentDescription = deliveryAnnouncement(d, detail)
|
||||
},
|
||||
) {
|
||||
if (d is Delivery.Sending) {
|
||||
|
|
@ -293,7 +338,7 @@ private fun DeliveryMarker(d: Delivery) {
|
|||
Spacer(Modifier.width(4.dp))
|
||||
}
|
||||
Text(
|
||||
d.marker,
|
||||
deliveryMarkerText(d, detail),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = tint,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,141 @@
|
|||
package com.siriusdevops.onionwire.ui
|
||||
|
||||
/**
|
||||
* Why a chat row is still queued, read from the SDK's outbox rows
|
||||
* (`Wire.outbox(pubkeyHex)` → `OutboxRow { messageId, status, attempts,
|
||||
* nextAttemptAt, lastError }`).
|
||||
*
|
||||
* [Delivery] answers *what* state a row is in; this file answers *how far along*
|
||||
* it is — `queued (try 3/8, retry 45s)` — from the store's own counters, never
|
||||
* from a time the app invented. Mirrors `src/tui.rs::retry_in_text` /
|
||||
* `delivery_tag` so both clients word the same state the same way.
|
||||
*
|
||||
* Nothing here is Android-specific: the join and the wording are pure functions
|
||||
* so `OutboxDetailTest` can assert them on the JVM without a device or the
|
||||
* native binding.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Next-attempt age for a queued row, from the absolute `next_attempt_at` the
|
||||
* store holds. Mirrors `src/tui.rs::retry_in_text`:
|
||||
* `<= 0` → `retry now`; `< 60` → `retry {n}s`; else → `retry {ceil-minutes}m`.
|
||||
*/
|
||||
fun retryInText(nextInSecs: Long): String = when {
|
||||
nextInSecs <= 0L -> "retry now"
|
||||
nextInSecs < 60L -> "retry ${nextInSecs}s"
|
||||
else -> "retry ${(nextInSecs + 59L) / 60L}m"
|
||||
}
|
||||
|
||||
/**
|
||||
* The outbox row's own numbers for one queued chat row.
|
||||
*
|
||||
* Only `"queued"` rows produce one: a terminal `"failed"` row keeps the last
|
||||
* time it was *scheduled* for, so rendering it as a countdown would be a lie —
|
||||
* a failed row is rendered from [Delivery.Failed] alone. `"sending"` has an
|
||||
* attempt in flight and no next-attempt time worth showing, matching the TUI's
|
||||
* bare `sending` tag.
|
||||
*/
|
||||
data class QueuedRowDetail(
|
||||
val attempts: UInt,
|
||||
val maxAttempts: UInt,
|
||||
val nextInSecs: Long,
|
||||
) {
|
||||
/** `queued (try 3/8, retry 45s)` — the row text, mirroring the TUI. */
|
||||
val text: String = "queued (try $attempts/$maxAttempts, ${retryInText(nextInSecs)})"
|
||||
|
||||
/**
|
||||
* `try 3/8, retry 45s` — the same numbers without the state word, for a
|
||||
* screen reader that has already heard "Queued, not delivered yet".
|
||||
*/
|
||||
val announcement: String = "try $attempts/$maxAttempts, ${retryInText(nextInSecs)}"
|
||||
}
|
||||
|
||||
/** One SDK `ChatMessage`, narrowed to the fields the join reads. */
|
||||
data class ChatRowInput(val direction: String, val delivery: String?)
|
||||
|
||||
/** One SDK `OutboxRow`, narrowed to the fields the join reads. */
|
||||
data class OutboxRowInput(
|
||||
val messageId: Long,
|
||||
val status: String,
|
||||
val attempts: UInt,
|
||||
val nextAttemptAt: Long,
|
||||
)
|
||||
|
||||
/** The store's own three pending encodings, trimmed and lowercased. */
|
||||
private fun outboxKind(status: String): String? = when (val s = status.trim().lowercase()) {
|
||||
"queued", "sending", "failed" -> s
|
||||
else -> null
|
||||
}
|
||||
|
||||
/** The same kind, read off the status the SDK already resolved for the message. */
|
||||
private fun deliveryKind(d: Delivery?): String? = when (d) {
|
||||
Delivery.Queued -> "queued"
|
||||
Delivery.Sending -> "sending"
|
||||
is Delivery.Failed -> "failed"
|
||||
Delivery.Sent, null -> null
|
||||
}
|
||||
|
||||
/**
|
||||
* The SDK's `OutboxRow.message_id` is `messages.id` — the key the chat list is
|
||||
* ordered by. The SDK's `ChatMessage` does not carry that id yet, so this join
|
||||
* is positional: `messages()` is `ORDER BY m.id` and the outbox rows are one
|
||||
* per pending send, so the *n*th pending chat row owns the *n*th outbox row
|
||||
* (rows are sorted by `messageId` to make that explicit rather than trusting the
|
||||
* core's row order).
|
||||
*
|
||||
* A positional join is only allowed to speak when it can prove itself: if the
|
||||
* counts disagree, if a row carries a status this build does not know, or if a
|
||||
* row's status does not match the status the SDK already resolved for the
|
||||
* message in that slot, *every* detail is dropped and the rows fall back to the
|
||||
* flat `queued` / `sending` / `failed` markers. Wrong numbers are worse than
|
||||
* none. (`ChatMessage.id` would remove the positional part entirely; tracked
|
||||
* as follow-up work.)
|
||||
*
|
||||
* Returns one entry per input row, `null` wherever there is nothing to add.
|
||||
*/
|
||||
fun outboxDetails(
|
||||
rows: List<ChatRowInput>,
|
||||
outbox: List<OutboxRowInput>,
|
||||
maxAttempts: UInt,
|
||||
now: Long,
|
||||
): List<QueuedRowDetail?> {
|
||||
val none: List<QueuedRowDetail?> = List(rows.size) { null }
|
||||
|
||||
val pending = rows.indices.filter {
|
||||
deliveryKind(parseDelivery(rows[it].direction, rows[it].delivery)) != null
|
||||
}
|
||||
val ordered = outbox.sortedBy { it.messageId }
|
||||
if (pending.size != ordered.size) return none
|
||||
|
||||
val details = arrayOfNulls<QueuedRowDetail>(rows.size)
|
||||
for ((slot, index) in pending.withIndex()) {
|
||||
val row = ordered[slot]
|
||||
val kind = outboxKind(row.status) ?: return none
|
||||
if (kind != deliveryKind(parseDelivery(rows[index].direction, rows[index].delivery))) {
|
||||
return none
|
||||
}
|
||||
if (kind == "queued") {
|
||||
details[index] = QueuedRowDetail(
|
||||
attempts = row.attempts,
|
||||
maxAttempts = maxAttempts,
|
||||
nextInSecs = row.nextAttemptAt - now,
|
||||
)
|
||||
}
|
||||
}
|
||||
return details.toList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Marker text for one row: the outbox row's own numbers when the join landed,
|
||||
* otherwise the flat state marker `MessageDelivery` already produced.
|
||||
*/
|
||||
fun deliveryMarkerText(d: Delivery, detail: QueuedRowDetail?): String =
|
||||
if (detail == null) d.marker else "${d.glyph} ${detail.text}"
|
||||
|
||||
/**
|
||||
* What a screen reader announces for one row. The detail is appended to the
|
||||
* sentence [Delivery] carries, never replaces it: a bare `try 3/8` is not a
|
||||
* sentence.
|
||||
*/
|
||||
fun deliveryAnnouncement(d: Delivery, detail: QueuedRowDetail?): String =
|
||||
if (detail == null) d.contentDescription else "${d.contentDescription} (${detail.announcement})"
|
||||
Loading…
Reference in a new issue