Gestire intenti e azioni dell'utente
Guidare i cambiamenti di stato a partire dagli eventi dell'interfaccia
Gestire intenti e azioni dell'utente è una lezione Kotlin Multiplatform Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Kotlin Multiplatform Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The UI Asks, You Decide
When a user taps a button, the UI should not run logic itself. It sends an intent to the ViewModel, which decides what happens.
Name Your Intents
Model each possible user action as a sealed class. Now every intent the screen can fire is named, typed and impossible to misspell.
sealed class HomeIntent {
object Refresh : HomeIntent()
data class Search(val q: String) : HomeIntent()
}One Entry Point
Expose a single onIntent function. Every screen event flows through it, giving you one tidy place to route behavior.
fun onIntent(intent: HomeIntent) {
// route to the right handler
}when Routes Each Action
Inside onIntent, a when matches each intent type. Because the class is sealed, the compiler ensures you cover every case.
when (intent) {
HomeIntent.Refresh -> refresh()
is HomeIntent.Search -> search(intent.q)
}Handlers Update State
Each handler does its work, then updates the StateFlow. The UI re-renders from that single state, exactly as before.
private fun refresh() = scope.launch {
_state.update { it.copy(isLoading = true) }
}Why Not Public Methods?
You could expose many public methods, but one intent channel makes the screen's whole contract visible in one type. Clean and testable.
Carry Data in the Intent
Intents that need input use a data class. The user's query or id rides along inside the action itself.
data class Search(val query: String) : HomeIntent()One-Off Events Are Different
State persists, but a toast or navigation happens once. For these one-off events use a Channel or SharedFlow, not StateFlow.
Emit Events with a Channel
Hold a private Channel for events and expose it as a Flow. The UI collects it to show messages or navigate just once.
private val _events = Channel<String>()
val events = _events.receiveAsFlow()Both Apps, Same Flow
Android sends intents from Compose, iOS from SwiftUI, but they hit the same onIntent. Behavior can never drift between platforms.
A Predictable Loop
Intent in, state out: this one-way loop makes the screen easy to reason about, debug and test. No hidden side paths.
Quick Check
Let's check how UI events should reach shared logic.
Recap
Model actions as a sealed intent type, route them through one onIntent, and update state from there. Use a Channel for one-off events. 🚀
Domande Frequenti
La lezione «Gestire intenti e azioni dell'utente» è gratuita?
Sì — il testo completo di «Gestire intenti e azioni dell'utente» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Kotlin Multiplatform Academy, passa a CoddyKit PRO. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Cosa imparerò in «Gestire intenti e azioni dell'utente»?
Guidare i cambiamenti di stato a partire dagli eventi dell'interfaccia Eserciti Kotlin Multiplatform Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Kotlin Multiplatform Academy?
Non è richiesta alcuna esperienza precedente. Kotlin Multiplatform Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Gestire intenti e azioni dell'utente»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Kotlin Multiplatform Academy?
Sì. Ogni lezione Kotlin Multiplatform Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Una base ViewModel multipiattaforma
- Esporre lo stato dell'interfaccia come StateFlow
- Gestire intenti e azioni dell'utente
- Collegare il ViewModel a ogni interfaccia