Obsługa intencji i działań użytkownika
Sterowanie zmianami stanu na podstawie zdarzeń interfejsu
Obsługa intencji i działań użytkownika to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🚀
Często zadawane pytania
Czy lekcja „Obsługa intencji i działań użytkownika” jest bezpłatna?
Tak — pełny tekst „Obsługa intencji i działań użytkownika” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Obsługa intencji i działań użytkownika”?
Sterowanie zmianami stanu na podstawie zdarzeń interfejsu Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?
Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Obsługa intencji i działań użytkownika”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?
Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wieloplatformowa baza ViewModel
- Udostępnianie stanu interfejsu jako StateFlow
- Obsługa intencji i działań użytkownika
- Powiązanie ViewModel z każdym interfejsem