From the course: iOS App Development: Design Patterns for Mobile Architecture

Create presenter

From the course: iOS App Development: Design Patterns for Mobile Architecture

Start my 1-month free trial

Create presenter

{QTtext}{timescale:100}{font:Verdana}{size:20}{backColor:0,0,0} {textColor:65280,65280,65280}{width:960}{justify:center} {plain} - [Narrator] The first step on our roadmap to applying this - [Narrator] The first step on our roadmap to applying this architectural stuff is first creating a presenter class architectural stuff is first creating a presenter class that we can put everything from the view controller in that we can put everything from the view controller in that doesn't belong in the view controller. that doesn't belong in the view controller. So let's come in to here. We'll just make a new file. So let's come in to here. We'll just make a new file. We'll call this a "SpyList Presenter." We'll call this a "SpyList Presenter." And I'll just make a class of that same name. And I'll just make a class of that same name. And let's go ahead and move these type aliases over. And let's go ahead and move these type aliases over. These are stuff that don't belong on the view controller. These are stuff that don't belong on the view controller. Also, these imports. Alamofire is a network library, Also, these imports. Alamofire is a network library, CoreData is our database, and Outlaw is a library for CoreData is our database, and Outlaw is a library for working with JSON, working with JSON, Again, stuff that doesn't belong in the view controller. Again, stuff that doesn't belong in the view controller. And then, let's move this data, so all the data that the And then, let's move this data, so all the data that the view controller should be working with should be off of view controller should be working with should be off of the presenter. the presenter. And then coming back, let's go through this section. And then coming back, let's go through this section. Beforehand, I've organized all these methods into pieces Beforehand, I've organized all these methods into pieces that would be broken in to different layers, that would be broken in to different layers, and that's a good thing to do before you decide to start and that's a good thing to do before you decide to start moving your code to separate files. moving your code to separate files. In this case, I've broken them into VLogic and anything In this case, I've broken them into VLogic and anything that belongs to other layers in to their that belongs to other layers in to their different extensions. different extensions. So table view methods are going to be VLogic specific, So table view methods are going to be VLogic specific, but the data methods, the model methods, networking, but the data methods, the model methods, networking, translation, and any type of methods like that all belong translation, and any type of methods like that all belong in the presenter or below. in the presenter or below. So let's go ahead and cut from Line 64, So let's go ahead and cut from Line 64, going down to the bottom of this. going down to the bottom of this. You can see the data method's private You can see the data method's private data methods translation. data methods translation. Those same type of things that we talked about. Those same type of things that we talked about. I'll go ahead and cut these and I'll go ahead and cut these and paste this into the presenter. paste this into the presenter. And I'll actually paste it separately. And I'll actually paste it separately. We have to go through and rename this We have to go through and rename this SpyList view controller to this presenter. SpyList view controller to this presenter. And we'll eventually be breaking these into their own And we'll eventually be breaking these into their own methods after the fact. methods after the fact. Just verify that we've pasted all of these. Just verify that we've pasted all of these. And then, let's come back to our view controller and And then, let's come back to our view controller and create one of these and start consuming it. create one of these and start consuming it. So in here, I'll say we have a file private. So in here, I'll say we have a file private. Our presenter is equal to "SpyList Presenter". Our presenter is equal to "SpyList Presenter". Make it explicitly unwrapped. Make it explicitly unwrapped. And for right now, we'll just new one of these up within And for right now, we'll just new one of these up within this viewDidLoad method. this viewDidLoad method. Now we just need a sign the presenter is equal to Now we just need a sign the presenter is equal to "SpyList Presenter". "SpyList Presenter". Again, we're going to be injecting this later on. Again, we're going to be injecting this later on. But for right now, we'll just new one of these up in here. But for right now, we'll just new one of these up in here. And let's start consuming it. And let's start consuming it. This load data method doesn't really belong in here anymore. This load data method doesn't really belong in here anymore. It's actually on the presenter. It's actually on the presenter. And then this data object doesn't And then this data object doesn't really belong in here anymore. really belong in here anymore. So we'll find all the instances of data and just So we'll find all the instances of data and just replace that with the presenter. replace that with the presenter. And we're ready to now move on to the next And we're ready to now move on to the next view controller and do the same thing there. view controller and do the same thing there.

Contents