Everyone.
Welcome back Johnny be here with Dev's Lokes dot com.
And in this lesson we are going to refine our log in logic as well as some other stuff having to do
with selecting a channel and this lesson is kind of mostly getting ready to work with messages.
So it might be a little bit all over the place but I'll try to make it as clear as possible.
We just got a lot of little little things that we need to fix up before we're ready to continue getting
messages.
All right.
So we're going to start out here in the channel Visi And here in the ad channel preste first thing that
we're going to do is we're going to do a check to say that we can only add a channel if we're already
logged into Want to see if off service for instance is logged in then we are able to show that modals
we're going to cut that and paste that right in here.
And then another thing that I noticed was that when we are logged in and then we log out the channels
they stay there.
All right.
So we need those channels to disappear after we log out.
So we're going covert come over to our message service starts with the file and we're going to create
a function to do that so I'm going to quickly just for a quick clean up is this function a little bit.
And folks could say phunk clear Channel's
And that's just channels dot every move.
All right.
So that's just clearing out our array of channels that we have here.
And while we're in here.
Another variable that we're going to need later is we're going to have a variable for the selected channel
so whichever channel that we have currently selected We're going to store that here from the say selected
channel and but that's going to be an optional because if we're not logged in then obviously we don't
have a selected channel so we're going to see a channel and a question mark for optional right.
It's going to save that and let's test let's test this.
But first we have to put it in the we have to call it somewhere.
So when they use the data service when we log out we are also going to call message service dot instance
dot clear channels.
So I'm going to say that.
And then the last thing that we need to do is in our policy here in our set up user info which is called
whenever the user data changes we are going to say right here in the else table view not reload data.
All right so the flow is we press log out and that triggers a user data did change notification right
here.
And so when we click log out we have set the is logged in bold to false.
So this portion of the function will be called and we will at that point the channels array will be
empty.
So when we reload the table view then itll reload with all empty.
All right.
So it was go ahead and run this just to test it to run it on iPhone 6 s and say run.
All right he's going to come over here and we have all of our channels here and I'm going to log out
and boom they are gone.
All right.
So let's continue on the next thing that we're going to do is kind of refine our log and process.
So when we first launch and this is called if we're logged in then we then we do the find use by email
and post a use of day to day change.
So right now we're calling our final channel right here.
But that's not where we actually wanted.
So we're going to we're going to create a function for for the user data to change.
But first we need to have an observer for it right.
So we're going to say notification center dot defunct dot ad observer and you see this guy right here
the observer is sulf the selector is that we haven't created it yet but I can actually just copy the
one from Channel Visi just to save us some time right here just a day to change it and when to come
back here and paste that right here.
All right so the selector is hash tag.
So look for then
chapt the dot user data to change.
And then we don't have this functions.
That's fine.
And the name of the notification is no to underscore user did to change the object is new.
OK so going through the flow again we launched the app from a fresh start.
But we had previously logged in.
So we use our find used by email function to get our information for our user.
Then we post the note refuse to do the change will be detected here in Chad Visi by this observer as
well as in the Channel see by its observer.
And then here in the.
So then we need to decide what we want to have happen in the chat we see when a user's data changes.
OK so we're going to do a check we're going to say if off service that instance that is logged in then
we are going to say this is where we're going to get our good channels which we'll figure out in just
a second.
Else if no one is logged in then we're going to change our channel label to say Please log in.
And I don't think we actually have an idea for that yet.
Missy hold down option and click on Moondust storyboard
and come down here and grab this outlet right in the sky right here and go control drag and drop you
right here and we're going to call this channel name label connect and go back.
And so here we're going to say channel name label dot text is equal to please log in.
OK.
So after we log out if we went back to the main chat the C at the top it'll say Please log in.
All right so now let's get the channels here.
Actually I think we're going to put this inside another function down here.
So we're going to funk on log in get messages.
So right when we log in this function is going to be called and we're gonna say Message Service that
instance dot get or find out find all channel completion success.
And right now we're not doing anything after the channels are gotten but just in preparation We'll see
if success and then I'll say do stuff with channels.
All right.
So save that and then call that right here.
So this is on log get messages
get on let's just clean this up.
But that I so let's go ahead and see if what we've done so far is working out.
We haven't done a whole lot just kind of lay down some boilerplate code.
And I think the one thing that will show is we should be able to still see our channels when we when
we log in and say we at the top come one two three four five six.
So you'll notice that when we logged in from here without having already been logged in the the channels
aren't showing up.
And the reason for that is that the channel view doesn't it doesn't know that the channels have been
loaded and so it doesn't know to reload the data.
So what we're going to do is we're going to create a notification that will be called from message service
that swift that will let the channel controller know hey I just pulled down all these channels so go
ahead and up load or reload your table and displayed them all right.
So that's going to be right here in place of this print statement.
We're going to need to create and unification so I'm going to open up Constans where you are here.
OK.
And I'm just going to copy this notification name pasted and we're going to say channels who did and
then changed the name to channels loaded.
All right so now we created the notification name.
Now let's go ahead and create the post and the observers for it.
So the Post is going to be in message service just lived here during the same notification center.
That default post we want this one right here.
The name is no to underscore channels loaded the object is no.
OK so after all of the channels have been retrieved from the API we're going to shut off this notification
that that that's ready to go.
All right.
And then in channel Visi I see down here we need to add an observer.
So I'm going to say notification Well let's create our selective function first.
I think that's the better way to go about this.
All right.
So we got at Objective-C funk and call this channel's loaded and the same as this guy right up here
to underscore a note of then notification.
And what we're going to do here is we're just going to reload our tape abuse data Serenity tape of you
reload data.
Right now let's create our observer and say notification center default and Observer the observer is
self the selector is hash tag selector and then channel the see that channels loaded.
That's our function and the name is known to underscore channels loaded.
The object is now going to say that and clean up a little bit spacing.
And just to go over the flow one more time and Mrs. Server start Swift after the channels are retrieved
we shoot this notification channel Visi observes that notification and calls this channel right here
channel is loaded which simply reloads the table view data rights from the Save that and run it up ready
so we need to test it from a fresh logons when to log out and then log in and spookiness have a dot
com.
One two three four five six log in.
And then we get.
So now the channels are showing up from our very own logon.
All right so that looks looks good already the next thing that I want to do is actually have this chat
view update some of its information based on which channel we select.
All right.
So let's go ahead and do that.
We're going to use the notification for that as well so jump on to Constans and we're going to create
a new notification name and just copy that and say you know to underscore channel selected came over
here to this channel so like I did I'm going to save that.
And we're we're going to be doing our work here is mostly in well I guess in both Channel B C and Chad.
DC First off in channel ABC we're going to create the function or we're going to utilize the built in
table view method for selecting and table view items.
We're going to say did select and that will pull up our function that we need to select row at.
OK.
So what are we doing when we select a row.
We are going to save the selected channel into our message service variable selected channel and then
we're going to notify the chapt VC that we have selected a channel and then we want to dismiss or to
close the menu have the chad slide back over.
Right.
So pretty simple we're going to say left channel equal when I say message serviced instance DOD channels
and we're gonna grab the channel that corresponds to the one that we select.
So index path that Arrau.
OK so now we have our selected channel and we're going to set that equal to the message service that
instance does select that channel is equal to that channel.
And then we're going to say Notification Center we're going to shut off our notification that the channel
has been selected that we just created.
So in a default top post and this is the one we want we're going to post the notification with the name
notice of channel selected object is new.
All right so again we create the channel based on which one we select set that equal to the selected
channel and should offer notification that we have indeed selected a channel.
All right.
And now what we're going to do is make the menu slide back in place.
And that's also pretty easy.
All we got to do is say self reveal View Controller and then say that reveal toggle.
And we want this one here animated with a bowl and say reveal toggle of animated true.
So we're just saying we're just accessing our review of your controller and we're calling misfunction
revealed toggle which is what we use to slide back and forth the menu.
All right.
Sue that looks good save for that.
And let's let's test that.
Just that so far.
All right so all we're doing here is we're just testing that when we select a.
Yeah there we go that when we select an item the menu closes.
All right so this cool stuff.
Then jump in a chat B.c.
So now we are shooting out a notification that we have selected a channel so we need to add an observer
for it so that we can listen for that notification saying notification center default dot and Observer
and the observer is olf the selector.
Let's go ahead and create that.
So here we're going to say at Objective-C and we say phunk channel selected and underscore and notice.
And that's of type notification.
There you go.
And then we're going to do something we'll work with that in just a second but our selector are going
to say so look after his chat and we see the opt channel selected the name of the notification that
we are listening for is a note with Channel selected and the object is nil.
All right so there we're going to save that.
And what do we want to have happen when we select the channel.
What we're going to call a function called update with channel.
And the reason we have some of these you may be wondering like why don't we just put whatever we're
going to put right here here.
The reason for that is that we're going to be calling some of these functions in multiple places.
So I'm just pre optimizing for that.
All right.
OK so here I'm going to say update with channel.
And what are we going to do here in the update with Channel.
Well right now we don't have a whole tend to do all that I really want to do is make it so that when
we do that when we select a channel that we update this right here this label to be the name of the
channel.
So we're going to do is we're going to say let channel name.
We have to unwrap our channel name from the message service that instant starts to look at channels
for say Message Service instance that selected channel
dot channel title and then we're going to use our coalescing nil.
So if it can't find a non optional stream then just set it equal to an empty string and then going to
say channel name label.
That text is people who are going to do our hash tag and then string inter-relate channel name all right
to save that and let's run that and see if that works.
All right.
So let's check this out.
Going to click on general dismisses And hey there we go.
Update our channel label right here.
So right now and now we're really starting to look like a chat app.
So good.
And a new channel and I call this water cooler create the channel boom it sets up we can add that.
All right.
We're looking really good.
All right so now this was kind of just a whole lot of logic in setting up some functions that were going
to be using here in the next several lessons.
But still some still some pretty cool stuff.
We're updating we're using notifications to let different controllers know what's going on and stuff.
So some pretty fun stuff and I'm just going to finish up by adding and committing our changes and then
I'll see you guys all in the next one.