Extract Methods into the View Commands Protocol

Here’s the refactoring strategy we’ll use to move responsibilities into MVP:

  1. Use the Extract Function refactoring to take commands issued to views, and move them into the View Commands protocol.

  2. Use the Move Function refactoring to move functions that call these View Commands. We’ll move them from the view controller to the presenter.

Let’s find something that issues commands to UIKit views. Since we used MVVM before, most of these will be in the didSet observer of viewModel. Looking at the bottom, we see it calls updateActivityIndicator. Let’s start there.

 private​ ​func​ ​updateActivityIndicator​() {
 if​ viewModel.isActivityIndicatorShowing ...

Get iOS Unit Testing by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.