What is the lifecycle of the MVP components? - android

Which component does the application start from? Where should they be created? (If the view is activity, and if you need to respect the independence of the components). Under what conditions can exist multiple Model or Presenter components? And who should manage the change and the creation of them?
i have no answer for my all questions about mvp lifecycle, so if somebody can continue Rohit answer - please

Model is responsible for giving the data whatever your requirements are.
Presenter act as a mediator b/w View and Model and contains the business logic(the
manipulation of data according to the users requirements)
View is the part which is visible to the users all your S.D.K. related code will be here
and the part which is visible to the user will be here.
Notes.
1.The View will communicate with the presenter to get Data from model
2.Model will get the data and give to presenter
3. Presenter will do if any modifications in the data is required and give it to View
4.The View and Model will not contact each other directly
5.All the logic will be in presenter so that it can be tested separately
For Android
The View is Activity where android related data will be present which are a part of Android S.D.K. and it will ask presenter for data which will contact the Model which will give you data from server or locally or from anywhere which will get back back to presenter then to view
The Android related data will be in View only and all the things which requires activity or android context should take place in view only
The Presenter will contain the business logic so that we can write JUNIT tests on it

Related

who is in charge of sort data in MVP Structure, View , presenter or Model?

I want to sort My ArrayList which I got it from the model If I want to put a dropdown to sort my data in Activity to sort in Date or alphabet ...
I must send the request to the presenter to sort my data or Can I sort it in view model?
Should the Presenter sort the list of items or is that a task for the view?
The correct answer is (drum roll please) it depends. Are you using a
UI framework that supports sorting natively? Are you sorting over a
large sequence that would be best performed as a database operation?
Does your UI framework support sorting through the database (e.g.
IQueryable support in many .NET UI toolkits).
There are multiple approaches to MVP. You have the Passive View in
which the Presenter has deep knowledge of the view via an Interface,
there's the supervising controller in which the presenter handles
complex view logic and the rest is left to the view (so again if your
view objects can handle sorting, let them do it if it's not an
expensive operation), finally is the Presentation Model which is
pretty much equivalent to MVVM in which the Presenter exposes behavior
as properties and the View reacts to those properties.
taken from here credit to Michael Brown

What role of adapter, service, content provider and other basic elements in MVP architecture of android app?

I figure out that it's very useful to use MVP pattern in my apps, but it's too complicated for me, how find out of role elements in apps, which is model, view, presenter?
What is adapter of recyclerview? I guess it should be a view, but it manage loading data to recyclerview, so it can be also presenter or model
How many presenters can have view? What is basic? For one view should be one present and one model? But if I have some view and can they use one presenter?
I have an app with recyclerview which is loading images from rest api. I wanna figure out elements for MVP. Activity with recycler view is View? Adapter is Presenter? Picasso is Model? Retrofit is Model? Notification Service for download in external storage is Model? Image class is Model?
Can you please give a link for big complex apps with MVP architecture, please?
There's no agreed definition on exactly the best way of MVP-ing in Android.
My answers to your questions.
I'd say the Adapter is a View, that is passed data from a Presenter.
I usually have 1 Presenter 1 View.
View: RecyclerView, Actvity, Fragment + Adpater all are views for me. Presenter: Is a normal java class that retrieves the data from Model (Retrofit, Sqlite, ContentProvider) and gives it the View to populate the RecyclerView.
See this article on MVP
Note:
Try to ensure the Presenter does not have any Android logic or Android stuff.
Model doesn't just mean the POJO's it can mean include storing in DB, ContentProvider etc. including network retrieval.
The View should not interact with the model directly i.e. data retrieval, storage etc. Only via the Presenter.

Android MVP - Which model to present to view

I am using the MVP design pattern.. The models which I am presenting to my view are currently POJOS that are models for my local Realm database. That being said, they have annotated members relevant to the database. Would it be better to use the presenter to map the DB models returned from the interactor to models that only contain data which the view should utilize (Have a seperate model POJO for my view)? Or is it fine practice to hand the view my db models?
Thanks.
The question you have to ask is
If I removed Realm from my app would I have to make changes to my
view if I use these entity classes
If the answer is no then I see no problem with using those entities to pass to your view. If however they are tied to Realm in such a way that you would have to change the view then this would break the decoupled idea of MVP.
I personally quite often make a ViewModel anyway. The reason being is I want to minimise the number of separate calls between my Presenter and my View. Ideally your view should be as close as possible to having the methods:
setLoadingUi();
setContentUi(ViewModel model);
setEmptyUi();
setErrorUi();
Whilst this isn't always possible if you have various view calls such as setConfirmButtonText that ties your Presenter into knowing too much about your View. What happens when you change the button for a swipe...your Presenter would need to change as well as your View.
Hope this helps!

