Learn Android MVVM architecture components with kotlin - android

After searching about android architecture components I realized that MVVM is better than other architectures at some points so I started to learn MVVM and I'm using Kotlin language but the problem is when I search for tutorials to start learning I found many components like RxJava, RxAndroid, Dagger2, Room, Lifecycle and etc. but they don't say why they are using them and why they choose some specific components. i will show you some examples :
1- Android MVVM with Dagger2, Retrofit, RxJava
2- Android MVVM with Dagger2, Retrofit, RxAndroid
3- Android app with MVVM dagger2 RxJava Livedata and Room
My questions are :
1- How should I decide to choose some of these components and work with them?
2- How should I know that I need to use some of them together(for example Livedata and RxJava)?
This is really confusing for me and I don't know how to start.

First you need to individually understand why to use Kotlin, MVVM, Dagger, RxJava, Retrofit.
MVVM and Kotlin:
https://medium.com/#zhangqichuan/android-development-with-mvvm-and-kotlin-9598c3623ce1
RxJava and RxAndroid:
RxAndroid is an extension to RxJava(Used for handling threads and asynchronous tasks), for more details check following links:
Difference between RxJava and RxAndroid?
https://www.quora.com/What-is-the-difference-between-Retrofit-and-RxJava-RxAndroid
Retrofit:
https://medium.com/#elye.project/kotlin-and-retrofit-2-tutorial-with-working-codes-333a4422a890
LiveData and room with kotlin:
Room is a database layer on top of an SQLite database which used to remove boilter plate while fetching data from local sqlite database, use following links:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#0
After understanding this components(Kotlin, MVVM, Dagger, RxJava and RxAndroid, Retrofit, LiveData) individually then you need to do all this things in single project, use following link:
https://github.com/egek92/Kotlin-MVVM-LiveData-Room

