Skip to content

Circuit

Recommended wiring: the presenter owns each field's TextFieldState and the validator that follows it. Circuit presenters are composable functions, so remember { TextFieldState() }, remember { FieldValidator(...) } and a LaunchedEffect that pipes textChanges() into the validator all work there naturally. The field owns the text, the presenter owns the form state, and the UI stays a dumb renderer of CircuitUiState. The text is never rendered from state, so a late recomposition can't drop a keystroke (see the decision record).

State and events

/**
 * Circuit state. Each field contributes its editing buffer (a TextFieldState — the field owns the
 * text) and the FieldValidator that follows it; both are @Stable references. Events flow back via
 * the sink.
 */
data class SignUpState(
    val emailText: TextFieldState,
    val email: FieldValidator<String>,
    val passwordText: TextFieldState,
    val password: FieldValidator<String>,
    val submitted: Boolean?,
    val eventSink: (SignUpEvent) -> Unit,
) : CircuitUiState

sealed interface SignUpEvent : CircuitUiEvent {
    data object Submit : SignUpEvent
}

The presenter

class SignUpPresenter : Presenter<SignUpState> {
    @Composable
    override fun present(): SignUpState {
        // Circuit presenters are composable functions, so `remember` keeps the text buffers and
        // validators across recompositions (`rememberRetained` to survive Circuit's retention).
        val emailText = remember { TextFieldState() }
        val passwordText = remember { TextFieldState() }
        val email = remember { FieldValidator("", rules = listOf(Required, Email)) }
        val password = remember { FieldValidator("", rules = listOf(Required, MinLength(8))) }
        // One-way hand-off: the field's text -> its validator. Never the other way.
        LaunchedEffect(emailText) { emailText.textChanges().collect(email::onValueChange) }
        LaunchedEffect(passwordText) { passwordText.textChanges().collect(password::onValueChange) }
        var submitted by remember { mutableStateOf<Boolean?>(null) }
        return SignUpState(emailText, email, passwordText, password, submitted) { event ->
            when (event) {
                // Form-level validate(): shows errors on every field, true when all pass.
                SignUpEvent.Submit -> submitted = listOf(email, password).validate()
            }
        }
    }
}

The UI

@Composable
fun SignUpUi(state: SignUpState, modifier: Modifier = Modifier) {
    Column(modifier) {
        ValidatedField(state.emailText, state.email, label = "Email")
        ValidatedField(state.passwordText, state.password, label = "Password")
        Button(
            onClick = { state.eventSink(SignUpEvent.Submit) },
            modifier = Modifier.fillMaxWidth(),
        ) { Text("Create account") }
        state.submitted?.let { Text(if (it) "Valid — proceeding" else "Fix the errors above") }
    }
}

@Composable
private fun ValidatedField(text: TextFieldState, field: FieldValidator<String>, label: String) {
    OutlinedTextField(
        state = text, // the field owns the text — no value / onValueChange
        label = { Text(label) },
        isError = field.state.isError,
        supportingText = field.state.supportingText(),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier
            .fillMaxWidth()
            .onFocusLost { field.onFocusLost() },
    )
}

Hosting it

/**
 * Minimal host. In a real app you register SignUpPresenter/SignUpUi in a Circuit via
 * Presenter.Factory + Ui.Factory and render through CircuitContent(screen); a routed
 * Screen needs @Parcelize on Android, which is app-level wiring beyond validation —
 * so we keep it out of this example.
 */
@Composable
fun SignUpCircuitHost() {
    val presenter = remember { SignUpPresenter() }
    SignUpUi(presenter.present())
}

What this example leaves out

A real app routes via a Screen + CircuitContent, registering the presenter and UI through Presenter.Factory / Ui.Factory. Screen is Parcelable on Android (@Parcelize plumbing), which is app-level wiring unrelated to validation — so the example hosts the presenter directly. Only circuit-foundation is needed for everything shown here, and rememberRetained is a drop-in upgrade for remember if you want the text buffers and validators to survive Circuit's retention scope.

Prefer immutable state throughout? The MVI/UDF wiring drops into a Circuit presenter unchanged. Fold rules with toFieldState, put the text String and the FieldValidationState in your CircuitUiState instead of the validator, and keep the TextFieldState in the presenter.