I am new to MVVM pattern, and this is a general question for me but I will use a specific example.
I have:
- A data class
Placewhich contains latitude, longitude, name etc. - A
MapsViewModelwhich contains a places listLiveData<List<Place>>used by theMapsFragment - And a
MapsFragmentwhich represents theViewof the MVVM pattern.
From my understanding the View should be oblivious of the Model, in this case MapsFragment should not know about the Place model.
I want to populate the Map with markers but markers need latitude and longitude (and other parameters such as name etc.) so i should observe the list of places and map each entry in the list to the marker for it.
I am not sure if I should place the code for mapping places to markers inside a MapsViewModel so that MapsFragment can just call mapsViewModel.getMarkers() and use that marker list to populate the map, or should the MapsFragment observe the mapsViewModel.getPlaces() and from the response map the list to markers and then populate the map.
If the MapsViewModel is supposed to be responsible for mapping places how will I observe the changes on the LiveData if new locations are added?
If the MapsFragment is supposed to be responsible for mapping then doesn't that break the MVVM pattern where Views should not know about the model?
The example is written in Kotlin but i did not tag the question with Kotlin because it is not language specific.
In my current implementations I am observing the places LiveData<List<Place>>
// MapsFragment
...
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mapsViewModel.getPlaces().observe(this, Observer { places ->
places?.forEach { place ->
var options = MarkerOptions()
options.position(LatLng(place.lat.toDouble(), place.lon.toDouble()))
mMap.addMarker(options)
}
})
}
...
// MapsViewModel
...
fun getPlaces(): LiveData<List<Place>> {
return Repositories.placesRepository.getPlaces()
}
...
P.S. One side question for the ListViews and other collection types: Should every cell have its own ViewModel or should the whole ListView have only one ViewModel?
Aucun commentaire:
Enregistrer un commentaire