40 lines
1.2 KiB
Kotlin
40 lines
1.2 KiB
Kotlin
package studio.daybreak.mail
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
|
|
/** Makes native confirmations fail closed and guarantees each plugin call gets one decision. */
|
|
internal class AndroidPgpConfirmationGate(
|
|
private val showPrompt: (Prompt) -> Unit,
|
|
private val dispatch: ((() -> Unit) -> Unit)
|
|
) {
|
|
data class Prompt(
|
|
val title: String,
|
|
val message: String,
|
|
val confirmLabel: String,
|
|
val cancelLabel: String,
|
|
val confirm: () -> Unit,
|
|
val cancel: () -> Unit
|
|
)
|
|
|
|
fun request(
|
|
title: String,
|
|
message: String,
|
|
confirmLabel: String,
|
|
cancelLabel: String = "Cancel",
|
|
onConfirmed: () -> Unit,
|
|
onCancelled: () -> Unit
|
|
) {
|
|
val decided = AtomicBoolean(false)
|
|
showPrompt(
|
|
Prompt(
|
|
title = title,
|
|
message = message,
|
|
confirmLabel = confirmLabel,
|
|
cancelLabel = cancelLabel,
|
|
confirm = { if (decided.compareAndSet(false, true)) dispatch(onConfirmed) },
|
|
cancel = { if (decided.compareAndSet(false, true)) dispatch(onCancelled) }
|
|
)
|
|
)
|
|
}
|
|
}
|