The first and easiest step is to move all the logic out of the View Controller and move it into the Presenter. In order to have any value the code should be organized into it's different responsibilities (Network, Database, Data Translation, etc.) as we will move this to those respective layers later. This first step will help us concentrate on isolating our View Layer. We will focus on our Spy List first.
- [Narrator] The first step on our roadmap to applying this architectural stuff is first creating a presenter class that we can put everything from the view controller in that doesn't belong in the view controller. So let's come in to here. We'll just make a new file. We'll call this a "SpyList Presenter." And I'll just make a class of that same name. And let's go ahead and move these type aliases over.
These are stuff that don't belong on the view controller. Also, these imports. Alamofire is a network library, CoreData is our database, and Outlaw is a library for working with JSON, Again, stuff that doesn't belong in the view controller. And then, let's move this data, so all the data that the view controller should be working with should be off of the presenter. And then coming back, let's go through this section.
Beforehand, I've organized all these methods into pieces that would be broken in to different layers, and that's a good thing to do before you decide to start moving your code to separate files. In this case, I've broken them into VLogic and anything that belongs to other layers in to their different extensions. So table view methods are going to be VLogic specific, but the data methods, the model methods, networking, translation, and any type of methods like that all belong in the presenter or below.
So let's go ahead and cut from Line 64, going down to the bottom of this. You can see the data method's private data methods translation. Those same type of things that we talked about. I'll go ahead and cut these and paste this into the presenter. And I'll actually paste it separately. We have to go through and rename this SpyList view controller to this presenter. And we'll eventually be breaking these into their own methods after the fact.
Just verify that we've pasted all of these. And then, let's come back to our view controller and create one of these and start consuming it. So in here, I'll say we have a file private. Our presenter is equal to "SpyList Presenter". Make it explicitly unwrapped. And for right now, we'll just new one of these up within this viewDidLoad method. Now we just need a sign the presenter is equal to "SpyList Presenter".
Again, we're going to be injecting this later on. But for right now, we'll just new one of these up in here. And let's start consuming it. This load data method doesn't really belong in here anymore. It's actually on the presenter. And then this data object doesn't really belong in here anymore. So we'll find all the instances of data and just replace that with the presenter. And we're ready to now move on to the next view controller and do the same thing there.
- Moving pieces of architecture
- Designing an MVA app
- Walking through a demo application
- Converting to MVP
- Moving non-view logic to a presenter
- Breaking logic into layers
- Using entities
- Setting up dependency injection
- Implementing the coordination pattern
- Adding data binding with RxSwift