android: one chat poller owned by the ViewModel, gated auto-scroll, Enter-to-send
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.
This commit is contained in:
parent
f95e058041
commit
04c993862c
3 changed files with 198 additions and 41 deletions
|
|
@ -12,9 +12,14 @@ import com.siriusdevops.onionwire.keepalive.KeepAliveService
|
|||
import com.siriusdevops.onionwire.keepalive.KeepAliveStatus
|
||||
import com.siriusdevops.onionwire.keepalive.NodeHost
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import uniffi.onionwire_sdk.ChatMessage
|
||||
|
|
@ -61,6 +66,22 @@ sealed interface Unlock {
|
|||
data class OneTap(val backing: KeyBacking) : Unlock
|
||||
}
|
||||
|
||||
/**
|
||||
* Everything one open chat screen renders, refreshed on a single cadence.
|
||||
*
|
||||
* [messages] is null until the first read lands — the screen shows its spinner
|
||||
* on that, exactly as it did reading the SDK itself. [error] is the last load
|
||||
* failure and is sticky on purpose: it survives the ticks that succeed, so a
|
||||
* transient store error is still on screen after the next 2s tick rather than
|
||||
* blinking away. A send clears it (the user has acted on it).
|
||||
*/
|
||||
data class ChatState(
|
||||
val messages: List<ChatMessage>? = null,
|
||||
val outbox: List<OutboxRow> = emptyList(),
|
||||
val maxAttempts: UInt? = null,
|
||||
val error: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Owns the UI side of the process's single node.
|
||||
*
|
||||
|
|
@ -101,6 +122,22 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
private val _keepAliveNote = MutableStateFlow<String?>(null)
|
||||
val keepAliveNote: StateFlow<String?> = _keepAliveNote.asStateFlow()
|
||||
|
||||
/**
|
||||
* The currently open chat, or null when no chat screen is composed.
|
||||
*
|
||||
* There is exactly one node and one chat on screen at a time, so this is
|
||||
* one state object rather than a map keyed by peer: a second poller for a
|
||||
* second peer would double the SDK traffic for a screen nobody is looking
|
||||
* at, and the node's store reads are not free.
|
||||
*/
|
||||
private val _chat = MutableStateFlow<ChatState?>(null)
|
||||
val chat: StateFlow<ChatState?> = _chat.asStateFlow()
|
||||
|
||||
/** The peer [chat] belongs to; guards a stale tick from a previous peer. */
|
||||
private var chatPeer: String? = null
|
||||
|
||||
private var chatPoller: Job? = null
|
||||
|
||||
init {
|
||||
// A node the foreground service kept alive is still open. Adopt it and
|
||||
// go straight to the roster: showing an unlock screen for a store that
|
||||
|
|
@ -177,6 +214,10 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
*/
|
||||
fun lock() {
|
||||
if (NodeHost.current() == null) return
|
||||
// Stop the chat tick before the node goes: the next read of a closed
|
||||
// store would surface as a chat load error on a screen the user has
|
||||
// already left, and every tick would keep re-reporting it.
|
||||
stopChatPoller()
|
||||
KeepAliveService.stop(getApplication())
|
||||
NodeHost.lock()
|
||||
_friends.value = emptyList()
|
||||
|
|
@ -381,6 +422,87 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
*/
|
||||
suspend fun outboxMaxAttempts(): UInt = uniffi.onionwire_sdk.outboxMaxAttempts()
|
||||
|
||||
/**
|
||||
* Starts the one 2s refresh loop for [pubkeyHex] and resets the chat state.
|
||||
*
|
||||
* Called from the chat screen entering the composition. Re-entering the
|
||||
* same peer while its poller is alive is a no-op — navigating to the chat
|
||||
* and back must never stack a second loop on top of the first.
|
||||
*/
|
||||
fun openChat(pubkeyHex: String) {
|
||||
if (chatPeer == pubkeyHex && chatPoller?.isActive == true) return
|
||||
chatPoller?.cancel()
|
||||
chatPeer = pubkeyHex
|
||||
_chat.value = ChatState()
|
||||
chatPoller = viewModelScope.launch { pollChat(pubkeyHex) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the loop when the chat screen leaves. Ignored when another peer has
|
||||
* taken over in the meantime (navigating chat A → chat B disposes A after B
|
||||
* has already opened).
|
||||
*/
|
||||
fun closeChat(pubkeyHex: String) {
|
||||
if (chatPeer != pubkeyHex) return
|
||||
stopChatPoller()
|
||||
}
|
||||
|
||||
private fun stopChatPoller() {
|
||||
chatPoller?.cancel()
|
||||
chatPoller = null
|
||||
chatPeer = null
|
||||
_chat.value = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the last load error. The send path calls this: the user has acted,
|
||||
* so a stale "could not read" line must not sit under the composer while
|
||||
* the send is in flight.
|
||||
*/
|
||||
fun clearChatError() {
|
||||
_chat.update { it?.copy(error = null) }
|
||||
}
|
||||
|
||||
/**
|
||||
* The cadence, in the ViewModel: inbound messages are appended by the node
|
||||
* in the background and the retry worker moves queued rows along, and in v1
|
||||
* there is no push channel into the UI, so messages and outbox are read
|
||||
* together on one 2s tick. The outbox rows are read on that tick 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.
|
||||
*
|
||||
* The retry budget is read once per open chat (it is a constant of the
|
||||
* outbox, not per-peer state); a failure leaves it null and the rows fall
|
||||
* back to the flat marker.
|
||||
*/
|
||||
private suspend fun pollChat(pk: String) {
|
||||
runCatching { outboxMaxAttempts() }.onSuccess { budget ->
|
||||
if (chatPeer == pk) _chat.update { it?.copy(maxAttempts = budget) }
|
||||
}
|
||||
while (currentCoroutineContext().isActive) {
|
||||
val loaded = runCatching { messages(pk) }
|
||||
val out = runCatching { outbox(pk) }
|
||||
// A cancelled loop can still be sitting in a completed SDK read:
|
||||
// writing that peer's rows into a state that now belongs to another
|
||||
// peer would show the wrong conversation.
|
||||
if (chatPeer != pk) return
|
||||
_chat.update { state ->
|
||||
val base = state ?: ChatState()
|
||||
base.copy(
|
||||
messages = loaded.getOrNull() ?: base.messages,
|
||||
outbox = out.getOrNull() ?: base.outbox,
|
||||
// Same order the screen's own loop reported them in, and
|
||||
// sticky on success — a new failure replaces the last one,
|
||||
// a successful tick does not clear the last one.
|
||||
error = out.exceptionOrNull()?.let { it.message ?: it.toString() }
|
||||
?: loaded.exceptionOrNull()?.let { it.message ?: it.toString() }
|
||||
?: base.error,
|
||||
)
|
||||
}
|
||||
delay(2_000)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends one chat message and reports what the core did with it.
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in a new issue