fix(android): Approve & add no-op from shared Compose busy flag
All checks were successful
ci / test (pull_request) Successful in 2m32s

PasteInviteScreen drove Verify and Approve from one busy boolean plus
LaunchedEffect(busy). Tapping Approve relaunched decode, which cleared
busy and cancelled add — so the roster never updated.

Extract InviteAddFlow with separate verifying/adding flags and launch
each action from its click handler. Unit tests were red against the
shared-busy state machine (4/5 failed) and green after the split (5/5).
This commit is contained in:
Sirius DevOps 2026-09-10 23:14:43 -04:00
commit f6ac5ea738
No known key found for this signature in database
5 changed files with 211 additions and 33 deletions

View file

@ -0,0 +1,60 @@
package com.siriusdevops.onionwire.ui
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
/**
* JVM-testable holder for the paste-invite verify/add flow.
*
* Verify and add own independent in-flight flags. They must never share a
* single `busy` boolean — that is what made Approve & add a no-op: the
* verify path reset the flag the add path was waiting on.
*/
class InviteAddFlow<P, A>(
private val onDecode: suspend (String) -> P,
private val onAdd: suspend (String) -> A,
) {
var preview: P? by mutableStateOf(null)
private set
var added: A? by mutableStateOf(null)
private set
var error: String? by mutableStateOf(null)
private set
var verifying: Boolean by mutableStateOf(false)
private set
var adding: Boolean by mutableStateOf(false)
private set
fun clear() {
preview = null
error = null
added = null
}
suspend fun verify(raw: String) {
if (verifying) return
verifying = true
error = null
try {
preview = onDecode(raw.trim())
} catch (t: Throwable) {
error = t.message ?: t.toString()
} finally {
verifying = false
}
}
suspend fun add(raw: String) {
if (adding || added != null) return
adding = true
error = null
try {
added = onAdd(raw.trim())
} catch (t: Throwable) {
error = t.message ?: t.toString()
} finally {
adding = false
}
}
}

View file

@ -28,11 +28,13 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import uniffi.onionwire_sdk.FriendInfo
import uniffi.onionwire_sdk.InviteInfo
@ -129,10 +131,13 @@ fun PasteInviteScreen(
onBack: () -> Unit,
) {
var raw by remember { mutableStateOf("") }
var preview by remember { mutableStateOf<InviteInfo?>(null) }
var error by remember { mutableStateOf<String?>(null) }
var added by remember { mutableStateOf<FriendInfo?>(null) }
var busy by remember { mutableStateOf(false) }
val flow = remember { InviteAddFlow(onDecode, onAdd) }
val scope = rememberCoroutineScope()
val preview = flow.preview
val error = flow.error
val added = flow.added
val verifying = flow.verifying
val adding = flow.adding
Scaffold(
topBar = {
@ -152,9 +157,7 @@ fun PasteInviteScreen(
value = raw,
onValueChange = {
raw = it
preview = null
error = null
added = null
flow.clear()
},
label = { Text("onionwire:v1:…") },
minLines = 4,
@ -164,25 +167,14 @@ fun PasteInviteScreen(
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Button(
enabled = raw.isNotBlank() && !busy,
onClick = {
busy = true
error = null
},
enabled = raw.isNotBlank() && !verifying && !adding,
onClick = { scope.launch { flow.verify(raw) } },
) { Text("Verify") }
if (added != null) {
TextButton(onClick = onBack) { Text("Done") }
}
}
LaunchedEffect(busy) {
if (!busy) return@LaunchedEffect
runCatching { onDecode(raw.trim()) }
.onSuccess { preview = it }
.onFailure { error = it.message ?: it.toString() }
busy = false
}
Spacer(Modifier.height(16.dp))
error?.let {
@ -223,20 +215,9 @@ fun PasteInviteScreen(
}
Spacer(Modifier.height(12.dp))
Button(
enabled = !busy && added == null,
onClick = {
busy = true
error = null
},
enabled = !adding && added == null,
onClick = { scope.launch { flow.add(raw) } },
) { Text("Approve & add") }
LaunchedEffect(busy, preview) {
if (!busy || preview == null || added != null) return@LaunchedEffect
runCatching { onAdd(raw.trim()) }
.onSuccess { added = it }
.onFailure { error = it.message ?: it.toString() }
busy = false
}
}
added?.let { f ->