Merge pull request 'fix(android): Approve and add no-op from shared Compose busy flag' (#18) from wt/t_af421dc6 into main
All checks were successful
ci / test (push) Successful in 2m30s
All checks were successful
ci / test (push) Successful in 2m30s
Reviewed-on: #18
This commit is contained in:
commit
8c79250acf
5 changed files with 211 additions and 33 deletions
|
|
@ -87,4 +87,7 @@ dependencies {
|
|||
implementation(libs.compose.material3)
|
||||
|
||||
debugImplementation(libs.compose.ui.tooling)
|
||||
|
||||
testImplementation(libs.junit)
|
||||
testImplementation(libs.kotlinx.coroutines.test)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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 ->
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,11 +8,14 @@ navigationCompose = "2.9.8"
|
|||
composeBom = "2025.12.01"
|
||||
jna = "5.19.1"
|
||||
coroutines = "1.11.0"
|
||||
junit = "4.13.2"
|
||||
|
||||
[libraries]
|
||||
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
|
||||
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-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
|
||||
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-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle" }
|
||||
|
|
|
|||
Loading…
Reference in a new issue