‫Welcome back.
‫We've done with the remote data source and the repository.
‫So we finished the model layer.
‫Now let's move to the view model layer.
‫The view model is a crucial component responsible for managing and providing data to the UI, the view
‫while maintaining the separation of concerns.
‫It's part of Android architecture components and serves as a bridge between the data source, the model
‫and the UI components.
‫The view.
‫The primary purpose of a view model is to store and manage UI related data, ensuring that this data
‫persists across configuration changes like, for example, device rotation and that the UI remains responsive.
‫Let's move to Android Studio and learn how to add and create the view models.
‫I'll create a new package, I'll name it as View Model and make sure that you name your packages with
‫small letters.
‫Then inside this package I'll create a new Java class and I'll name it as Quiz View Model.
‫Again, guys, I want from you to focus with me.
‫There are a lot of concepts, a lot of details.
‫Please go back to the View model section in order to learn more about View models.
‫But don't worry, I'll explain.
‫Everything I write extends the view model.
‫We have two types of view models either Android View model or View model, and they are both from the
‫life cycle package.
‫Both the view model and Android View model are components provided by the Android architecture, components
‫that play a role in managing and persisting UI related data across configuration changes.
‫However, they serve slightly different purposes and have different life cycles.
‫For example, the regular view model is a component of the Android architecture components and is part
‫of the mVVM model view ViewModel pattern.
‫It's a basic class for a view models, and it's not tied to the Android framework.
‫This is the view model.
‫It's an abstract class.
‫It's designed to store and manage UI related data, and provides an abstraction layer between the UI
‫components, activities, and fragments and the underlying data source.
‫For example, a database, network, service or repository.
‫This separation of concerns helps in maintaining a clean and testable architecture, but a view model
‫is not aware of Android application or context, and should not be used to access Android specific components
‫like system, services, resources or preferences.
‫It makes the management of pure data and presentation logic very simple.
‫While the Android View model is a subclass of a view model that is aware of the Android application
‫context.
‫This allows you to access Android specific components such as system services, resources, and preferences,
‫making it suitable for situations where you need application wide context or access to system services.
‫In this example, I'm not going to access context, so I remove Android.
‫I'll use view model.
‫I don't need to access the context.
‫Okay, I'll start by creating a quiz repository.
‫Instance repository equals to new quiz repository.
‫The repository is used to interact with data source example an API in this application and fetch data.
‫Then I need to create live data of type questions.
‫List questions.
‫Live data.
‫Live data is an observable data holder class that allows you to observe changes in the underlying data.
‫In this case, it's expected to hold the question list object, which suggests that it's intended to
‫store a list of questions.
‫Then I'll create a constructor empty constructor for the quiz view model, and inside it I'll initialize
‫the questions list.
‫Live data repository dot get questions from API.
‫This method used to fetch questions from an API.
‫It's likely returns a live data object containing a question list object.
‫So if we go back you can see from the repository it's returning type is live data of question list.
‫Then I need to create a method called get question list or questions list live data or I can create
‫it.
‫It's a getter method.
‫For this live data you can click Alt Plus Insert Getter and select this or you can write it.
‫This is a getter method that allows.
‫External components.
‫Example the UI components to access the questions list.
‫Live data it returns questions list.
‫Live data as a live data of type.
‫Questions list object.
‫Congratulations, we finished the view model.