Everyone will come back to class.
Joining me here with slopes dot com.
And in this lesson our final lesson we are going to do a number of things we are going to implement
the likes feature and we're going to do some code cleanup and refactoring.
All right.
So let's get started.
This is what our app currently looks like.
We are able to filter on the different categories that we have here except for popular that doesn't
actually do a thing for us right now.
So it's actually work on that.
All right.
So here in our set listener we are filtering.
We set an observer and to filter on category.
But in the popular what we're going to do is we're going to we're going to have all of the thoughts
but instead we're just going to order those by the number of likes.
So what we're going to need to do is we're going to have to have it here and we're going to have two
different observers depending on which of the four categories is selected if it's popular then we're
going to have a different observer.
All right.
So we're going to say if selected category is equal to the thought category that popular raw value.
So if it is popular then we're going to go ahead and copy this because we're going to need a slightly
different one.
When you copy that and paste it here when I close the bottom pain but instead of sorting by category
we're going to do is we're going to order by not time stamp but numb likes.
There we go.
If we select category is anything else then we are going to stick with the one that we had before.
OK.
So I'm going to cut and paste it instead of our else statement.
All right.
I'm going to slip all this into Control II and that formats your code just in case you did it.
And let's go and test this out and run it.
All right.
So let's see here.
Serious.
Those all still work and then click on popular.
And there we go.
All right so now we are displaying everything and ordering it by the number of likes right now we only
have to posts that have any likes and those were manually set in the database.
But it looks like it is working correctly.
All right.
Looking good.
All right.
So you may be looking at this and cringing.
You know we have a lot of code here and we have some big chunks of it that are being repeated and as
part of the dry principle we do not want to repeat ourselves right.
So looking at this I'm not really happy with the all of this parsing data that is happening here inside
of the controller.
I think this could be done inside of our thought model.
OK.
So let's go ahead and in thought.
Thus if we were going to create ourselves a class function and this means that this class can be.
Or this function can be called from the class without actually being from an instance of the class is
going to say class funk.
And we're going to call this parse data.
And we are going to need to import firebase here because we need to take into it a snapshot.
OK.
And this is a type of query snapshot.
Okay.
And then we are going to return an array of thought.
Okay.
And so here we're going to say our thoughts is equal to an array of thought.
We're going to initialize it.
And then down here we are going to return it because it is going to say return thoughts.
All right that'll get rid of that error.
And then going back to our main Visi does Swift.
I'm just going to go ahead and copy or actually cut this whole section here from the guard let snap
all the way down to the end of the for loop.
So Mike cut that and come in here to our thought and piece that right here.
And so now we're going to say Garlett snap snap shot else return and then we'll return thoughts and
empty array of thoughts all right and that looks good.
Let's see here initialiser conditional.
So this should be optional right here.
There we go.
And then we'll get rid of this stuff right here.
Start a new thought and that should actually be good to go.
So then here in our main Visi and we're going to call thought are we going to say self-taught thoughts
equal to thought
that parse data and then we're past into it the snap shot.
All right let's go ahead and do the same thing here on our other listener here.
Right here we're going to delete this and say self that fots is equal to the fact that parsed data and
we passing into the snapshot this try that and run it Kate looks like it is still working as expected.
Very good.
And this looks so much cleaner now as well.
All right so where to go.
All right so I think the only thing that we have left to do is our likes feature here.
So when we click on one of these we are adding a light to it.
All right.
So let's go ahead and work on that.
What we need to do is here in our thought cell does Swift we need to add a gesture recognizer to that
image.
OK so here in the Awake from NIB we are going to say let tap equal you I tap gesture recognizer and
we're going to initialize it.
We're going to select the second one here.
The target is self and the action is going to be a function that we haven't created quite yet but it's
going to be pounde selector and then will create that function right here.
And we've got to say at BJC this is new in O.B. J.C. This is new and so for we have to expose the function
to the Objective-C frameworks with the same objective see funk and I want to call this function like
tapped.
OK.
And so back up here in our selector we can say like tapped.
All right.
And then we can say like likes image that add gesture gesture recognizer and the recognize that we are
adding is tap and then we can say like image we need to tell the image that it should interact with
user interaction.
So is user interaction enabled.
Yes it is true.
All right so now when we tap on the image something we'll be able to happen here in the you like tap
function.
So what do we want to have happen.
So let's check out our database here in our database.
Come back here to date.
We need a path to the document.
OK.
So up until now all of our paths have been just to the collection.
Thoughts.
Now we need a path to the document.
All right.
And so it's a very similar process.
All we got to do is say fire looks like we need to import firebase.
So import firebase and we're going to get a path to the document that corresponds to the one that we
are clicking on.
So to do that we are going to need a variable to reference it.
So here in I'm going to see variables and I'm going to say private var thought this is going to be a
type thought.
OK.
And then here in configure cell I'm going to say self that thought is equal to the thought that is passed
into the function.
Ok so now that we have access to the thought we're going to see a fire store dot fire store and we're
going to say Dot collection path and that one is going to be thoughts.
Ref.
And then we burrow down further dot dot document document path and that one's going to be fought dot
document ID.
So now we have gone from collection and we've gone one layer deeper to the document and so visually
what we've done is we've said we've found a reference to our thoughts collection and then we've gone
down one layer deeper into the document.
And so what we can do then is there's a couple of ways to update the data.
I'll show you one way first and then the next we can say data set data.
OK.
And but we'll need the one with options.
So sit data with options and see what we can do is we can that said data.
I'm going to put this on a new line and what we do is we specify the field that we are updating.
And we feel that we are updating is the number of likes.
So this is going to be numb who likes and the value is the number of likes that it currently has thought
that number likes plus 1.
Okay.
And then the options here.
This is important because if we don't set the options to set options that merge merge and what is going
to do is it's going to overwrite everything else inside of the document.
So what we would end up with is a document with only likes and everything else would be blank.
OK.
So this is one way to update a field for a specific document using the data set data.
All right so let's go ahead and test this and make sure it works.
All right.
Let's go and open up our simulator and moment of truth.
When I click on this we should see this update to 1.
Oh there we go one.
Here's another one.
Up to 16.
All right now in this course we aren't going to handle the case of you know whether you have already
liked one.
And then if you click it it should maybe subtract the like that's going to be in the next target topic
when we implement users and log in.
That way we can actually keep track of who has liked what post because right now there's no way of knowing
whether this user has liked a particular post.
We just don't have that logic in place right now right.
So right now you can add as many likes as you want to any post.
All right.
So enjoy that.
All right and then here in popular now we should see this really working.
So check this out.
These both have five.
But if I click on this one then we should see this one move right up above the previous one yeah.
There we go.
So now you can see kind of how the lights are working.
All right.
Supercool.
All right.
The other way that I wanted to show you how to update data.
So I'm just going to comment this one out.
So this was the method one here is another method method too.
All right you can say in the first part is the same fire store that fire store dobbed collection.
And actually let's see here document.
Let's see if we can show you a different way of doing this part two to document path.
Instead of doing data collection that document what we can do is instead say I'm going to take thoughts.
So this is the collection of thoughts and then we can do a smash and then we can do some string interpolation
here.
Let's see here.
This is document or thought that document ID.
All right.
And so this right here is equivalent to a collection dot document.
All right.
And so just a little bit more succinct if you ended up having a whole bunch of chains together.
Then this would be probably a good way of doing it more succinctly.
All right so then after this I'm going to put this on a new line we were going to say update data.
OK.
And so here we have update data and we have one with a completion and one without it.
Don't really care about the completion at this point.
So then we were going to say the numb likes and this is going to be again the thought doesn't seem like
a plus one I'm just going to copy this and paste it here.
All righty.
So these are the two different ways you can use that sit data.
But remember you have to use the options of options that emerge if you want to merge.
If you don't want emerging You just want to override everything with whatever you're putting right here
then that's that's an option as well.
But the update data that kind of has the merge built into it.
All right so it's going to run this and test it make sure it works.
Click on it.
Click here.
And it looks like something's broken.
Let's check it out.
OK.
So I think the reason is that this is coming out as an optional.
So we're just going to force on rapid and the run that again.
Now I just check this out.
Oh there we go.
All right.
So this is all working now.
All right so we are finished.
How cool is this app.
You know we can add our thoughts.
We can sort on different categories and by popularity.
We have learned so much in this course already.
All right.
So let's let's do a quick recap.
We've learned how to create fire firebase project which involves getting your Google P list adding the
cocoa pods and all of that and then we learned about the architecture and the structure of using fire
store how you have collections and documents and how you can nest those one inside the other.
We learned about how to add documents to our database using various methods.
And then we also learned about how to retrieve that data using observers.
And one time fetches we learn how to filter and sort those and also how to update single singular documents.
Right.
So yeah I hope you guys had a lot of fun with this.
I know that I did.
All right.
And the next target topics that we cover we're going to be looking at authorization and log in as well
as security rules right.
So I will see you around.