{"id":187,"date":"2026-10-20T06:21:00","date_gmt":"2026-10-20T06:21:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=187"},"modified":"2026-11-06T08:48:26","modified_gmt":"2026-11-06T08:48:26","slug":"unidirectional-data-flow-udf-in-android-with-jetpack-compose-events-state-effects","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/unidirectional-data-flow-udf-in-android-with-jetpack-compose-events-state-effects\/","title":{"rendered":"Unidirectional Data Flow (UDF) in Android with Jetpack Compose: Events, State &#038; Effects"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Modern Android applications often contain multiple UI components interacting with shared application state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider an e-commerce application where users can add products to a shopping cart, increase or decrease quantities, remove products, and complete purchases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Every interaction changes the application&#8217;s state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Without a consistent state management approach, these changes can become difficult to track.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A button updates one part of the UI but not another.<\/li>\n\n\n\n<li>Cart totals become inconsistent.<\/li>\n\n\n\n<li>Multiple components modify the same state.<\/li>\n\n\n\n<li>Navigation happens more than once.<\/li>\n\n\n\n<li>Snackbar messages disappear unexpectedly.<\/li>\n\n\n\n<li>Business logic becomes mixed with composables.<\/li>\n\n\n\n<li>Unit testing becomes complicated.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Unidirectional Data Flow (UDF)<\/strong> helps solve these problems by defining a predictable direction for user actions and state updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore UDF using Kotlin, Jetpack Compose, ViewModel, StateFlow, and MVVM architecture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also build a Shopping Cart application to demonstrate these concepts.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Unidirectional Data Flow means<\/li>\n\n\n\n<li>Why UDF matters<\/li>\n\n\n\n<li>UI events and actions<\/li>\n\n\n\n<li>Immutable UI state<\/li>\n\n\n\n<li>StateFlow integration<\/li>\n\n\n\n<li>ViewModel event handling<\/li>\n\n\n\n<li>Reducer-style state updates<\/li>\n\n\n\n<li>Side effects<\/li>\n\n\n\n<li>Navigation handling<\/li>\n\n\n\n<li>Snackbar handling<\/li>\n\n\n\n<li>State hoisting<\/li>\n\n\n\n<li>UDF vs MVVM vs MVI<\/li>\n\n\n\n<li>Practical Shopping Cart implementation<\/li>\n\n\n\n<li>Testing UDF<\/li>\n\n\n\n<li>Common mistakes<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is Unidirectional Data Flow?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unidirectional Data Flow is a state management approach where information follows a predictable direction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI sends user actions to a state holder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The state holder processes those actions and updates the state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI observes the new state and renders it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic UDF Architecture<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"> <code>         User Interaction\n                 |\n                 v\n          Jetpack Compose\n                 |\n                 | UI Event\n                 v\n              ViewModel\n                 |\n                 | Process Event\n                 v\n            Update State\n                 |\n                 v\n              StateFlow\n                 |\n                 | New UI State\n                 v\n          Jetpack Compose<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel owns the screen state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI does not directly modify that state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead, the UI sends actions describing what the user wants to do.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Predictable state transitions<\/li>\n\n\n\n<li>Clear state ownership<\/li>\n\n\n\n<li>Easier debugging<\/li>\n\n\n\n<li>Better testability<\/li>\n\n\n\n<li>Reduced accidental coupling<\/li>\n\n\n\n<li>Consistent UI behavior<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Why Is UDF Important in Jetpack Compose?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose is a declarative UI toolkit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of manually modifying UI widgets, developers describe the UI based on state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Traditional Imperative Approach<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>textView.text = \"Cart: 3 items\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The application directly changes a UI component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Declarative Compose Approach<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun CartSummary(itemCount: Int) {\n\n    Text(\n        text = \"Cart: $itemCount items\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When itemCount changes, Compose can update the displayed text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important Principle<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI should be driven by state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UI = Render(State)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">UDF complements this model by defining how user actions produce new state.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Understanding the Three Core Parts of UDF<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A typical UDF implementation contains three main concepts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Events<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Events describe user actions or other inputs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AddProduct<\/li>\n\n\n\n<li>RemoveProduct<\/li>\n\n\n\n<li>IncreaseQuantity<\/li>\n\n\n\n<li>DecreaseQuantity<\/li>\n\n\n\n<li>Checkout<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">State represents what the screen currently displays.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cart products<\/li>\n\n\n\n<li>Total amount<\/li>\n\n\n\n<li>Loading status<\/li>\n\n\n\n<li>Error message<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. State Holder<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The state holder processes events and produces updated state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Android, ViewModel is commonly used as the screen-level state holder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Architecture<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Events\n   |\n   v\nViewModel\n   |\n   v\nState\n   |\n   v\nCompose UI<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. What Are UI Events?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">UI events represent interactions that the application needs to handle.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sealed interface CartAction {\n\n    data class AddProduct(\n        val productId: Int\n    ) : CartAction\n\n    data class RemoveProduct(\n        val productId: Int\n    ) : CartAction\n\n    data class IncreaseQuantity(\n        val productId: Int\n    ) : CartAction\n\n    data class DecreaseQuantity(\n        val productId: Int\n    ) : CartAction\n\n    data object Checkout : CartAction\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each action represents an intention.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI sends an action to the ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel decides how the state should change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use a Sealed Interface?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A sealed interface defines a known set of action types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A when expression can handle all supported actions explicitly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A sealed action type is useful for complex screens, but simple screens can use individual ViewModel functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">UDF does not require every application to use a single event dispatcher.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Understanding Immutable UI State<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Immutable UI state represents a snapshot of the screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class CartUiState(\n    val items: List&lt;CartItem&gt; = emptyList(),\n    val totalPrice: Double = 0.0,\n    val isCheckingOut: Boolean = false,\n    val errorMessage: String? = null\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The state object describes the current UI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When data changes, the ViewModel creates a new state value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example State Update<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>_uiState.update { current -&gt;\n\n    current.copy(\n        isCheckingOut = true\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Predictable updates<\/li>\n\n\n\n<li>Easier testing<\/li>\n\n\n\n<li>Clear state ownership<\/li>\n\n\n\n<li>Reduced accidental mutation<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer immutable collections and data classes for UI state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid exposing mutable collections that other components can modify without producing a new state value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. StateFlow in UDF Architecture<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is commonly used to expose UI state from ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class CartViewModel : ViewModel() {\n\n    private val _uiState =\n        MutableStateFlow(CartUiState())\n\n    val uiState: StateFlow&lt;CartUiState&gt; =\n        _uiState.asStateFlow()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel owns MutableStateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI observes the read-only StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This prevents the UI from directly modifying ViewModel state.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Handling Events in ViewModel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A ViewModel can expose a function that accepts actions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun onAction(action: CartAction) {\n\n    when (action) {\n\n        is CartAction.AddProduct -&gt; {\n            addProduct(action.productId)\n        }\n\n        is CartAction.RemoveProduct -&gt; {\n            removeProduct(action.productId)\n        }\n\n        is CartAction.IncreaseQuantity -&gt; {\n            increaseQuantity(action.productId)\n        }\n\n        is CartAction.DecreaseQuantity -&gt; {\n            decreaseQuantity(action.productId)\n        }\n\n        CartAction.Checkout -&gt; {\n            checkout()\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel acts as the central event handler.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each action is processed according to application rules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Centralized event handling<\/li>\n\n\n\n<li>Predictable behavior<\/li>\n\n\n\n<li>Easier testing<\/li>\n\n\n\n<li>Clear separation of concerns<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. What Is a Reducer?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A reducer is a function that takes the current state and an action and produces a new state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conceptually:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>New State = Reduce(Current State, Action)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class CounterState(\n    val count: Int = 0\n)\n\nsealed interface CounterAction {\n\n    data object Increment : CounterAction\n\n    data object Decrement : CounterAction\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reducer Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun reduce(\n    state: CounterState,\n    action: CounterAction\n): CounterState {\n\n    return when (action) {\n\n        CounterAction.Increment -&gt; {\n            state.copy(count = state.count + 1)\n        }\n\n        CounterAction.Decrement -&gt; {\n            state.copy(count = state.count - 1)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The reducer is a pure function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not perform network requests, database operations, or navigation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It calculates the next state from its inputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Deterministic behavior<\/li>\n\n\n\n<li>Easy unit testing<\/li>\n\n\n\n<li>Reusable transition logic<\/li>\n\n\n\n<li>Clear separation between state changes and side effects<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. What Are Side Effects?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A side effect is an operation that interacts with something outside a pure state calculation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Network requests<\/li>\n\n\n\n<li>Database writes<\/li>\n\n\n\n<li>Logging<\/li>\n\n\n\n<li>Navigation<\/li>\n\n\n\n<li>Showing a Snackbar<\/li>\n\n\n\n<li>Requesting permissions<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun loadProducts() {\n\n    viewModelScope.launch {\n\n        val products = repository.getProducts()\n\n        _uiState.update {\n            it.copy(products = products)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The repository request is a side effect.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Updating state afterward makes the result observable to the UI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not every side effect belongs inside ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, UI-specific operations such as displaying a Snackbar should normally be performed by the UI layer in response to an appropriate state or event.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. UDF vs MVVM vs MVI<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These concepts are related but not identical.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Concept<\/th><th>Primary Focus<\/th><\/tr><tr><td>MVVM<\/td><td>Separating View and ViewModel responsibilities<\/td><\/tr><tr><td>UDF<\/td><td>Predictable direction of actions and state<\/td><\/tr><tr><td>MVI<\/td><td>Intent-driven state transitions, often with reducers<\/td><\/tr><tr><td>Clean Architecture<\/td><td>Application boundaries and dependency direction<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Can They Work Together?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A modern Android application can use:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clean Architecture for dependency boundaries<\/li>\n\n\n\n<li>MVVM for presentation organization<\/li>\n\n\n\n<li>UDF for state management<\/li>\n\n\n\n<li>Reducer-style functions for complex state transitions<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the simplest combination that solves your application&#8217;s requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not introduce unnecessary architectural abstractions into small screens.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Practical Mini-Project: Shopping Cart Using UDF<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build a Shopping Cart application using Jetpack Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Features<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Display available products<\/li>\n\n\n\n<li>Add products to cart<\/li>\n\n\n\n<li>Increase quantity<\/li>\n\n\n\n<li>Decrease quantity<\/li>\n\n\n\n<li>Remove products<\/li>\n\n\n\n<li>Calculate cart total<\/li>\n\n\n\n<li>Display cart statistics<\/li>\n\n\n\n<li>Handle checkout confirmation<\/li>\n\n\n\n<li>Manage UI state with StateFlow<\/li>\n\n\n\n<li>Process actions using UDF<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Technology Stack<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin<\/li>\n\n\n\n<li>Jetpack Compose<\/li>\n\n\n\n<li>Material 3<\/li>\n\n\n\n<li>ViewModel<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>Kotlin Coroutines<\/li>\n\n\n\n<li>MVVM<\/li>\n\n\n\n<li>UDF<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This project uses in-memory state to keep the focus on UDF.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A production application would normally use a repository and persistent storage for cart data.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. Recommended Project Structure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>com.example.udfcart\/\n\u2502\n\u251c\u2500\u2500 model\/\n\u2502   \u251c\u2500\u2500 Product.kt\n\u2502   \u2514\u2500\u2500 CartItem.kt\n\u2502\n\u251c\u2500\u2500 presentation\/\n\u2502   \u251c\u2500\u2500 CartAction.kt\n\u2502   \u251c\u2500\u2500 CartUiState.kt\n\u2502   \u251c\u2500\u2500 CartViewModel.kt\n\u2502   \u2514\u2500\u2500 CartScreen.kt\n\u2502\n\u2514\u2500\u2500 MainActivity.kt<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. Step 1 \u2014 Create the Product Model<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>model\/Product.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.udfcart.model\n\ndata class Product(\n    val id: Int,\n    val name: String,\n    val priceInPaise: Long\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each product contains a unique identifier, name, and price.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We store the price in paise using Long rather than Double.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This avoids common floating-point rounding problems when calculating currency totals.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. Step 2 \u2014 Create the Cart Item Model<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>model\/CartItem.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.udfcart.model\n\ndata class CartItem(\n    val product: Product,\n    val quantity: Int\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CartItem represents a product and its selected quantity.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">14. Step 3 \u2014 Create the Cart Actions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/CartAction.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.udfcart.presentation\n\nsealed interface CartAction {\n\n    data class AddProduct(\n        val productId: Int\n    ) : CartAction\n\n    data class RemoveProduct(\n        val productId: Int\n    ) : CartAction\n\n    data class IncreaseQuantity(\n        val productId: Int\n    ) : CartAction\n\n    data class DecreaseQuantity(\n        val productId: Int\n    ) : CartAction\n\n    data object Checkout : CartAction\n\n    data object CheckoutMessageShown : CartAction\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">All supported user actions are defined in one place.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel processes these actions and updates UI state.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">15. Step 4 \u2014 Create the UI State<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/CartUiState.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.udfcart.presentation\n\nimport com.example.udfcart.model.CartItem\nimport com.example.udfcart.model.Product\n\ndata class CartUiState(\n    val products: List&lt;Product&gt; = emptyList(),\n    val cartItems: List&lt;CartItem&gt; = emptyList(),\n    val totalPriceInPaise: Long = 0L,\n    val totalQuantity: Int = 0,\n    val checkoutMessage: String? = null\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CartUiState contains everything required to render the shopping cart screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The checkout message is represented as state so it can be acknowledged after display.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">16. Step 5 \u2014 Create the Cart ViewModel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/CartViewModel.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.udfcart.presentation\n\nimport androidx.lifecycle.ViewModel\n\nimport com.example.udfcart.model.CartItem\nimport com.example.udfcart.model.Product\n\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.asStateFlow\nimport kotlinx.coroutines.flow.update\n\nclass CartViewModel : ViewModel() {\n\n    private val availableProducts = listOf(\n        Product(\n            id = 1,\n            name = \"Android Phone\",\n            priceInPaise = 1_999_900L\n        ),\n        Product(\n            id = 2,\n            name = \"Bluetooth Speaker\",\n            priceInPaise = 249_900L\n        ),\n        Product(\n            id = 3,\n            name = \"Smart Watch\",\n            priceInPaise = 499_900L\n        ),\n        Product(\n            id = 4,\n            name = \"Wireless Headphones\",\n            priceInPaise = 299_900L\n        )\n    )\n\n    private val _uiState = MutableStateFlow(\n        CartUiState(products = availableProducts)\n    )\n\n    val uiState: StateFlow&lt;CartUiState&gt; =\n        _uiState.asStateFlow()\n\n    fun onAction(action: CartAction) {\n\n        when (action) {\n\n            is CartAction.AddProduct -&gt; {\n                addProduct(action.productId)\n            }\n\n            is CartAction.RemoveProduct -&gt; {\n                removeProduct(action.productId)\n            }\n\n            is CartAction.IncreaseQuantity -&gt; {\n                addProduct(action.productId)\n            }\n\n            is CartAction.DecreaseQuantity -&gt; {\n                decreaseQuantity(action.productId)\n            }\n\n            CartAction.Checkout -&gt; {\n                checkout()\n            }\n\n            CartAction.CheckoutMessageShown -&gt; {\n                _uiState.update {\n                    it.copy(checkoutMessage = null)\n                }\n            }\n        }\n    }\n\n    private fun addProduct(productId: Int) {\n\n        val product = availableProducts\n            .firstOrNull { it.id == productId }\n            ?: return\n\n        _uiState.update { state -&gt;\n\n            val existingItem = state.cartItems\n                .firstOrNull {\n                    it.product.id == productId\n                }\n\n            val updatedItems = if (existingItem == null) {\n\n                state.cartItems + CartItem(\n                    product = product,\n                    quantity = 1\n                )\n\n            } else {\n\n                state.cartItems.map { item -&gt;\n\n                    if (item.product.id == productId) {\n\n                        item.copy(\n                            quantity = item.quantity + 1\n                        )\n\n                    } else {\n\n                        item\n                    }\n                }\n            }\n\n            state.withUpdatedCart(updatedItems)\n        }\n    }\n\n    private fun removeProduct(productId: Int) {\n\n        _uiState.update { state -&gt;\n\n            val updatedItems = state.cartItems.filterNot {\n                it.product.id == productId\n            }\n\n            state.withUpdatedCart(updatedItems)\n        }\n    }\n\n    private fun decreaseQuantity(productId: Int) {\n\n        _uiState.update { state -&gt;\n\n            val updatedItems = state.cartItems.mapNotNull { item -&gt;\n\n                if (item.product.id != productId) {\n\n                    item\n\n                } else if (item.quantity &gt; 1) {\n\n                    item.copy(\n                        quantity = item.quantity - 1\n                    )\n\n                } else {\n\n                    null\n                }\n            }\n\n            state.withUpdatedCart(updatedItems)\n        }\n    }\n\n    private fun checkout() {\n\n        _uiState.update { state -&gt;\n\n            if (state.cartItems.isEmpty()) {\n\n                state.copy(\n                    checkoutMessage = \"Your cart is empty\"\n                )\n\n            } else {\n\n                state.withUpdatedCart(\n                    updatedItems = emptyList()\n                ).copy(\n                    checkoutMessage =\n                        \"Demo checkout completed successfully\"\n                )\n            }\n        }\n    }\n\n    private fun CartUiState.withUpdatedCart(\n        updatedItems: List&lt;CartItem&gt;\n    ): CartUiState {\n\n        return copy(\n            cartItems = updatedItems,\n            totalQuantity = updatedItems.sumOf {\n                it.quantity\n            },\n            totalPriceInPaise = updatedItems.sumOf {\n                it.product.priceInPaise * it.quantity\n            }\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel receives actions and updates immutable state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Every cart mutation recalculates:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cart items<\/li>\n\n\n\n<li>Total quantity<\/li>\n\n\n\n<li>Total price<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This prevents different parts of the UI from maintaining conflicting totals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The checkout operation is a demonstration only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It clears the local cart and displays a confirmation message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not process payments or place a real order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Production checkout should be handled through a secure backend with server-authoritative pricing and transaction validation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">17. Step 6 \u2014 Create the Shopping Cart Screen<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>presentation\/CartScreen.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.udfcart.presentation\n\nimport androidx.compose.foundation.layout.*\nimport androidx.compose.foundation.lazy.LazyColumn\nimport androidx.compose.foundation.lazy.items\nimport androidx.compose.material3.*\nimport androidx.compose.runtime.*\nimport androidx.compose.ui.Alignment\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.unit.dp\n\nimport androidx.lifecycle.compose.collectAsStateWithLifecycle\n\nimport com.example.udfcart.model.CartItem\nimport com.example.udfcart.model.Product\n\n@OptIn(ExperimentalMaterial3Api::class)\n@Composable\nfun CartScreen(\n    viewModel: CartViewModel\n) {\n\n    val uiState by viewModel.uiState\n        .collectAsStateWithLifecycle()\n\n    val snackbarHostState = remember {\n        SnackbarHostState()\n    }\n\n    LaunchedEffect(uiState.checkoutMessage) {\n\n        val message = uiState.checkoutMessage\n\n        if (message != null) {\n\n            snackbarHostState.showSnackbar(message)\n\n            viewModel.onAction(\n                CartAction.CheckoutMessageShown\n            )\n        }\n    }\n\n    Scaffold(\n        topBar = {\n\n            TopAppBar(\n                title = {\n                    Text(\"UDF Shopping Cart\")\n                }\n            )\n        },\n        snackbarHost = {\n            SnackbarHost(snackbarHostState)\n        }\n    ) { paddingValues -&gt;\n\n        Column(\n            modifier = Modifier\n                .fillMaxSize()\n                .padding(paddingValues)\n                .padding(16.dp),\n            verticalArrangement =\n                Arrangement.spacedBy(12.dp)\n        ) {\n\n            Text(\n                text = \"Available Products\",\n                style = MaterialTheme.typography.titleLarge\n            )\n\n            uiState.products.forEach { product -&gt;\n\n                ProductRow(\n                    product = product,\n                    onAdd = {\n\n                        viewModel.onAction(\n                            CartAction.AddProduct(product.id)\n                        )\n                    }\n                )\n            }\n\n            HorizontalDivider()\n\n            Text(\n                text = \"Your Cart\",\n                style = MaterialTheme.typography.titleLarge\n            )\n\n            if (uiState.cartItems.isEmpty()) {\n\n                Text(\"Your cart is empty\")\n\n            } else {\n\n                LazyColumn(\n                    modifier = Modifier.weight(1f),\n                    verticalArrangement =\n                        Arrangement.spacedBy(8.dp)\n                ) {\n\n                    items(\n                        items = uiState.cartItems,\n                        key = { it.product.id }\n                    ) { item -&gt;\n\n                        CartItemRow(\n                            item = item,\n                            onIncrease = {\n\n                                viewModel.onAction(\n                                    CartAction.IncreaseQuantity(\n                                        item.product.id\n                                    )\n                                )\n                            },\n                            onDecrease = {\n\n                                viewModel.onAction(\n                                    CartAction.DecreaseQuantity(\n                                        item.product.id\n                                    )\n                                )\n                            },\n                            onRemove = {\n\n                                viewModel.onAction(\n                                    CartAction.RemoveProduct(\n                                        item.product.id\n                                    )\n                                )\n                            }\n                        )\n                    }\n                }\n            }\n\n            Text(\n                text = \"Items: ${uiState.totalQuantity}\",\n                style = MaterialTheme.typography.titleMedium\n            )\n\n            Text(\n                text = \"Total: ${formatPrice(uiState.totalPriceInPaise)}\",\n                style = MaterialTheme.typography.titleLarge\n            )\n\n            Button(\n                onClick = {\n\n                    viewModel.onAction(\n                        CartAction.Checkout\n                    )\n                },\n                modifier = Modifier.fillMaxWidth()\n            ) {\n\n                Text(\"Checkout\")\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CartScreen observes UI state using collectAsStateWithLifecycle().<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It forwards user actions to the ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI does not directly modify cart data.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">18. Step 7 \u2014 Create the Product Row<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add this composable to CartScreen.kt.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun ProductRow(\n    product: Product,\n    onAdd: () -&gt; Unit\n) {\n\n    Card(\n        modifier = Modifier.fillMaxWidth()\n    ) {\n\n        Row(\n            modifier = Modifier\n                .fillMaxWidth()\n                .padding(12.dp),\n            verticalAlignment =\n                Alignment.CenterVertically\n        ) {\n\n            Column(\n                modifier = Modifier.weight(1f)\n            ) {\n\n                Text(\n                    text = product.name,\n                    style = MaterialTheme.typography.titleMedium\n                )\n\n                Text(\n                    text = formatPrice(product.priceInPaise)\n                )\n            }\n\n            Button(\n                onClick = onAdd\n            ) {\n\n                Text(\"Add\")\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ProductRow displays product information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It receives a callback rather than depending on ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This makes it reusable and easier to test.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">19. Step 8 \u2014 Create the Cart Item Row<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add this composable to CartScreen.kt.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun CartItemRow(\n    item: CartItem,\n    onIncrease: () -&gt; Unit,\n    onDecrease: () -&gt; Unit,\n    onRemove: () -&gt; Unit\n) {\n\n    Card(\n        modifier = Modifier.fillMaxWidth()\n    ) {\n\n        Column(\n            modifier = Modifier.padding(12.dp),\n            verticalArrangement =\n                Arrangement.spacedBy(8.dp)\n        ) {\n\n            Text(\n                text = item.product.name,\n                style = MaterialTheme.typography.titleMedium\n            )\n\n            Text(\n                text = formatPrice(\n                    item.product.priceInPaise * item.quantity\n                )\n            )\n\n            Row(\n                verticalAlignment =\n                    Alignment.CenterVertically,\n                horizontalArrangement =\n                    Arrangement.spacedBy(8.dp)\n            ) {\n\n                OutlinedButton(\n                    onClick = onDecrease\n                ) {\n\n                    Text(\"-\")\n                }\n\n                Text(\n                    text = item.quantity.toString()\n                )\n\n                OutlinedButton(\n                    onClick = onIncrease\n                ) {\n\n                    Text(\"+\")\n                }\n\n                TextButton(\n                    onClick = onRemove\n                ) {\n\n                    Text(\"Remove\")\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CartItemRow displays the quantity and subtotal for a product.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All user interactions are forwarded through callbacks.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">20. Step 9 \u2014 Create a Price Formatting Function<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add this function to CartScreen.kt.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private fun formatPrice(\n    priceInPaise: Long\n): String {\n\n    val rupees = priceInPaise \/ 100\n    val paise = priceInPaise % 100\n\n    return \"\u20b9$rupees.${paise.toString().padStart(2, '0')}\"\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The function converts integer paise into a readable rupee amount.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production applications, use locale-aware currency formatting.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">21. Step 10 \u2014 Create MainActivity<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File: <code>MainActivity.kt<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.udfcart\n\nimport android.os.Bundle\n\nimport androidx.activity.ComponentActivity\nimport androidx.activity.compose.setContent\nimport androidx.compose.material3.MaterialTheme\nimport androidx.lifecycle.viewmodel.compose.viewModel\n\nimport com.example.udfcart.presentation.CartScreen\nimport com.example.udfcart.presentation.CartViewModel\n\nclass MainActivity : ComponentActivity() {\n\n    override fun onCreate(\n        savedInstanceState: Bundle?\n    ) {\n\n        super.onCreate(savedInstanceState)\n\n        setContent {\n\n            MaterialTheme {\n\n                val cartViewModel: CartViewModel =\n                    viewModel()\n\n                CartScreen(\n                    viewModel = cartViewModel\n                )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MainActivity displays the Shopping Cart screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel owns the cart state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Activity does not contain cart business logic.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. Required Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create an Android project with Jetpack Compose enabled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure the following dependencies are available.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    implementation(\n        \"androidx.activity:activity-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-ktx:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-runtime-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"org.jetbrains.kotlinx:kotlinx-coroutines-android:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.compose.material3:material3\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace placeholders with compatible stable versions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the Compose BOM to manage Compose library versions when applicable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. Expected Application UI<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Initial Screen<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|        UDF Shopping Cart       |\n|                                |\n| Available Products             |\n|                                |\n| Android Phone         &#91;Add]    |\n| \u20b919999.00                      |\n|                                |\n| Bluetooth Speaker     &#91;Add]    |\n| \u20b92499.00                       |\n|                                |\n| Smart Watch           &#91;Add]    |\n| \u20b94999.00                       |\n|                                |\n| Wireless Headphones   &#91;Add]    |\n| \u20b92999.00                       |\n|                                |\n| Your Cart                      |\n|                                |\n| Your cart is empty             |\n|                                |\n| Items: 0                       |\n| Total: \u20b90.00                   |\n|                                |\n|          &#91;Checkout]            |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">After Adding Products<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|        UDF Shopping Cart       |\n|                                |\n| Available Products             |\n| ...                            |\n|                                |\n| Your Cart                      |\n|                                |\n| Android Phone                  |\n| \u20b919999.00                      |\n| &#91;-] 1 &#91;+] &#91;Remove]             |\n|                                |\n| Bluetooth Speaker              |\n| \u20b94998.00                       |\n| &#91;-] 2 &#91;+] &#91;Remove]             |\n|                                |\n| Items: 3                       |\n| Total: \u20b924997.00               |\n|                                |\n|          &#91;Checkout]            |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI automatically updates when cart state changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel recalculates totals after every cart mutation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. How the Complete UDF Application Works<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: User Taps Add<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The user taps Add for a product.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: UI Sends an Action<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>viewModel.onAction(\n    CartAction.AddProduct(product.id)\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: ViewModel Processes the Action<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel finds the selected product.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: State Is Updated<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel creates a new cart state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: StateFlow Emits the Latest State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The new CartUiState becomes available to collectors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Compose Observes the State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">collectAsStateWithLifecycle() receives the updated state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: UI Recomposition<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compose updates the cart list, quantity, and total price.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This demonstrates the complete UDF cycle.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. Handling Navigation in UDF<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Navigation is a UI responsibility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A ViewModel should generally express that an operation has completed or that navigation is needed, without depending directly on NavController.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class LoginUiState(\n    val isLoggedIn: Boolean = false\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compose<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>LaunchedEffect(uiState.isLoggedIn) {\n\n    if (uiState.isLoggedIn) {\n\n        navController.navigate(\"home\") {\n\n            popUpTo(\"login\") {\n                inclusive = true\n            }\n\n            launchSingleTop = true\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel exposes login state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI reacts to that state and performs navigation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Design navigation so repeated state observation does not create duplicate destinations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use appropriate back-stack handling and idempotent navigation behavior.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Handling Snackbar Messages<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Snackbar display is a UI-specific operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A useful approach is to expose a pending message in UI state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class ScreenUiState(\n    val message: String? = null\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compose<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>LaunchedEffect(uiState.message) {\n\n    val message = uiState.message\n\n    if (message != null) {\n\n        snackbarHostState.showSnackbar(message)\n\n        viewModel.onMessageShown()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel exposes a message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI displays the Snackbar.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After handling, the UI acknowledges the message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For repeated identical messages or multiple queued messages, use unique identifiers or a queue-based state model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A single nullable string cannot represent multiple identical pending messages reliably.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">27. Reducer Pattern in Larger Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As state transitions become more complex, a dedicated reducer can improve organization.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class CounterUiState(\n    val count: Int = 0\n)\n\nsealed interface CounterAction {\n\n    data object Increment : CounterAction\n\n    data object Decrement : CounterAction\n\n    data object Reset : CounterAction\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reducer<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun counterReducer(\n    state: CounterUiState,\n    action: CounterAction\n): CounterUiState {\n\n    return when (action) {\n\n        CounterAction.Increment -&gt; {\n            state.copy(count = state.count + 1)\n        }\n\n        CounterAction.Decrement -&gt; {\n            state.copy(count = state.count - 1)\n        }\n\n        CounterAction.Reset -&gt; {\n            state.copy(count = 0)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ViewModel Integration<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun onAction(action: CounterAction) {\n\n    _uiState.update { current -&gt;\n\n        counterReducer(current, action)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pure state transitions<\/li>\n\n\n\n<li>Easier unit testing<\/li>\n\n\n\n<li>Predictable behavior<\/li>\n\n\n\n<li>Clearer event processing<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">28. Testing UDF State Transitions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One of the main benefits of UDF is predictable testing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example ViewModel Test<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Test\nfun `adding product updates cart state`() {\n\n    val viewModel = CartViewModel()\n\n    viewModel.onAction(\n        CartAction.AddProduct(1)\n    )\n\n    val state = viewModel.uiState.value\n\n    assertEquals(\n        1,\n        state.totalQuantity\n    )\n\n    assertEquals(\n        1_999_900L,\n        state.totalPriceInPaise\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The test sends an action and verifies the resulting state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No Android UI is required.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the appropriate JUnit dependencies and test environment for your project.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">29. Testing Reducers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pure reducers are especially easy to test.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Test\nfun `increment increases count`() {\n\n    val initialState = CounterUiState(\n        count = 5\n    )\n\n    val result = counterReducer(\n        initialState,\n        CounterAction.Increment\n    )\n\n    assertEquals(\n        6,\n        result.count\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The reducer receives inputs and returns a result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not require coroutines, a database, or Android framework components.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">30. Common UDF Architecture Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Modifying State Directly from Composables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep screen-level mutable state owned by the appropriate state holder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Using Mutable Collections in UI State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer immutable snapshots.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Performing Business Logic in UI Components<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Move reusable business logic into ViewModels, use cases, or domain components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Creating Too Many Event Types<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not introduce unnecessary event abstractions for simple screens.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Using SharedFlow Without Considering Lifecycle<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Transient event delivery may be lost when the UI is inactive.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Mixing Navigation Logic into Repositories<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep navigation decisions and execution in appropriate presentation components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Ignoring Coroutine Cancellation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Preserve cancellation when handling asynchronous operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Recalculating State in Multiple Places<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Centralize important state transformations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Making ViewModels Too Large<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Extract complex business rules into appropriate domain components.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 10: Skipping State Transition Tests<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Test actions and their expected results.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">31. Production Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep UI state immutable.<\/li>\n\n\n\n<li>Expose read-only StateFlow.<\/li>\n\n\n\n<li>Define clear state ownership.<\/li>\n\n\n\n<li>Use Unidirectional Data Flow.<\/li>\n\n\n\n<li>Keep composables focused on rendering.<\/li>\n\n\n\n<li>Forward user actions through callbacks.<\/li>\n\n\n\n<li>Use ViewModel for screen-level state.<\/li>\n\n\n\n<li>Use repositories for data access.<\/li>\n\n\n\n<li>Use pure reducers when helpful.<\/li>\n\n\n\n<li>Keep side effects outside pure reducers.<\/li>\n\n\n\n<li>Handle coroutine cancellation correctly.<\/li>\n\n\n\n<li>Use lifecycle-aware Flow collection.<\/li>\n\n\n\n<li>Model important outcomes as state.<\/li>\n\n\n\n<li>Acknowledge handled messages and outcomes.<\/li>\n\n\n\n<li>Avoid unnecessary architectural complexity.<\/li>\n\n\n\n<li>Test state transitions independently.<\/li>\n\n\n\n<li>Use persistent storage for durable data.<\/li>\n\n\n\n<li>Profile performance before optimizing.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">32. UDF Quick Reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Concept<\/th><th>Purpose<\/th><\/tr><tr><td>UDF<\/td><td>Predictable action and state flow<\/td><\/tr><tr><td>UI Action<\/td><td>Represents user intention<\/td><\/tr><tr><td>UI State<\/td><td>Current screen representation<\/td><\/tr><tr><td>ViewModel<\/td><td>Screen-level state holder<\/td><\/tr><tr><td>StateFlow<\/td><td>Observable state<\/td><\/tr><tr><td>MutableStateFlow<\/td><td>Mutable state holder<\/td><\/tr><tr><td>Reducer<\/td><td>Pure state transition function<\/td><\/tr><tr><td>Side Effect<\/td><td>External operation<\/td><\/tr><tr><td>State Hoisting<\/td><td>Moving state ownership upward<\/td><\/tr><tr><td>MVVM<\/td><td>Presentation architecture<\/td><\/tr><tr><td>MVI<\/td><td>Intent-driven state management approach<\/td><\/tr><tr><td>Repository<\/td><td>Data access abstraction<\/td><\/tr><tr><td>collectAsStateWithLifecycle()<\/td><td>Lifecycle-aware state collection<\/td><\/tr><tr><td>LaunchedEffect<\/td><td>Compose-managed coroutine side effect<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33. Practical Exercises<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1: Counter UDF<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build a counter using actions, state, and a reducer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Login Form<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create login state with email, password, loading, and error information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: Product List<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Implement AddProduct and RemoveProduct actions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: Shopping Cart<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build the complete Shopping Cart application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: Quantity Controls<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add increase and decrease quantity actions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: Checkout State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add loading and completion states for a simulated checkout.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: Snackbar<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Display and acknowledge a confirmation message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: Navigation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to a success screen after a completed operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: Reducer Testing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Write unit tests for pure state transitions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 10: Repository Integration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Replace in-memory cart state with a repository backed by Room.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">34. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Unidirectional Data Flow in Android?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">UDF is an approach where user actions and state updates follow predictable paths.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why use UDF with Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Compose renders UI from state, making UDF a natural approach for managing interactions and updates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is UDF the same as MVVM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MVVM organizes presentation responsibilities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">UDF defines the direction of actions and state updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They can be used together.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between UDF and MVI?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">UDF is a general data-flow principle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MVI is a more specific architectural approach often built around intents, state, and reducer-style transitions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a reducer?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A reducer is a function that calculates new state from current state and an action.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should every Android application use reducers?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reducers are useful when state transitions become complex.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How should navigation be handled?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI should generally execute navigation in response to appropriate application state or presentation signals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can StateFlow be used with UDF?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is commonly used to expose observable UI state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I test UDF?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Send actions to the state holder and verify the resulting state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pure reducers can be tested independently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is UDF suitable for production Android applications?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">UDF can improve predictability, maintainability, and testability when implemented appropriately.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What We&#8217;ve Learned<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we explored:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Unidirectional Data Flow fundamentals<\/li>\n\n\n\n<li>UI actions and events<\/li>\n\n\n\n<li>Immutable UI state<\/li>\n\n\n\n<li>ViewModel event handling<\/li>\n\n\n\n<li>StateFlow integration<\/li>\n\n\n\n<li>Reducer-style state updates<\/li>\n\n\n\n<li>Side effects<\/li>\n\n\n\n<li>UDF vs MVVM vs MVI<\/li>\n\n\n\n<li>Jetpack Compose integration<\/li>\n\n\n\n<li>Practical Shopping Cart application<\/li>\n\n\n\n<li>Navigation handling<\/li>\n\n\n\n<li>Snackbar handling<\/li>\n\n\n\n<li>Reducer testing<\/li>\n\n\n\n<li>Common mistakes<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Unidirectional Data Flow is an important concept for building predictable Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining Kotlin, Jetpack Compose, ViewModel, StateFlow, and clear state ownership, developers can create interfaces that are easier to understand, test, and maintain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The most important principle is simple:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>User actions flow toward the state holder, and updated state flows back to the UI.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In our next tutorial:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Article 43 \u2014 Dependency Injection in Android with Kotlin: Manual DI, Constructor Injection &amp; Hilt Fundamentals<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Dependency Injection is<\/li>\n\n\n\n<li>Why Dependency Injection matters<\/li>\n\n\n\n<li>Dependency Inversion Principle<\/li>\n\n\n\n<li>Constructor Injection<\/li>\n\n\n\n<li>Manual Dependency Injection<\/li>\n\n\n\n<li>Dependency Containers<\/li>\n\n\n\n<li>Interface-based dependencies<\/li>\n\n\n\n<li>Hilt fundamentals<\/li>\n\n\n\n<li>ViewModel injection<\/li>\n\n\n\n<li>Repository injection<\/li>\n\n\n\n<li>Testing with fake dependencies<\/li>\n\n\n\n<li>Practical Android mini-project<\/li>\n\n\n\n<li>Production best practices<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Happy Coding!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning modern Android development with <strong>Moiks Developer Blog<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Unidirectional Data Flow (UDF) in Android using Kotlin, Jetpack Compose, ViewModel, and StateFlow. Learn UI events, immutable state, side effects, reducer patterns, and build a practical Shopping Cart application.<\/p>\n","protected":false},"author":1,"featured_media":188,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[3,333,8],"tags":[],"class_list":["post-187","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-android-tutorials","category-android-architecture"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions\/189"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/188"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}