معالجة نوايا المستخدم وإجراءاته
قيادة تغييرات الحالة انطلاقًا من أحداث الواجهة
معالجة نوايا المستخدم وإجراءاته درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🚀
الأسئلة الشائعة
هل درس «معالجة نوايا المستخدم وإجراءاته» مجاني؟
نعم — نص درس «معالجة نوايا المستخدم وإجراءاته» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «معالجة نوايا المستخدم وإجراءاته»؟
قيادة تغييرات الحالة انطلاقًا من أحداث الواجهة تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «معالجة نوايا المستخدم وإجراءاته»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- قاعدة ViewModel متعددة المنصات
- كشف حالة الواجهة باعتبارها StateFlow
- معالجة نوايا المستخدم وإجراءاته
- ربط ViewModel بكل واجهة