2

I have a view controller with a mapview (Apple Map), on top of that there is a view with a button and a textfield. The view is compacted around the search button, when I touch it it expands revealing the textfield.

I want to use the MVVM pattern so here are some questions:

  1. Who should be responsible of the animation? The animations happens manipulating constraints that the ViewController is holding, should the ViewModel be responsible for that? Also, the the search bar has a boolean, indicating whether it's open or closed, who should hold it? The ViewModel or the ViewController?

  2. What should be the role of a ViewModel of the searchfield, take events from it, pass them to the model and then notify the ViewController?

  3. This app will receive information from services, who should make the call to the services? The ViewController, the ViewModel or the Model?

1

To start with, MVVM does not have the concept of the controller. So, I assume that when you mention the ViewController you mean the View, eg. the form and all the visual elements.

Back to your questions:

Q1:

Who should be responsible of the animation?

The ViewModel is responsible to trigger the animation. In fact, you should see this in a broader way. Think of the case where, in some point in future, you decide that the animation is not the right effect. What sort of visuals you will use is part of the View. The ViewModel should signal the view to trigger some sort of visual cues. Then the view makes it happen.

The animations happens manipulating constraints that the ViewController is holding, should the ViewModel be responsible for that?

What kind of constraints? Do you mean things that affect the UI? Such as how long the textfield will be? If so, then the View deals with these.

Also, the search bar has a boolean, indicating whether it's open or closed, who should hold it? The ViewModel or the ViewController?

This is a property in the ViewModel. The View links back to an event in ViewModel and when the user touches the search button, the event in the ViewModel changes sends a signal to the View to change the status of the textfield (collapse/expand).

Q2:

What should be the role of a ViewModel of the searchfield, take events from it, pass them to the model and then notify the ViewController?

Yes, all these and one more important thing: prepare the results from the search (most likely retrieved by the model) in a form that the View is able to present

Q3:

This app will receive information from services, who should make the call to the services? The ViewController, the ViewModel or the Model?

It can be either the Model or the ViewModel depending on what kind of services you are looking at and how you design the Model/ViewModel. You talk about "making the call" and not "receiving" so I think it is more reasonable to allow the Model initiate the sequence of updating/informing the ViewModel and then the ViewModel the View

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.