Should Every View Have A ViewModel?
I've been developing in Android for a few months back and I am currently learning and implementing MVVM arquitecture, but the info related in Internet confuses me a little bit.
Google recommend to have only one viewModel per view, other resources says that is not necessarily for all views....
In my current project I have a view where i don't know if is needed or not, main activity has a RecyclerView with clickable items (this activity has his viewModel) and when user clicks on one item it takes him to a details page populated using Bundle (I think until this point a viewmodel is not needed, or it is?), but as an update I want to implement a BarcodeScanner button in main activity and as result it will make a GET to an API and populate the same view (details with de result of the call).
I this case, does my details view needs a viewModel?
Any info is appreciated.
It is a design choice. It is not necessary for any view to have a view model to function. However, Google recommends the pattern because it leads to good design.
If you want to say that your app is MVVM, then you need to keep the view separated from the data that drives it. This typically requires a translation layer to query and transform the model data in to data that can be consumed by the view.
I didn't read this article thoroughly, but it has a diagram that illustrates the relationship between the model, view model, and view:
In your barcode scanner button example, the API server can be thought of as the model. Your view model should interface with the API (make the GET calls) in such a way that it can post updates to the view (typically using LiveData). The view stays up to date by observing the API results.
I would recommend isolating the interface to the API in it's own class or service. Then the view calls the view model when the barcode scanner button is clicked and the view model call the API class.
Regarding your bundle populated details page question: It's hard to know without looking at your code, but presumably your main activity is populated from data in a database (whether local, or through an API). You recycler view contains a list of items, and upon selecting an item, it's details are displayed in a new view. If I were implementing that, I would pass only the ID of the item to the detail view and initialize the view model with that same ID. Then the view model would lookup all data associated with the item and display it.
Presumably more data is displayed in the detail view than in the RecyclerView. The main activity does not need to access the item details or hold them in a ViewAdapter (and should not).
However, these are all design choices. There is nothing that says your app has to be written a certain way or follow a certain pattern. It can function regardless. My advice is to follow the pattern even when you think you don't need to until you learn why you need to!
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View