Why need to use ViewModels in MVC?

Two questions about MVC:
1) What is difference between .net MVC and Android MVC?
2) Why need to use ViewModels in MVC?
I would appreciate if anyone can answer these questions.
reference from this
Model-View-Controller
In the MVC, the Controller is responsible for determining which View is displayed in response to any action including when the application loads. This differs from MVP where actions route through the View to the Presenter. In MVC, every action in the View correlates with a call to a Controller along with an action. In the web each action involves a call to a URL on the other side of which there is a Controller who responds. Once that Controller has completed its processing, it will return the correct View. The sequence continues in that manner throughout the life of the application:
Action in the View
-> Call to Controller
-> Controller Logic
-> Controller returns the View.
One other big difference about MVC is that the View does not directly bind to the Model. The view simply renders, and is completely stateless. In implementations of MVC the View usually will not have any logic in the code behind. This is contrary to MVP where it is absolutely necessary as if the View does not delegate to the Presenter, it will never get called.
MVC is a concept rather than a solid programming framework. You can implement your own MVC in any platforms.
Model: What to render
View: How to render
Controller: Events, user input
Question 1: Android MVC and .Net MVC
Android MVC:
In Android you don't have MVC, but you have the following:
You define your user interface in various XML files by resolution, hardware, etc.
You define your resources in various XML files by locale, etc.
You extend clases like ListActivity, TabActivity and make use of the XML file by inflaters.
You can create as many classes as you wish for your business logic.
A lot of Utils have been already written for you - DatabaseUtils, Html.
More Ref : MVC pattern on Android
.net MVC :
ASP.Net MVC is an open source web application framework that implements the model–view–controller (MVC) pattern.
More Ref : http://en.wikipedia.org/wiki/ASP.NET_MVC_Framework
Question 2: Why need to use ViewModels in MVC?
ViewModel help us to organize and manage data in a strongly-typed view with more flexible way than complex objects like models or ViewBag/ViewData objects.It allow you to shape multiple entities from one or more data models or sources into a single object, optimized for consumption and rendering by the view.
ViewModel contain fields that are represented in the view (for LabelFor,EditorFor,DisplayFor helpers)
ViewModel can have specific validation rules using data annotations or IDataErrorInfo.
ViewModel can have multiple entities or objects from different data models or data source.
More Ref: http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications

MVC on android: Model change and animation

After some thinking/reading (especially following series) I came to accept the following general rules for implementing MVC:
Model - set of simple dummy Value Objects, responsible for keeping the state. Model classes are Observable and notify Observers - i.e. views when changed. Model code could be android agnostic.
View - classes able to keep/update its visual state - based on android.view.View/ViewGroup family - by inheritance or composition. View classes are Model-aware, they receive update of Model's state and re-draw accordingly. All user-input listening code is delegated to Controller.
Controller - all user-input processing code in your activity/fragments. Controller updates the Model, which in turn, will trigger subscribed View update.
Sounds simple and certainly doable in most cases.
Now, consider for example:
Controller/Activity keeps track of 2 Model objects model1 and model2 and changes them based on some application logic.
View receives updates from objects model1 and model2 independently as per design above.
Now, suppose the View wants to show animation based on mutual transition of both model1 and model2 for which it needs to know changes of both objects at one time, or as a single event.
What would be recommended way of doing it, keeping mvc design above in place?
I thought about options:
Have a "SmartModel" which encapsulates both model1 and model2, make the mutual changes/transition and inform the View. I don't like this approach because Model becomes not really just "dumb" state representation, but also drags some application logic from Controller.
Have Controller inform the View about the changes it did on both model1 and model2. In that case neither model1 nor model2 need to implement ability to notify the View, i.e. there is no link between Model->View anymore.
May be there is much better option / I am missing something.
Any insightful suggestions will be highly appreciated.
I think the option 2) is similiar with MVP pattern, controller is kind of presenter to change the view for model.
Model View Controller, Model View Presenter, and Model View ViewModel Design Patterns
If you want to follow the strict MVC way, maybe you need to fire some change event to notify the View observers, so they can know what and how exactly the model changes, so they can do partial refresh and animation.

Categories

Resources