<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: ‪Mohammed khudair‬‏</title>
    <description>The latest articles on Forem by ‪Mohammed khudair‬‏ (@mohammed_57).</description>
    <link>https://forem.com/mohammed_57</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1451489%2F68eab32a-05a3-4438-80bd-daa6a1995e85.jpg</url>
      <title>Forem: ‪Mohammed khudair‬‏</title>
      <link>https://forem.com/mohammed_57</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohammed_57"/>
    <language>en</language>
    <item>
      <title>MVVM Architectural Pattern in Android</title>
      <dc:creator>‪Mohammed khudair‬‏</dc:creator>
      <pubDate>Mon, 20 May 2024 15:00:56 +0000</pubDate>
      <link>https://forem.com/mohammed_57/mvvm-architectural-pattern-in-android-j03</link>
      <guid>https://forem.com/mohammed_57/mvvm-architectural-pattern-in-android-j03</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2adhyntmhmtdhx42w75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2adhyntmhmtdhx42w75.png" alt="This diagram illustrates communications between the components." width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;MVVM is a recognized software design pattern that enhances code maintainability by separating the user interface (View) from the brainy business logic (Model) through an intermediary component called the ViewModel. This approach improves testability and reusability. MVVM overcomes the drawbacks of MVP and MVC, providing a clean and structured codebase. It ensures a clear understanding of crucial logic components in Android applications, facilitating feature management.&lt;/p&gt;

&lt;p&gt;With MVVM, understanding what's happening in our Android app is a breeze, making it a snap to add or remove features. Plus, it's a team player during Unit Testing, ensuring our code gets all the attention it deserves without interference from other classes. Go, MVVM! 🚀✨.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is MVVM?
&lt;/h2&gt;

&lt;p&gt;"In simple terms, MVVM stands for Model-View-ViewModel. Its primary goal is to enhance the clarity and comprehensibility of our code. MVVM champions testability, maintainability, reusability, and scalability, making it a powerful approach for developing robust and user-friendly applications."&lt;/p&gt;

&lt;h3&gt;
  
  
  Main Components
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Model:&lt;/strong&gt;&lt;br&gt;
represents the data and business logic of the application, typically does not have any direct knowledge of the user interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;View:&lt;/strong&gt;&lt;br&gt;
This layer represents the user interface elements. It focuses on displaying data and handling user interactions without any logic or knowledge about the data itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ViewModel:&lt;/strong&gt;&lt;br&gt;
Acts as a bridge between the View and the Model. It exposes data to the View through a mechanism like StateFlow, KotlinFlow, or a similar one. It also manages the lifecycle of the data and ensures the View only receives relevant updates.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Some Of Its Key features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Separation of Concerns:&lt;/strong&gt;&lt;br&gt;
Here each component has a specific role and responsibility, this enhances reusability and readability, where developers can focus on each component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Testability:&lt;/strong&gt;&lt;br&gt;
Each layer can be tested independently, leading to more robust and reliable apps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maintainability:&lt;/strong&gt;&lt;br&gt;
MVVM promotes clean and maintainable code by organizing responsibilities into distinct components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt;&lt;br&gt;
It is easier to add features or modify existing ones without disrupting the entire application.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How does the MVVM work?
&lt;/h3&gt;

&lt;p&gt;"The View binds to data exposed by the ViewModel, the ViewModel handles events initiated by the View and communicates with the Model, the Model processes events and manages data storage or retrieval, and the updated data flows back to the View through the ViewModel."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6et916gjd7dy3r1d1ck.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6et916gjd7dy3r1d1ck.png" alt="This diagram illustrates communications between the components." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This diagram illustrates communications between the components.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Getting started with MVVM
&lt;/h2&gt;

&lt;p&gt;Before we get started, it would be awesome if you're familiar with some cool tools like Coroutine, Hilt, and StateFlow that make our MVVM pattern journey smoother. In this article, I'll be using Jetpack Compose. Don't worry if you haven't explored Jetpack Compose yet - you can easily apply the same ideas to an Activity or Fragment.&lt;/p&gt;

