fix(android): Approve and add no-op from shared Compose busy flag #18

Merged
sirius merged 1 commit from wt/t_af421dc6 into main 2026-09-12 02:05:47 +00:00
5 changed files with 211 additions and 33 deletions

View file

@ -87,4 +87,7 @@ dependencies {
implementation(libs.compose.material3) implementation(libs.compose.material3)
debugImplementation(libs.compose.ui.tooling) debugImplementation(libs.compose.ui.tooling)
testImplementation(libs.junit)
testImplementation(libs.kotlinx.coroutines.test)
} }

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

View file

@ -0,0 +1,131 @@
package com.siriusdevops.onionwire.ui
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
class InviteAddFlowTest {
private data class Preview(val id: String)
private data class Added(val id: String)
@Test
fun add_while_verify_in_flight_completes_add_exactly_once() = runTest {
val decodeEntered = CompletableDeferred<Unit>()
val decodeGate = CompletableDeferred<Unit>()
var decodeCount = 0
var addCount = 0
val flow = InviteAddFlow(
onDecode = {
decodeCount++
if (decodeCount == 1) {
decodeEntered.complete(Unit)
decodeGate.await()
}
Preview("p")
},
onAdd = {
addCount++
Added("a")
},
)
val verifyJob = launch { flow.verify("invite") }
decodeEntered.await()
flow.add("invite")
decodeGate.complete(Unit)
verifyJob.join()
assertEquals(1, addCount)
assertEquals(Added("a"), flow.added)
}
@Test
fun onAdd_is_invoked_exactly_once_per_approve() = runTest {
var addCount = 0
val flow = InviteAddFlow(
onDecode = { Preview("p") },
onAdd = {
addCount++
Added("a")
},
)
flow.verify("invite")
flow.add("invite")
assertEquals(1, addCount)
assertEquals(Added("a"), flow.added)
}
@Test
fun failed_decode_sets_error_and_never_calls_onAdd() = runTest {
var addCount = 0
val flow = InviteAddFlow(
onDecode = { error("bad invite") },
onAdd = {
addCount++
Added("a")
},
)
flow.verify("invite")
assertEquals("bad invite", flow.error)
assertNull(flow.preview)
assertNull(flow.added)
assertEquals(0, addCount)
}
@Test
fun double_tap_approve_while_in_flight_does_not_add_twice() = runTest {
val addEntered = CompletableDeferred<Unit>()
val addGate = CompletableDeferred<Unit>()
var addCount = 0
val flow = InviteAddFlow(
onDecode = { Preview("p") },
onAdd = {
addCount++
addEntered.complete(Unit)
addGate.await()
Added("a")
},
)
flow.verify("invite")
val first = async { flow.add("invite") }
addEntered.await()
val second = async { flow.add("invite") }
addGate.complete(Unit)
first.await()
second.await()
assertEquals(1, addCount)
assertEquals(Added("a"), flow.added)
}
@Test
fun after_successful_add_further_taps_are_inert() = runTest {
var addCount = 0
val flow = InviteAddFlow(
onDecode = { Preview("p") },
onAdd = {
addCount++
Added("a")
},
)
flow.verify("invite")
flow.add("invite")
flow.add("invite")
flow.add("invite")
assertNotNull(flow.added)
assertEquals(1, addCount)
}
}

View file

@ -8,11 +8,14 @@ navigationCompose = "2.9.8"
composeBom = "2025.12.01" composeBom = "2025.12.01"
jna = "5.19.1" jna = "5.19.1"
coroutines = "1.11.0" coroutines = "1.11.0"
junit = "4.13.2"
[libraries] [libraries]
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" } androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
jna = { module = "net.java.dev.jna:jna", version.ref = "jna" } jna = { module = "net.java.dev.jna:jna", version.ref = "jna" }
junit = { module = "junit:junit", version.ref = "junit" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" }
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycle" } androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycle" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle" } androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle" }