I just start with architecture components, so I would like to know differences with clean architecture and What is the best way to implement with MVP.
How to integrate ViewModel with presenter. I appreciate any help with this
Architecture Components are independent libraries made to help make better Android apps. You can fit them in any Architecture based upon on your need.
That said, Google recommends using MVVM architecture in their Architecture Guidelines. Also you can use Presenterwith MVVP architecture.
Take a look at this News Sample app made by following Architecture Guidelines or check out the Clean Architecture with Architecture Components Template by Buffer
Related
I want to implement MVP pattern for my Android application along with Dagger and RealmDB. I am a complete noob in these concepts. I have referred https://android.jlelse.eu/mvp-dagger-2-rx-clean-modern-android-app-code-74f63c9a6f2f . The blog post though good lacks a bit of explanation about the code demonstrated.
Can anyone recommend good blog posts with explanations which I can follow to implement MVP, Dagger and RealmDb in my app? Any help is highly appreciated.
first I say check ORM's benchmark and choice your decide about your ORM. Don't worry about learning new ORM because it's too easy. here is a benchmark of ORM's ORM's benchmark
Architecture One
first choice is using MVVM. This is a good architecture that you can find many article and blogs in internet. The best benefit if MVVM is you can use is beside data binding that is a great library that support by google and is very useful. Databinding make your project more scale-able and easy to maintenance.
Architecture two
Second Article is going beyond MVVM and databinding. It's using Android Architecture Components . It's couples of component's that introduce and support by google. this components include Databinding,LiveData,Lifecycle,ViewModel,RoomDB,... that google has a good documentation for them and how to use them together.
benefits of this architecture can find in this sentence from google:
Android architecture components are a collection of libraries that
help you design robust, testable, and maintainable apps
Another "which is better" question, I know these are highly subjective. My definition for better would be:
Being used by the best dev teams
Best for unit testing
Most modular / easiest to build out a prod app
What Google recommends (this is where I'm most confused)
All of the Android blueprints use package-by-feature:
https://github.com/googlesamples/android-architecture
/tasks
/addedittask
/taskdetail
and all of the architecture components samples using package by layer:
https://github.com/googlesamples/android-architecture-components
/db
/model
/ui
/persistence
I'm confused because at the bottom of the Architecture Components site, it actually has links to the Android MVP and MVVM blueprint samples, which seems to me to be contradictory.
Based on Android Architecture guide, and on Clean Architecture, I would suggest this approach
data/
model/
remote/
local/
Repository
domain/
usecases/
GetUserListUseCase
presentation/
screen1/
screen1Activity
screen1Fragment
screen1ViewModel
screen2/
screen2Activity
screen2Fragment
screen2ViewModel
core
common/
di/
More here: https://www.toptal.com/android/android-apps-mvvm-with-clean-architecture
I think it depends on project size. Also in different companies team use different approaches.
I prefer to use in a small project second type of package managing.
If you want to reuse ModelView component, just keep them in feature packages. And maybe with this type:
/db
feature1/
feature2/
/model
feature1/
feature2/
/ui
/persistence
feature1/
feature2/
In Android Architecture Components docs on the Handling Lifecycle page in Best practices paragraph, written:
Use Data Binding to maintain a clean interface between your views and the UI controller.
Can someone provide me with link to example on how to use this libraries together?
You can take a look into this Google Sample, which applies Architecture Components and the Data Binding Library together.
I'm working on an app for Android and I'm confused when do I need to use architectural design pattern.
My project is medium size and I'm working on it alone, but I want to learn how to work using a design pattern, because most of the companies use it when building a project.
How does one select an architectural design pattern when there are so many of them?
You have to read and decide. Check these from Google:
https://developer.android.com/topic/libraries/architecture/guide.html
https://github.com/googlesamples/android-architecture
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!