&lt;p&gt;Now let's set up the necessary components. Let's kickstart the journey.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project setup&lt;/strong&gt;&lt;br&gt;
for our ViewModel add the following dependencies to your app-level &lt;code&gt;build.gradle&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies {
... other dependencies...
 implementation ("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are using Hilt make sure to add these dependencies.&lt;br&gt;
In app-level &lt;code&gt;build.gradle&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins {
 ... other plugins...
    id("com.google.dagger.hilt.android")
    kotlin("kapt")
}

... other components ...

dependencies {
... other dependencies...
  implementation("com.google.dagger:hilt-android:2.49")
  kapt("com.google.dagger:hilt-android-compiler:2.44")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In project-level &lt;code&gt;build.gradle&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins {
   ... other plugins...
    id("com.google.dagger.hilt.android") version "2.44" apply false

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re not familiar with setting up Hilt, no problem! Check out this simple guide from the &lt;a href="https://developer.android.com/training/dependency-injection/hilt-android#setup"&gt;official Hilt doc&lt;/a&gt;. It’s simple!👍&lt;/p&gt;

&lt;h3&gt;
  
  
  Define Project Structure
&lt;/h3&gt;

&lt;p&gt;Define the project structure based on the separation of concern, typically, you will have separate packages of the components&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft5uct8g1ob6uxzx7zial.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft5uct8g1ob6uxzx7zial.png" alt="Image description" width="338" height="404"&gt;&lt;/a&gt;&lt;br&gt;
Here’s a glimpse of how I’ve organized my package structure.&lt;/p&gt;
&lt;h3&gt;
  
  
  Let’s start with the “data layer”
&lt;/h3&gt;

&lt;p&gt;The data layer is considered as our &lt;strong&gt;Model Component&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data class Game(
    val id: Int,
    val title: String,
    val rating: Float,
    val review: String
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is our Game class that holds the data info.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class GameRepository @Inject constructor() {

     suspend fun getGames(): List&amp;lt;Game&amp;gt; {
         // Fake data fetching, replace this with your actual data source
         delay(3000)
        return listOf(
            Game(1, "The Witcher 3: Wild Hunt", 4.8f, "A masterpiece of storytelling and open-world design."),
            Game(2, "Red Dead Redemption 2", 4.9f, "An epic tale of life in America at the dawn of the modern age."),
            Game(3, "Assassin's Creed Valhalla", 4.5f, "Explore a dynamic and beautiful open world set against the brutal backdrop of England's Dark Ages."),
            Game(4, "Cyberpunk 2077", 4.2f, "An open-world action-adventure story set in Night City, a megalopolis obsessed with power, glamour, and body modification."),
           Game(5, "The Legend of Zelda: Breath of the Wild", 4.7f, "Step into a world of discovery, exploration, and adventure in The Legend of Zelda: Breath of the Wild."),

           // Add more games as needed
        )
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;“Here, we simulate fetching data from a server or local database just to simplify things and focus on the MVVM concept.”&lt;/p&gt;

&lt;h3&gt;
  
  
  UI Layer
&lt;/h3&gt;

&lt;p&gt;The ViewModel Component plays a crucial role as part of the MVVM, where we handle events and reduce the view state. The ViewModel acts as a middleman between the View and the Model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@HiltViewModel
class GameViewModel @Inject constructor(
    private val repository: GameRepository
) : ViewModel() {

    private val _uiState = MutableStateFlow(GameUiState(loading = true))
    val uiState: StateFlow&amp;lt;GameUiState&amp;gt; get() = _uiState

    fun getGameList() {
        _uiState.value = GameUiState(loading = true)
        viewModelScope.launch {
            try {
                val games = repository.getGames()
                _uiState.value = GameUiState(loading = false,games = games)
            } catch (e: Exception) {
                _uiState.value = GameUiState(loading = false, error =e.message?:"Network error" )
            }

        }
    }
    // Other methods business goes here..
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let’s dive into the &lt;strong&gt;View Component&lt;/strong&gt; elements: Implement the View component responsible for rendering the UI and observing the Model’s state changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GameUiState&lt;/strong&gt; This represents the view state on the screen,&lt;br&gt;
whether it’s loading or success fetching the data or an error occurs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data class GameUiState (
    val loading :Boolean = false,
    val games: List&amp;lt;Game&amp;gt; = emptyList(),
    val error: String? = null
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In the MainActivity&lt;/strong&gt; class, I’ve adopted Jetpack Compose, I put the UI design in a separate file &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GameScreen&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val viewModel by viewModels&amp;lt;GameViewModel&amp;gt;()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MVVM_PatternProjectTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    GameScreen(viewModel = viewModel)
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GameScreen file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Be careful to add this import for items.
import androidx.compose.foundation.lazy.items

@Composable
fun GameScreen(viewModel: GameViewModel) {
    val gameUiState by viewModel.uiState.collectAsState()


    LaunchedEffect(viewModel) {
        viewModel.getGameList()
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        when {
            gameUiState.loading -&amp;gt; {
                // Display a loading indicator
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    CircularProgressIndicator(color = Color.Blue)
                }
            }

            gameUiState.error != null -&amp;gt; {
                // Display an error message
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    val errorMessage = gameUiState.error
                    Text(text = "Error: $errorMessage", color = Color.Red, fontSize = 18.sp)
                }
            }

            else -&amp;gt; {
                // Display the list of games
                GameList((gameUiState.games))
            }
        }
    }

}

@Composable
fun GameList(games: List&amp;lt;Game&amp;gt;) {
    LazyColumn {
        items(games) { game -&amp;gt;
            GameListItem(game = game)
        }
    }
}

@Composable
fun GameListItem(game: Game) {
    // Display a game item
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
            .shadow(4.dp, RoundedCornerShape(8.dp))
    ) {
        Column(
            modifier = Modifier.padding(8.dp)
        ) {

            Text(
                text = game.title,
                style = TextStyle(
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold
                )
            )
            Text(
                text = game.review,
                modifier = Modifier.padding(4.dp)
            )
            Text(
                text = " Rating: ${game.rating}",
                color = Color.Gray,
                style = TextStyle(
                    fontWeight = FontWeight.Bold
                )
            )
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we’re crafting the UI for &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GameScreen &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;where we observe data from the ViewModel and manage loading states, errors, and display the games list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you’re still using views it will look something like this:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val viewModel:GameViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

      // Observe the state
       lifecycleScope.launchWhenStarted {
           viewModel.uiState.clolect { uiState -&amp;gt;
                    render(uiState)
              }

        // Launch getGamelist fun to update uiState.
        viewModel.getGameList()
    }

    override fun render(state: GameUiState) {
        // Update UI based on the state
        if (state.loading) {
            // Show loading indicator
        } else if (state.error != null) {
            // Show error message
        } else {
            // Display the list of games
            val gameList = state.games.map { it.title }
            // Update UI with gameList
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here’s the result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8r3tcs2q26svmk1cs9c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8r3tcs2q26svmk1cs9c.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congratulations&lt;/strong&gt; 🎉🎆&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkept79oteeo047tgvckh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkept79oteeo047tgvckh.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember, the adoption of MVVM is a journey, not a sprint. Start with small, manageable steps, and gradually integrate MVVM into your projects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Discover the joy of exploring! Every adventure is a chance to find something wonderful, learn something new, and transform obstacles into stepping stones toward a future filled with unexpected victories and triumphs.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;“Stay curious, stay driven, and watch your skills soar. Happy coding!”&lt;/p&gt;

</description>
      <category>mvvm</category>
      <category>android</category>
      <category>designpatterns</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
