Рет қаралды 16,937
What is MVVM and how do I use it in Android?
By the way, check out our Android Developer Learning path: bit.ly/3l6vG86
In this video, we talk about what Mvvm is and why you might want to use it. We also show some of the benefits of using Mvvm for your app architecture.
This is a second part to networking with retrofit where we are going to be introducing the Model View ViewModel Architecture which is an architecture recommended by google for developing android applications.
For those who might not understand generally what *Architecture patterns* are ****in android, they are patterns introduced to help developers properly structure project codes and separate core logic from the Activities or Fragment especially in large or real mobile applications that might require feature changes while it grows as it is very helpful in the maintenance of the project while adding and removing features. The most popularly used architectures on android are
MVC - Model View Controller
MVP - Model View Presenter
MVVM -Model View ViewModel
We will be discussing MVVM
MVVM encourages separation of concern and in this case making sure that the business logic is entirely separated from the view. You can take the business logic to be the logic that controls the main features of the app and view it to be the user interface elements that allow the user to interact with the app.
Model - The model comprises of the Repository and the ApiService
View - The view is the MainActivity
ViewModel - The ViewModel is the MainViewModel
Creating the Repository class
The repository class is usually the single source of truth (SSOT)in a project, for instance, if you are going to be depending on two data sources say an API and a local database then this class serves as a mediator between the two sources and knows where to get the data from so that the viewmodel is solely responsible for making the data available for the view to observe.
The ViewModel Class
Before you use the ViewModel class you have to add these dependencies to your project. Open build.gradle and add the following dependencies.
tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Stay tuned and subscribe to tutorialsEU: goo.gl/rBFh3x