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!
Related
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
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
I have been reading about MVVM pattern. View is supposed to observe for changes in ViewModel and act on it accordingly but I am confused if following code in View is ok in MVVM architecture.
fun onClick(view:View){
showUser(viewModel.getUserDisplayName())
}
Here View is not observing for a change, its rather asking for latest data from ViewModel. Is this considered correct in MVVM?
This is a question that possibly has no one answer, as different points might be made depending on the exact use case. However:
Sir Codesalot (great handle) is in my opinion technically correct. Let me elaborate.
In MVVM the view should pass UI interaction events (commands) to the ViewModel.
Here is an article from microsoft (who invented mvvm): https://msdn.microsoft.com/en-us/library/ff798384.aspx
The examples here are not related to android, but the concepts should be the same, especially if you look at the first diagram.
The ViewModel should do its magic, react to these events by manipulating data accordingly and then notify the observers (usually the view) of state changes. The view then reacts to the state changes.
In this way, if you would just pass data back from the ViewModel (in a synchronous way), then your view might be missing side effects. In your particular example, there probably aren't any, but consider that the method you call does not only return data, but also changes some internal state (e.g. counts the number of times the data was accessed). Then your view will not know of these.
Of course, you can make the case, that you can return all relevant data for the view, but it starts to break the single responsibility principle.
Here is an interesting blog post, which might give a better example, why the view should always get the state after the ViewModel: https://medium.com/upday-devs/mvvm-rxjava-learnings-1819423f9592
To the point of the other case, the wikipedia article about MVVM https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel states, that the ViewModel can expose public properties.
So in the end, it will most probably be cleaner if you do not return data directly. However, you are the architect and you know your app the best and there might be cases where patterns can be broken. After all they are guidelines. If you know why you break it (and maybe document it), then everything should work out.
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.
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.