Hey everybody this is Caleb with Dev slopes and in this video we're going to actually start searching
for users based on their email from our data service.
It's going to be so cool.
This is my favorite part of the app getting to search for users and add them to groups.
So let's go ahead and let's pull open our X code project and we're actually going to begin by going
into the data service because that is where we're going to write our function to get an email returned
back to us for a search query that we pass in from our YOU textfield.
So let's go into our data service and we're going to add a function.
Let's let's put it down here at the very bottom.
OK we're going to call it phunk get e-mail.
Meaning that's what we want to get back for.
Search query.
We're going to pass in a search query of type string and you know let's actually go ahead and give it
an internal parameter here of queery.
And what we want is for it to return an array of all of the potential emails.
Let's say that we searched C-A maybe we would have Carl Cayla.
That's not an alphabet order but Karl Kalib you know it would show all of this C.A. starting emails.
So we need an array returned back to us of all of the potential options.
So to do that since we're dealing with firebase we're going to need to use a completion handler that
is escaping.
So it's an escaping closure that lets the values escape out of the closure and we can pass it back to
this function.
So let's go ahead and create a handler.
And of course like I said it's escaping.
Now we've done this like 100 times in this course and we'll do it a few more times but we're basically
creating a function that we can pass values into and we're not returning anything to this.
We're not returning anything to this function explicitly but we do have access to it whenever we call
it from somewhere else.
So like I said we're going to need an e-mail array.
So this is going to be an array of type string.
Ok lots of strings lots of e-mails and we're not returning anything to this function but this function
will be available to us wherever we call it.
Go ahead and give it some curly brackets.
And I want you to notice here in the GET ALL feed messages function.
We created an array of message at the very beginning then at the very end after we had filled it.
We'd handed that back to our handler.
We're going to do a very similar thing here.
So I'm going to go ahead and create an e-mail array similar or actually exactly like what we need to
return at the end.
So go ahead and type var e-mail array and it's going to be an e-mail of type string and we're going
to instantiate it.
Now we're going to go ahead and look through our users reference and we're going to write a for loop
that's going to search through all of them and return the ones that match our search query.
So go ahead and call ref users and we're going to go ahead and observe every user.
So we're going to observe the whole reference and we're going to watch for values.
Now if you've never used firebase before I probably should have explained this but value means any data
changes at a location like our users reference or recursively at any child node.
So we're not going into a child here specifically not a specific user yet.
We're just looking at the entire reference for all of its values and we're going to go ahead and get
a snapshot bakso press enter and I'm going to call this user snapshot.
OK.
So now that we are observing the user snapshot we're going to go ahead and we're going to create an
instance of user snapshot and then we're going to loop through it like with a for loop.
So go ahead and type guard let users snapshot equals user snapshot which is the one we passed in here.
Children not all objects we're going to cast this as an array of data snapshot.
I'm moving quickly because we've done this before else meaning if we can't do that we're going to go
ahead and just return and get out of this function.
All righty so assuming everything is good to go we get some users back.
We're going to go ahead and look at that user snapshot and we're going to go ahead and type for user
and user snapshot.
Now we're going to loop through every single user and we need to actually take the user and pull out
their e-mail.
If you remember in our firebase array we have a bunch of users here and each user has a unique ID an
e-mail and a provider.
We're going to pull out the e-mail and save it into a constant.
So go ahead and type let e-mail equal user OK so we we cycle through we start appear at the very tippy
top with this guy.
Maybe it's not going to open up.
Interesting let me refresh.
Looks like it's a little sleepy sleepy firebase.
There we go.
OK.
So if we open up this user we can cycle through and we can pull out his e-mail.
So the key is e-mail and the value is Marty.
Great Scott dotcom.
So go ahead and type user child snapshot.
And of course the child that we want is called e-mail.
Now we don't want that though we want the values that we're going to type value and we're going to cast
that.
We're going to force cast that as a string because it comes in as a value of any.
So we're going to go ahead and forecast it to be a string.
So we now have the e-mail of the user.
And now this is where we're going to check to see if the queery let's say we passed in CA we're going
to see if the e-mail contains that in any way.
So we're going to go ahead and type if e-mail.
And here's a really cool we can do there's a function for strings that we can type called Dot contains
and we can check to see if this string contains another string.
So if CA is a part of this string then we can check to see if it's there.
So check it out if email contains queery what we searched.
OK.
If that's true because.
Sorry.
If you look you can see that this returns a boolean.
So we need to check if email contains queery is true then we're going to go ahead and type e-mail array
and we're going to append the e-mail.
OK the e-mail that we returned the e-mail that we pulled out of our user snapshot.
Easy as that.
But what if we cycle through.
And for instance the account that I'm logged into right now is Harvey Dent com which is one of these
what if I cycled through and found myself ok.
I don't I shouldn't be able to add myself manually to a group I should automatically join a group or
I shouldn't be able to appear in the search results.
What I'm saying I shouldn't have to add myself to a group manually.
So I'm going to also check to see.
And so if the e-mail contains the query and the e-mail is not equal to my own e-mail and you know how
we're going to check that we're going to use firebase surprise surprise.
We're going to go ahead and call off.
Current User e-mail.
All righty.
So if and only if the e-mail contains the query and the e-mail is not my e-mail then we're good to go.
Now it's yelling at us.
Oh and it's asking that we optionals unwrap the current user.
That's fine.
So this is a way that we can search through and we can create an array of all the emails that match
a certain search query at the very end of this for loop.
We're going to go ahead and we're going to return or not return sorry we're going to use the completion
handler.
So go ahead and type handler and if you look you'll see it's asking for an array of type String.
We have one that's called e-mail array.
Very cool.
So that's going to go ahead and fill up our array.
We can return it here to our handler and now wherever we call this we can get an e-mail array for all
of our users from firebase for the search query that we pass in.
It's very very cool.
So what we're going to do is we're going to go head and head into create groups Visi already and we're
going to go set up how we can use this.
So let's think what we're doing right now is we are just showing static cells and we'll fix that in
just a second.
But for now what we're going to do is we're going to set up our email search textfield to basically
have some way of knowing when we are typing.
Because if I type C it should search that query and find all the e-mails starting with see if I type
C.A. it should update and then show all the text fields starting with CA.
So we need a way to identify and basically utilize the power of these text fields.
And so in order to do that we're going to go ahead and conform to you.
Textfield delegate and let's do that down here.
Let's type extension create groups AVC and UI textfield delegate.
OK.
Now you'll probably notice that if I build and run we're good to go there will be no errors from this
because there are no required delegate methods.
But we we can definitely use them to give us the ability to monitor what's going on in the email search
textfield So now that we've conformed to that we can go ahead and type email search textfield delegate
is going to be equal to self.
OK.
Now this gives us the ability to control and interact with our textfield the way that we want to.
And the coolest thing is we can set something up that will basically observe and monitor whenever we
type anything on the keyboard if we type a letter if we delete something it's going to go ahead and
call a notification every single time that happens.
And here's how we're going to do it.
We're going to call email search textfield and we're going to use what's called add target K and add
target basically allows you to add a target a selector and a control event.
OK.
Now the target of course is self.
It's the UI textfield itself the selector.
This works just like a tap gesture recognizer or you swipe gesture recognizer.
We're basically going to call Pound's selector and then whatever goes in here whatever function goes
in here will be called for a certain UI control event.
And there are tons of them check this out.
If I put a period here you can have it be called on any editing event.
All events all touch events editing change editing began.
Touch cancel touch drag.
There's tons of them.
But we're going to use editing changed because we want to know are letters being added.
Are they being removed et cetera et cetera.
So that is what we're going to do.
The target we're talking about is editing change that's how we're going to monitor what is happening
in this textfield.
Now we do need to write a function that will be called every time that the editing is changed and we're
going to write that right here right bloviated load.
So go ahead and write phunk text textfield did change.
And this is going to be our own function.
Now in this textfield we have two things that we need to think about.
Obviously when we're typing text it should be searching and updating an email array.
And if we delete everything back down to nothing if we remove all the characters it should clear the
array and it should reload the table.
The table view.
So there are no cells.
So we're going to basically check to see if it's empty.
It's going to empty the array and it's going to reload the table view if it's not empty it's going to
do the search and we're going to fill an array in this view controller.
So we're going to start by typing if email textfield text is equal to an empty string meaning if there
is nothing in the text field we're going to go ahead and just reload the table view table what is it
called oh yeah is Table View.
OK.
Table View reload data.
Awesome.
But what if there is still data that is accessible to this table view it's not going to clear it out.
So we actually need to create an array that our table is going to pull from and we're going to do that
very similarly to how we've done this in view controllers in this app in the past.
We're going to create an array in this view controller and we will fill it up later when we call get
email for search queries.
So go ahead and type var e-mail IRA and this is of course going to be an e-mail array of type string
and we're going to instantiate it from the beginning.
So that's good.
And now we're going to go ahead and we're going to just say if the search field is empty We're going
to make sure that the e-mail array whoopsies e-mail array is also empty because then when the table
view reloads there will be nothing displayed in the table view.
But what if there is something in the text field so else if there is something in the text field what
we should do is call data service instance dot whereas our function here to get email for search query.
Now where's that search query coming from.
Our textfield.
So let's go ahead and let's call e-mail search textfield text and we need to unwrap it forcefully by
the way.
It'll give us an error if we don't.
And that's how we're going to pass in our search queries so every time that it's edited we're going
to go ahead and pass in whatever it says.
So if I type a c it's going to search for all the e-mails and update the table view if I type C-A it'll
update it again and show that all the current emails.
Very very cool and totally realtime.
So this is where we're going to use our handler to return our e-mail array and fill up the one in this
view controller.
So go ahead and press enter.
We'll call this e-mail Luray and we can basically set it up to be like this self e-mail IRA meaning
the one that we have in this view controller is going to be equal to e-mail array meaning the one that
we get returned Let's call this return e-mail or just for specificity returned e-mail or right.
Awesome.
And once those e-mails come in we should call self table view not reload data so it can show the new
search.
Now of course this function is going to do nothing unless we call it from our selector.
So let's go ahead and call textfield did change but watch what happens when I try to build it.
If you've ever used you I tap gesture recognizers you know that you need to expose the function to Objective-C.
And so we can do that by simply clicking fix or typing at 0 BJC.
So I don't know about you but I want to go test this out.
There's only one thing we need to do to actually make this work and that is to set up our table view
to actually pull from the e-mail array.
Now of course returning 3 is great but we might have more users than that.
So let's go and pull it from e-mail array because those will get downloaded and we're going to just
take the count of the e-mail right now to get each user's e-mail.
We're going to go ahead and we're going to use the index path of the table view to pull out the right
e-mail from our e-mail right at the right time.
So for every e-mail we can go ahead and just type email right and we can pass it the index path.
That way if we have the first cell it's going to pull out the first user from the e-mail array and pass
that in as the proper cell.
Now later we're going to set up the selection status but not right now.
Let's go ahead and build and run this let's go see if our search function works properly.
Let's go see how we did.
All right.
So here we go.
Let's pull open our simulator here.
Firebase for now.
OK.
So go into groups and if we pull it up there's nothing so we shouldn't see any cells.
But as soon as I open it up let me type A C.
Ok cool.
All of these users have Cs of course because there is dot.com.
How about let's just search for Evan so.
OK dev slopes devs slopes.
But how about Evan himself.
Awesome.
So Evan shows up singularly And if I erase it updates the search every single time.
So Mark Marty and Mark both show up because they start with a R and Marty only shows up.
That is so cool.
OK so our search is working beautifully.
We can now search through all of our users and oh let's let's see what user we're logged into on this
account.
Let's double check.
I have not yet set up the dismiss function.
Looks like we have it here actually in our view controller file and we go back and set that.
My mistake
OK so close button was pressed is supposed to dismiss the view controller.
So go ahead and type that dismiss.
Animated is true.
Completion is nil.
Now Dunbarton we're going to save that later we have a function to create a group and to save it up
and firebase but we're not going to use that yet.
So for now we're going to go ahead and check to see what user We're signed in as and we're going to
try to search for ourself because remember we shouldn't be able to find ourselves where say we're logged
in as Batman.
Gotham dot net so let's go search for us.
Let's see the Bat Man.
OK good.
So we can't find ourselves because that's our user that we're trying to create the group from when we
click done later we're going to add ourselves into the group.
But for now when we create a group it's only going to show us the accounts that we search for.
Super super cool guys this is amazing.
In the next video we're going to set up our table view cell so that when we tap add group when we search
for an e-mail and we select them we're going to set it so that the check mark shows it's going to be
hidden by default.
Then what we're going to do is we're going to set it so that when we select a person their email is
going to be added to this label.
And so we can search back we can go find another person at Marty at Harvey ad Chuck and they will all
show up in the survey.
Then when we click done it's going to go ahead and it's going to create a group upload it to firebase
and then that will be that we're going to start that in the next video it's going to be super cool.
Again guys this is one of my favorite view controllers.
I think it's the coolest one in this app.
So get excited we're going to head over to the next video and set that up now.