Everyone.
Welcome back Johnny be here with Dev's Lokes dot com.
And in this lesson we are going to refine our loggin 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 I want to see if off service dot instance dot is logged in then we are able to show that
motile.
I want to cut that and paste it 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 Kovar 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.
I know it's good to 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 channels
so whichever channel that we have currently selected We're going to store that here is to 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 Channel and 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 the use of data service when we log out we are also going to call a message service instance
not 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 you dot 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 Bhool 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 with all empty.
All right.
So let's go ahead and run this just to test it to run 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 user data did change.
So right now we're calling our find all 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 default 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.
Use the data 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 and then
chapt the dot user data to change and that we don't have this functions.
That's fine.
And the name of the notification is no.
Underscore user did it 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 if you user do the change.
This will be detected here in Chad Visi by this observer as well as in the channel Visi 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 dot 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.
Hold down option and click on main story board
and come down here and grab this outlet right.
This guy right here and go control drag and drop you right here and we're going to call this channel
name label it 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 we see 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 phunk 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 instance
that 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 to stuff with channels.
All right.
So save that and then call that right here.
So this is one loggin get messages
get let's just clean this up.
But that right 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 be at the top come to three four five six.
So you'll notice that when we logged in from here without having already been logged in 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 both here through 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 them here we need to add an observer.
So I'm going to say notification.
Well let's create our selector 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.
I say 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.
All right.
Now let's create our observer and say notification center that default does and Observer.
The observer is self the selector is.
Hash tag selector and then channel the C 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 more 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.
All right we're going to save that and run it on Friday so we need to test it from a fresh logons when
I log out and then log in and cygnets.
Dot com.
One two three four five six.
Log in.
And there we go.
So now the channels are showing up from our very own logon.
All right.
That looks looks good already that 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 I'm going 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 on this channel so elected.
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 Visi 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.
When 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's selected channel and then
we're going to notify the chapt VC that we have selected a channel and then we want to dismiss We want
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 service instance dot channels
and we're going to grab the channel that corresponds to the one that we select.
So index paff dot row.
OK.
So now we have our selected channel and we're going to set that equal to the message service that instance
does select the 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 the default post and this is the one we want.
We're going to post a notification with the name notify 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 you'd 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 saurians he revealed toggle kind of made it true.
So we're just saying we're just accessing our revealed view controller and we're calling misfunction
revealed toggle which is what we use to slide back and forth the menu.
All right.
Sue looks good and save 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 let's close stuff then jump in a chat B.
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 and say notification center default 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 will work with that in just a second but our selector we're going
to say so look for his chat.
BBC datt channel selected the name of the notification that we are listening for is note with Channel
selected and the object is new.
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 pretty 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 flipping channels we're
going to say Message Service that instance the selected channel
datt channel title and then we're going to use our coalescing Neal.
So if it can't find a non optional string then just set it equal to an empty string and they're going
to say channel name label.
That text is people too we're going to do our hash tag and then string interpolate channel name.
All right.
So 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 at so good.
It had a new channel and I wouldn't 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 our using notifications to let different controllers know what's going on and stuff.
So some pretty fun stuff.
And know 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.