Viper & Protocol
The course is part of this learning path
In this course, we're going to take a look at the VIPER architectural pattern and also protocol-oriented programming and we're going to start our project.
Hey, within this lecture, we're going to try and see if the codes that we have written so far, is working right now, because we have written so much code and we have created most of the functions. Obviously, we need to change the UITableViewCell from here, and we need to take care of the details, but I just want to know if all this code works or not. So, what we need to do, we need to make sure that everything is working and in order to do that, I'm just going to go ahead and change the background of the ViewController and then make sure that everything is connected with each other. What I mean that I'm just going to change the background color to something like yellow, .yellow. And if I run this in a simulator, if I see the yellow color, then it's okay. But I cannot run it right now, why? Because we haven't taken care of the router yet. Since we haven't taken care of the router yet, we don't have the view interactor and presenter connected with each other because we have written all them in the protocols and we have all written them in the classes as well, but we haven't assigned a value to them, right? They do not have any kind of values assigned to them, yet. Here you go, you cannot see them. So, how do we do that? And why didn't we do it yet? Because we have to do it inside of the router. That is the main job of the router and before we have come to this far, I said that you're going to understand it in a much better way once we have completed the view of the interactor and the presenter and come back here, and that's exactly what's going to happen, right now. I'm just going to create a view over here, this will be a CryptoViewController, so I'm going to create the instances of the view presenter and the interactor as well, and I'm just going to connect them with each other, so this will be an interactor each other. So, this will be an AnyInteractor and this will be a CryptoInteractor, here you go. Now, what we need to do, we need to assign the views, for example, view.presenter's value. Now, view.presenter will be the presenter, right? Now, if I come down here, I can just say presenter.view is going to be a view. presenter.router will be the router. presenter.interactor will be the interactor and finally, interactor.presenter will be the presenter. Here you go. Now, everything is connected with each other. What we have said, we said that router will orchestrate the whole thing, and that is exactly what we are doing over here, okay? And also, we're going to have to take care of this entry point, as well. We have taken care of the orchestration but we have to create an entry point. In order to do that, I'm just going to call this entry and yep, let's see. We can say this is going to be a UIViewController, and also, this will be an AnyView, right? So, we can write it like this, or maybe with an & in between, like this. So, this can work but actually, it's a much better idea to do this with typealiases. So, let me show you what I mean. This is going to be a get and if we write this, obviously, we need to confirm this and write it inside of the class, as well. But since I haven't made them optional this will give me an error. If I want to make them optional, then I have to come to the protocol and change them, one by one. Because if I leave this option, this will give me an error. But if I make those optional, then we're going to get an error inside of the protocol, as well. So, this is getting a little bit interesting as you can see, we cannot do that. So, rather than that I'm just going to create a typealias. So, this will be an EntryPoint and our EntryPoint will be in AnyView but also a UIViewController. If I do that, x code will understand each time it sees the entry point, it will think of AnyView and UIViewController. It means that. So, rather than doing this, I can just come over here and say, this is an EntryPoint. And I can make this optional, and there won't be any kind of repercussions. And obviously, I can just change it to be an EntryPoint optional here, again. So, why did I do that? Because I can come over here and say, router.entry is equal to the view itself. So, I can make the EntryPoint, the entry UIViewController, my own will. And if it gives you an error, you can just cast it as an EntryPoint, because I know that our CryptoViewController is also an AnyView and also a UIViewController, as well. Now, since I did that, it's great but I have to go into the SceneDelegate and make the necessary changes in the SceneDelegate, as well. Because right now, we have deleted the main storyboard and this doesn't even know where to begin with. If I can connect this with the router, then everything will be okay. Now, as you can see it creates a window scene over here, but we don't use it generally, but if we want to use it, let's see how it's done. I'm going to call this routerInstance and I'm just going to, instantiate the router by saying that CryptoRouter.startExecution and that's why we have created this function aesthetic function, and I can just call this initialViewController, and this will be the routerInstance.entry. Here you go. Now, we have reached the ViewController. Now, all I got to do, make sure that this ViewController is going to be the first one in the window. Now, I'm going to create a window over here from the scene, and the scene is created here for us, but I'm going to call this windowScene because it doesn't have any kind of variable name. Now, this will drive from the window scene. And then later on, I can just assign this inside of the window by saying that rootViewController, which means that the first ViewController to be displayed. Here we go, that's all about it. window.rootViewController is the initialViewController we got from the router. So, after that, obviously, we need to connect our window with the current window over here. So, I'm just going to say self.window = window and window.makeKeyAndVisible. Here you go, that will show them. Okay, so we have created the window. We have made the rootViewController, our rootViewController, our initialViewController that we got from the routerInstance, and I believe we have connected everything inside of the router, as well. Now, this is getting the thing from the view and the view is the CryptoViewController that we have created before. Now, let's run this inside of a simulator. And if we see the yellow background, then everything will be okay, right? If we see the yellow background, everything will be okay. Now, if we see it then we're just going to update the functions, and tada. Now, we're going to have to wait and see. So, let me open my simulator, I believe it's not up and running, yet. Let's wait a little bit and see if this is going to work and here you go, it's working. We have seen the yellow background and also, we have seen the downloading sign, downloading message label, as well. Great, of course it's not downloading yet, because we haven't triggered the download function in any way, okay? And also, we have to take care of these update functions, as well. Now, this update functions here, so I have put a breakpoint by mistake, let me delete it, but it's not triggered. Even if we trigger it, we don't even have a UITablViewCell, yet. But we need to trigger this downloadCryptos at one point. Let's take care of everything in the next lecture and make sure everything works together, and finish this application together.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.