0Pricing
Kotlin Multiplatform Academy · レッスン

User IntentsとActionsを処理する

UIイベントから状態の変化を引き起こします

「User IntentsとActionsを処理する」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🚀

よくある質問

「User IntentsとActionsを処理する」レッスンは無料ですか?

はい。「User IntentsとActionsを処理する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

「User IntentsとActionsを処理する」で何を学びますか?

UIイベントから状態の変化を引き起こします ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Kotlin Multiplatform Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「User IntentsとActionsを処理する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?

はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. クロスプラットフォームViewModelの基底クラス
  2. UI StateをStateFlowとして公開する
  3. User IntentsとActionsを処理する
  4. ViewModelを各UIにバインドする
← Kotlin Multiplatform Academyに戻る