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

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.

Related

Learn Android MVVM architecture components with kotlin

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

Should I use ViewModel with UseCase?

This is more of a question about designing applications, rather than fixing a specific issue.
So most Android tutorials I see use ViewModel as a layer between the data source and the views. Hence my first impression was that a ViewModel is supposed to handle data fetching and updating, but then I read about 'Use cases' which most Android samples don't even mention and I don't understand how all these parts fit together. What's the relationship between a ViewModel and a use case?
1) Clean Architecture this is approach how to design your application. This is not about specific realization like in case of ViewModel.
2) If you looked at the official Android documentation you will not find any mentions of Clean Architecture. Google not forcing this approach.
3) ViewModel this is part of MVVM design pattern. So if we looking info Clean Architecture MMVM can be part of Presentation layer (same as MVP commonly used in this layer). But you still need UseCase to make interactions between Data layer and Presentation layer.

Android Architecture Questions

I've been reading about architecture to android projects. And I found some stuff, but I guess I misunderstood some concepts or not even understood at all.
One of my questions is about handling api objects, if I have a local database, Should I use the same object from api to store in local database?
I'm also looking for explanation about why use MVVM or MVP, actually they looks like different stuff, I have figured out that MVP is a pattern more concerned about handling UI responsibilities, MVVM I think is oriented to handle communication between UI and database. So I misunderstood the concepts or it make sense?
The last but not least important topic is about dependency injection, I have read about the concept and this question came to my mind, why should I use any framework as dagger to handle this, if I can handle this pattern by myself, once it's not thaaat complicated?
Should I use the same object from api to store in local database?
It can really depend on how good your API object is. You should rather base your local database object on what it really mean in a logical way and if your endpoint is well done it could be the same. The important part in your architecture is to isolate your logic parts from your I/O parts (UI, database, API) so if you want to redesign your UI, change the Webservice you use it will not be too painful.
So I misunderstood the concepts or it make sense?
I am not that familiar with MVVM so I can not really answer that question. But for me the important is not to follow "by the book" one pattern or another but rather to adapt your architecture from what you like from each. I currently try do to so with the Clean Architecture. You can take a look at all the concepts Uncle Bob speak about in this article about making code cleaner and more maintainable.
why should I use any framework as dagger to handle this, if I can handle this pattern by myself, once it's not thaaat complicated?
You don't have to use dagger if you're not familiar with it. But if your project start to grow and you start to be a team of 2, 3, 5... working on it, a framework as dagger can help you keeping a common standard about how you make your dependency injection and then making the code more coherent. Dagger also provide some tools as scopes that can save you some time if you're familiar with it.

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!

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