Android architecture components with MVP - android

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!

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.

Testing Firebase Database with an in-memory fake/db

Is there any in-memory implementation for Firebase database for testing Android client logic and integration? I have seen node.js fakes but could not find anything in java. I am not looking for a plain mock which is only good for basic unit-testing.
You must do refactoring your application, and implement an architectural approach. For example, such as MVP, MVVM, Clean, etc. The main thing is the layers. In this case, you can test these layers independently of each other using mock objects.
On the Internet you will find a lot of documentation on this topic,
eg Essential Guide For Designing Your Android App Architecture,
Why to choose MVVM over MVP — Android Architecture and many others.

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

Android MVP implementation

i came across a few articles talking about the subject but i cannot understand how to apply MVP on my app code. is there any specific guidelines for a clean MVP implementation for android.
thank you
Checkout this link:
Material movies
MVPAndroidBootstrap
Clean-Contacts
MvpCleanArchitecture
Android-CleanArchitecture
At this links you will find a lot of MVP implementations for android
UPDATED:
mosby - A Model-View-Presenter library for modern Android apps
ThirtyInch - A new MVP library for Android
Moxy - Android MVP library without problems of lifecycle and boilerplate code
UPDATED:
Interesting-Android-repositories - contains a lot MVP implementations for Android.
Here on github
https://github.com/saksham24/Android-Firebase-Mvp-Mvc-Mvvm-chat
i made a repo containing 3 applications with same functionality but written in 3 different android patterns(Mvc, Mvp, Mvvm)
Understanding three different pattern is quite easy if we get a simple good example on them so i made a repo to contribute my knowledge to this developer community.
Also the repository is written using proper java guidelines and conventions(including naming and packages, modules) so people looking for such project can also view this repository.
I've recently created lightweight (with no overhead dependencies) MVP library in Kotlin. It also supports LiveData subscription between Presenter and Model under the hood.
Please find detailed "how-to" and sources here: https://github.com/smaslenko/instant-mvp

Categories

Resources