As a jr. android developer, i was thinking like you but i choose a route like :
Learn MVVM with ViewModel(AAC),LiveData,Observable pattern.
Make your async and multi threading processes with Coroutines.
Make your Dependency Injection with Koin.
(If you want you can see how to mix them. Koin will be added soon. https://github.com/furkanaskin/CoinStalker)
Future?
Dagger 2, maybe Rxjava etc..

If you know the Core Android basics and has done at least 1 or 2 projects then you can jump to this, https://android.jlelse.eu/why-to-choose-mvvm-over-mvp-android-architecture-33c0f2de5516

Maybe what you need is start from basics.
There are a couple of google official code labs about Android architecture.
You can start looking at this code lab and then the others.
https://codelabs.developers.google.com/codelabs/kotlin-android-training-view-model

Simple Note App is written in Kotlin using MVVM architecture
https://github.com/prakashshuklahub/Simple-Notes-Kotlin-App
Architecture Components used in the app are as follows
LiveData
Room:
ViewModel:
Repository:

You may check the GitHub Project using
Kotlin
MVVM
Jetpack
Retrofit2
Here is full source GitHUb Link: https://github.com/enamul95/kotlin_MVVM_Jetpack

Related

Implementing MVVM, Dagger2, Retrofit, Rxjava, LiveData, ViewModel together along with Android testing

I have knowledge of MVVM, Dagger2, Retrofit, Rxjava, LiveData, ViewModel. I did a basic implementation of them together without Fragments https://github.com/karthik5418/android--demo/tree/snapdeal_search
Now I have some doubts in going advanced implementation of them
Do I need to learn DataBinding (i.e is it necessary to get clean code and for testing)
I gone through few blogs for advanced implementation, every blogs is has its own way. Some looks higher level of abstraction, that I thing is not necessary
reference : https://github.com/MindorksOpenSource/android-mvvm-architecture
https://proandroiddev.com/mvvm-architecture-using-livedata-rxjava-and-new-dagger-android-injection-639837b1eb6c
https://medium.com/#iammert/new-android-injector-with-dagger-2-part-1-8baa60152abe
I am confused now, what should I follow for my production ?
Please help me with some reference that I can follow for my production.
check out this repo https://github.com/balwinderSingh1989/MVVMCleanAndroid that contains a architecture structure that you use to kick start your project.
And yes, you should be learning databinding (there's hardly any learning curve involved). Also make sure you explore about binding adapters as well.

Android architecture components with MVP

New Android Architecture Components released on google IO 17.
So should we use MVP with architecture components and MVVM?
In google sample on Github, they have used the MVVM model.
Google Sample
About the selection of architectural pattern and its implementation, it always depends on many factors such as team members, app's types, and so on.
In Google's blog post, they have already said that Architecture Components are mainly
Opinions not Prescriptions.
However, three main components (Room, ViewModel, ViewData) are developed with wide-range usage but not focus on any specific pattern.
For example,
If you are using SQLite to persist local data in your app, you can
you Room no matter what kind of pattern you are using, it is really a
great library with a lot of benefits such as reducing boiler plate
codes, validating SQLite syntax in compilation time, etc.
Besides, LifeCycle, LiveData, and ViewModel have their own strong
points. Especially, they address lifecycle-handling issues really well.
LiveData gives you one more tool of observer pattern which handles data stream reactively. (Like a great feature RxJava provides us).
...
At the end, for your question.
So should we use MVP with architecture components?
The answer is "it's nice part of Android framework, so why not".
Updated:
If you want to see how a MVP pattern project works with new Architecture Components, please checkout my repository on Github. In which I enhance previous MVP model by using Room for local persistence and LifeCycle to create lifecycle-aware Presenter.
I'm currently building a template project that uses MVP pattern along with Google Architecture components (Room, LiveData), and compare to mix version of Realm and LiveData:
https://github.com/duyp/mvp-template
I think some of Google Architecture Components are compatible with MVP Pattern, not only for MVVM :D
You can check out 2 following branches:
realm_livedata: MVP with Realm and LiveData
room_livedata: MVP with Room persistence library and LiveData
It's much more complex than Google sample project (I used Dagger 2, RxAndroid, Retrofit, Gson, and clean architecture) hope you find a better solution for your work. If any question or suggestion, you can leave comments or issues on my github project.
I also created a project using MVVM with new Google architecture components with Realm, Dagger 2, Live data: https://github.com/duyp/mvvm-template
Happy coding!

MVP or MVVM for Android with respect to Unit Testing?

Have used MVP model in one module of my android app with the help of which code was testable and I was able to run Unit test via that code.
Now I researched for MVVM model, my main focus is to make my code testable, can MVVM model could help me better than MVP model??
The unit testing for the MVP or MVVM are easy to implement with the help of Mockito.
But the only difference is MVVM architecture is bit more complex then MVP.If you are comfortable with subscribers,dependencies injections etc then go for MVVM otherwise till now MVP is the best.
You can check this link or this for more details.

How ViewModel in MVVM (data binding) is differ from ViewModel in Android Architectural components

From the link:
https://developer.android.com/topic/libraries/architecture/viewmodel.html
I understood that ViewModel(Android Architectural components) is aware of Life cycle of Component.
Can we use these two ViewModels together ?
Firstly, you need to realize MVVM is the name of an architecture. It's used to extract the logic from the View and put it into the ViewModel. One of the great benefits of doing this is making this logic more easily testable.
The ViewModel class from the Architecture Components has the same name but is in itself not the same. It's just a handy tool to keep data from being deleted when the state of your View changes (e.g.: when the screen is rotated).
As for your last question, the two can of course be used together, but the two are definetely not the same.
For more information on implementing MVVM there are some great answers here on StackOverflow already (e.g.: Android MVVM Design Pattern Examples).
Yes, you can use more than one ViewModel in android application.
I have created 2 boilerplate projects for newbies references to MVVM architecture
MVVM with Retrofit- LiveData - ViewModel pattern
MVVM with Room database - LiveData-ViewModel pattern

MVVM on Android with DataBinding or RxJava?

I am building an Android app and am fairly new to both MVVM and data binding concepts on Android.
I have read at a few places ( like here) that both RxJava and Google's DataBinding library can be used for implementing MVVM pattern.
I went through the official Android documentation for DataBinding and am going through a project referenced by Google on Mvvm using DataBinding. However, I have no idea how RxJava works as of yet, which might be obvious from the question.
My question is, keeping in mind the long term goals of extending my app, is it worth investing effort in learning RxJava? Or does DataBinding suffice as a complete substitute for it? I don't know RxJava at all but by reading about it online gives me the impression that it can solve a wide range of problems.
I do not mind investing time in learning RxJava but is it worth the effort than simply using Google's DataBinding (which I have some sort of grasp on)?
RxJava is a completely different concept than DataBinding. It's more of a way of handling concurrency than it is about binding data. I 100% think it's worth learning. The Android community has embraced it with open arms.
Shameless plug: I compiled a list of RxJava resources awhile back - http://gregloesch.com/dev/2014/10/20/resources-for-learning-rxjava-android.html

Categories

Resources