Welcome back to class everyone Johnny B here with devil slopes dot com and this does and we are going
to pull down the firestorm of data that we are holding in our comments collection and display them.
Right so in the last lesson we figured out how to add our comments.
So now we need to display them.
So it's going to be very similar to what we did in our main Visi where we had our collection of thoughts
and we create a listener.
Let's see here.
Here is our thoughts.
A listener of type firelit listener registrations were going to create a listener and then we sit that
listener and we add a snapshot listener and then we pass the snapshot to a parse data function in our
thought cell here where we go through and we loop through all of the documents in the snapshot and parse
out and pull out the data that we need create a new thought and add it to the thoughts array.
And that is returned.
So those are going to be very similar except anywhere you see thought is going to be replaced with commande
pretty much OK.
So let's get to it.
Jumping into our comments Visi is go create our listener variable here soon to say our comment listener
is of type fire listener.
Fire listener registration that go up and in view viewed it up here.
We are going to add it.
You did appear we're going to add it.
And then before I forget in view to disappear we need to remove it.
OK.
So here we have a comment.
Listener to remove.
And that's just to avoid any memory leaks or anything and anything like that.
All right and so here in view did appear what are we going to do.
We're going to say comment.
Well the scenario is equal to.
We're going to say fire store thought and then we're going to get a reference to our thoughts or our
comments collection and to that we've got to go through the thoughts collection down to the thought
that we are selected and then into the comments subcollection.
So we're going to say a collection path and this is thoughts ref.
We're going to go one layer deeper into the document.
And this is a self thought that document I.D. and then I'm going to do a return and a dot and then one
more layer which is collection path and this is our comments ref.
And then we are going to say Dot and snapshot list here.
And we're going to get our completion handler here.
We're going to have a snapshot and we're going to have an optional error as well and close this bottom
pane here and I'm going to unwrap that snapshot.
Some say guard let snapshot equal snap shot else.
So if this doesn't work then there was an error.
So we're going to say Debug.
Print say error fetching comments and then I'm going to print out that error.
OK.
And then we are also going to return to this function over if it was successful then we have a snapshot
that we can use and what we're going to do is we are going to go ahead and jump into our comment that
swift file here and we have this function that we copied over from our thought that's a file and we're
going to come into it.
And we're just going to modify it for our comment.
All right so instead of a an array of thoughts we're going to make an array of comments and instead
of thought class we are going to have a common cause and right here instead of returning an array of
thoughts we are returning an array of comments and see here that looks good that looks good if you're
going to return comments here.
We're going to say comments penned and we'll leave that as it is for now and let's see here and here
we're going to return comments OK.
All right.
So data is good.
Username is good.
Time stamp is also good.
And we don't need these the last three here.
And instead of thought text we want the comment text.
And this is a comment text and that looks good.
So I'm going to delete up to here.
So instead of a new thought we are going to instantiate a new comment and that is equal to a comment
that initialized it.
And we're going to initialize it from the user name the time stamp and the comment text area.
Now we have a new comment.
And so what we're going to do is we're going to say comments that append a new element and a new element
that we are pending is new comment.
All right and just to run through what we have done over here in our comments Visi and we were going
to call a comment that parsed and data and we're going to pass into it a snapshot and this snapshot
contains all of the comments in our comments document our comments collection.
Right now we have two documents in our comments collection for this particular document.
And so we were going to pass that snapshot into this function here.
If we jump to it we then create an empty array of comments and this parse data function returns an array
of comments and we go through the process of unwrapping the snapshot.
Then we look through the documents in the snap and we extract the data from each one looking for the
user name time stamp and comment text.
We then instantiate a new comment from our comment initialiser and append that comment to our array
right here.
And finally we return that array back into our comments.
AVC right here setting it equal to our array of comments right here.
And we then use to that we then use in our table view methods.
OK.
So right here before we do this in the event that another comment is added instead of going through
the whole process and adding even more Commines Plus the 1 to our array we're going to remove the comments
from I say self-talk Commines dot remove.
So we want remove all.
And then after that we say self-taught table view dot relo data and then that should display the data.
And unless I forgot something which is possible this should work.
So go ahead and run it.
All right so let's go to our serious category and click on our thought that has some comments.
And oh there we go.
This work.
Let's see here.
Johnny Be Good.
Looks like I've missed something up here so let's go and check that out here in our comment cell.
Oh right here.
This should be a time stamp.
Time stamp text.
That text is equal to the time stamp.
All right let's try that again.
All right going back to our serious category.
Clicking on our first one here and there we go.
So now we have our username our time stamp and our comment.
All right.
Awesome.
So let's go and try this trying out a live.
When I press this and this should populate who there we go.
All right.
And so you can see that this isn't quite ordered right.
So let's go ahead and fix that back in our comments AVC as he hears it.
And this to a new line.
And then here I'm going to add another one and say Dot horder.
And we want to order by string and the string that we were ordering by is false.
There you go.
Let's try it again.
Right let's go ahead and click on a new one let's add a comment.
I'm going to say Sleeping is the best and I had it.
And boom.
There we go.
Come back out.
Now this is one.
This is at three now.
So now we have three.
That looks good.
All right guys I think this is working.
So now we have a whole Commons feature built out.
Now anyone can add comment.
Anyone can add a comment and it gets added right away with your user name and the time stamp was go
ahead and log out and create a new user with a different name.
Here I'm going to say be at b dot com password is 1 2 3 4 5 6.
User name is Bella create the user sign in
and then come over here.
Add a comment here new comment from Bella added.
And now we have a different user posting here.
All right so this is a great way to go put yourself on the back.
We have this app is becoming more and more full featured.
We can add posts we can authenticate we can log in and now we can add comments on individual posts.
So it's coming along very nicely.
Before we finish up I want to address that keyboard issue.
So if we click on one of our comments and we click here and we have the key the software keyboard toggled
then you'll see that our keyboard.
It covers up our textfield and we can't see what we are writing right.
So that's no good.
So we want to do is we want to be able to make it so that when we enter our textfield that the whole
view is actually going to shift up and keep that textfield on top of the keyboard.
All right and it's kind of a little bit complicated and it's outside of the scope of this course.
So what we did is I just included a helper File here in our assets.
So if you go ahead and take a look in your assets folder and look for the file keyboard a bound view
swift and if you just drag that into your project and put it in utilities and I'm going to say finish
and then make sure that the targeting membership is checked.
And what this does is let's just take a look.
It's an extension for a view with a function bind to keyboard and basically what it does is it listens
for whenever the keyboard is shown and hidden.
And what it does is it will whichever of you that you bind this to the keyboard will keep it right on
top of the keyboard.
All right.
So it's pretty cool.
And what we're going to do is here in the comments Visi and view to load all we're going to say is self
taught view that bind to keyboard and then let's run this and see how it looks.
I can click on one of these and when I click into the add comment I've got to toggle the software keyboard.
There we go.
So you see that the entire view kind of just slid up.
All right.
And that's that's pretty good.
But now when we finish
nothing happens it doesn't go away so don't fix that here.
When we had come and tapped in our completion down here once it's all done all we're gonna do is we're
going to say self-taught had come in tech start to resign first responder and let's run that again.
And come into our comments and click on it and then slides up and then we can add another comment and
added And then we slide back down.
All right.
Perfect.
All right so that pretty much takes care of that keyboard issue.
And you are able to use this file in any of your projects that you want to write.
All right so the last thing that we have for this course and this app is securing it.
Right now we still have this open to anybody to modify and write and read to our data so the next lesson
we're going to talk about security rules and how to lock it down so that you can specify exactly what
you want your users to be able to read and write.
All right.
So we'll talk about that next time and I'll see you then.