This project is already split in some virtual folders that will get us
thinking about the Model view and Controller.
The AppDelegate and the GCDBlackbox have been place outside of this folders and
we can ignore them for now.
In the Model folder, we have this clients folder and then under Clients,
we have TMDB for the movie database.
This folder structure represents the idea that we could have multiple
clients or managers that handle all the code related to an API, but since we're
only working with the movie database, we just have the single TMDB folder, but
you could imagine others like one for Flickr or Facebook or so forth.
Now in the TMDB folder, we have SWF files that pertain to the main model of
our project, the TMDB movie as well as the API's configs, constants and
a TMBD client that will handle all the communications with the API itself.
We'll talk about this more later.
You'll also notice there's this one controller-like file called
TMBDOffViewController.
We've designated this file as part of the model instead of the controller,
because it's used specifically for the movie databases off-flow and
nowhere else.
But that was just our design decision, it doesn't have to be this way.
If we look inside the TMDBClient file,
we see that the client is only partially implemented.
These two methods here are still commented out.
Here at the top, we've moved the authentication state into this class
instead of it being in the app delegate like it was in my favorite movies.
And if we scroll to the bottom, we have some helper methods.
And at the very bottom,
we have a method that allows us to use this client like a singleton.
So, in a very similar way as to how we use the AppDelegate in
My Favorite Movies.
Now these two empty methods at the top called taskForGETMethod and
taskForPostMethod.
The purpose of these methods is to reduce the common code in our request
and you've probably noticed that there's a lot of common code between
each network request that we've been making.
We also have this TMDBConvenience.swift file,
which you'll be adding code to as well.
Now let's look at the View,
it contains a single file called Borderdbutton.swift.
Now some of you may be thinking, what gives?
Shouldn't this folder contain our storyboards and
media files since they are what the user sees, they are the View.
Well, you could do it that way and it's really up to you, but
we've decided to keep our NBC folder split and organization or
we're just focusing on the SWF files and not the other associated media files.
Our reasoning for this is because most of the actual view classes
have already been built by Apple and are packaged together inside of UIKit.
And in that way, we've decided that this View folder over here should only
contain custom code of ours that is purely view-related.
So in this case, the only custom UI code that we have is this BorderedButton.
Finally, let's talk about the controllers.
This folder contains classes that touch a little bit of the view and
a little bit of our model.
I'll start with the loginViewController.
This class is a prime candidate for why our previous app,
My Favorite Movies was so poorly designed.
In this controller, we have four methods for the four different steps that
are require for the movie databases authorization flow and
you'll note that the log in with token uses the new step to A,
which we're going to cover more detail later.
But we have the getRequestToken, then loginWithToken, which is step two.
GetSessionID and then the user ID isn't really part of the authorization flow,
but we get the user ID ahead of time, so that we can
use it with requests like adding and removing movies from favorites.
Now each of these methods, let me blow one of them out right here.
Each of these methods has a lot of duplicate code and
they make this controller a really big and heavy class.
I think, let's see, we're at 340 lines right now.
So, it's starting to get pretty large.
But what's worse is that if want to add more complexity, this controller is
just going to keep growing and growing and become really hard to deal with.
And when you have big files like this and lots of duplicate code,
then you know you have problems.
What we should be thinking about is how we make our code light, clean and
reuseable not overly complex and hard to manage?
For our apps, a great way to fix this problem is to always keep our
view controller small by migrating a logic like this,
this networking code away into the model.
So for this application,
we're going to take the code that's in this login view controller and
move it into the model or into the movie database client, this TMDB client.