จัดการเจตนาและการกระทำของผู้ใช้
ขับเคลื่อนการเปลี่ยนแปลงสถานะจากเหตุการณ์ใน UI
จัดการเจตนาและการกระทำของผู้ใช้ เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 🚀
คำถามที่พบบ่อย
บทเรียน “จัดการเจตนาและการกระทำของผู้ใช้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “จัดการเจตนาและการกระทำของผู้ใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “จัดการเจตนาและการกระทำของผู้ใช้”
ขับเคลื่อนการเปลี่ยนแปลงสถานะจากเหตุการณ์ใน UI คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “จัดการเจตนาและการกระทำของผู้ใช้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน ViewModel ข้ามแพลตฟอร์ม
- เปิดเผยสถานะ UI เป็น StateFlow
- จัดการเจตนาและการกระทำของผู้ใช้
- ผูก ViewModel เข้ากับ UI แต่ละแบบ