 Hello everyone this video introduces code for working with Google's cloud fire store and you can grab the code from the link down below and This is a clip that was kind of recorded a while ago and has been kind of cut together for this one So if you hear some references to things that maybe are not relevant or if you see that the screen is a little bit different That's why the the video you're about to watch is a little bit older But everything is still Relevant in terms of the content in terms of the code. So if you have any questions, please let me know I'm gonna focus in this video just on the basics, you know Like what it what it means to talk to the fire store how to send data up to it how to get the data back and About as simple a way as possible. Okay, and hopefully that will help you understand the process a lot more And get something workable going All right, so I have my trusty fire base example application here It's got a slightly new layout on the main page. I just couldn't stand the look at those old buttons anymore What you'll notice here is there is a new activity called fire store plus list view And so that's what we're gonna spend our time with we're gonna ditch the recycler view We're just gonna put in some simple data using our app into the fire store and then Query the fire store to get that data out. Okay, so let's click on this activity right away. There's not much to it In Android studio, we're going to be doing all of our work in Firestore list activity here and then activity firestore list dot XML the layout file for this activity When you check this out from github, you're gonna see a lot more code in here follow the instructions on the title card at The beginning of this video to get to this starting point where I am currently at. All right, so let's build this together So what do I got here? I've got two text fields to edit text fields, excuse me and What I'm doing is I'm gonna put the names and ages of people into the fire store And I was thinking this could be maybe like patients in a hospital something like that Okay, so, you know, you can type in a name here if you click on the age You can pick numbers. I have no data validation going on here For this demonstration, all right, so obviously that's not a valid age But anyway, this is all the app does it has these two things And you can't do anything you can just type in now All right, so let's Take these two fields which are already all defined for you and let's Get the data out of them first of all. Okay, so I'm gonna go into my layout file Which is activity firestore list on XML. Let me make this bigger so you can maybe see it a little bit better My big head is still in the way a little bit. That's got to be a little bit better All right, so I'm in my layout file. I've got my labels and my Textboxes. Let's just make a submit button really fast. Okay, so I'm gonna make a submit button We'll wrap the content. We'll wrap the content should always give your you don't have to but it's always a good idea to give your View elements an ID so that you can then refer to them from your code, right? So I'm gonna give this thing and I make extensive use of auto-complete, right? I'm gonna give this thing an ID of I'll just call it the submit button easy to remember Okay What text do I want to show in the submit button? I'll just show the text submit Now Android Studios, I'm happy with me here It's showing me this red because it wants me to put this string into the strings dot XML file I should do that. I'm not gonna do it trying to get this done relatively quickly All right, I am in a constraint layout here So I need to set this buttons location according to constraints So let's just put it in the middle of the screen horizontally the way to do that Let's constrain it to the start the start of the button To the start of its parent Okay, and let's constrain constrain the end of the button to the end of the parent That'll center the button horizontally. You can actually see it move up there, right and in a little Preview screen. All right, let's put it below The edit text so we're gonna constrain the top of this button to the bottom of the guy above it one of these things and What is age edit text? That's the idea of edit box So if we do age edit text All right, now we've got our submit button down below All right, that's pretty good. And then of course the final thing is we need to set up a click listener The click listener is the name of the method that will be called In our activity. I haven't defined that method yet. So I'll just call it on submit click, okay, and Android studio is giving me a warning here because it's saying This method public void on submit click was not found in my activity. Okay, so let's go do that Let's go put that in the activity switch back over to my fire store list activity All right, and shrink this out So let me define that method that it wanted me to define. So it's gonna be public Void always public void for these click listeners What did I call it? On I gotta go get the name I Called it on submit click On submit click and then this method also needs to take one parameter, which is a view All right, so we're clicking this thing and now what we need to do is we need to get the data out of the text boxes Okay, so first of all I need to get the text box widgets So that I can get the values out of them and this pattern you ought to be familiar with by now in order to get the View out of our layout we call find view by ID and then we give it the ID that we defined Over here. So the ID for the name edit text box is name edit text Okay, our ID Name edit text and you can see, you know, if everything looks nice and purple here, you're in good shape Let's get the text box For the age while we're at it. So I just duplicated the line Gonna make some edits here in the interest of time Okay, so now I have my text box widgets Let's actually get the values out of them. Okay, so the way that I can get that is String the name is going to be get the edit text box and then call get text Okay, and that gives it. Why is it so unhappy here? Because Get text does not actually return the string which is kind of interesting it returns an Inner portion of the edit text box. So but the solution is to call to string on that and now we've got it Okay, so Let's get the age The age string now this is an edit text box. So it's gonna be This content is a string even though it's a numeric string it's still a string So I get this back and it's a string But maybe it's gonna be useful for me to store this as an actual integer The preferred way to convert a string to an integer in Java is to call integer dot parse Oops, I spell integer correctly parse int and then give it the string that you want to convert into the integer now if this thing Happens to not be a string You'll get a nasty exception here Which you should handle when you're doing your validation and things like that. I'm not going to I just want to get this going So the final thing I'm going to do is let's just make sure our submit button is working So let's log the statement to the log cat Okay, so log cat takes two parameters. The first is The tag the tag is usually you want to give it the name of the activity as a string Okay, so I'm gonna give it the name of the activity as a string and then what's the thing that you want to report? Okay, so let's say What do you want to log submitted? Name was the name and then the age was Let's give the age String here because you can't concatenate integers with strings All right, so this should Give us some log output. All right, let's rerun this To get the changes applied Okay, I'm back at my main menu. Let me pull up my log cat down here at the bottom Go to the firestore list view type in a name age Other good old days click submit All right, and something popped up in my log cat submitted the name Lucas with age 21 okay, and if I click this again, it just keeps firing off Let's make sure the changes are working So this code is executing great Now what we want to do right? We're working with the firestore here We want to put this thing up into the firestore as data So at this point we could do that But in Android in particular, it's much more useful to think of the concept of you know the name and the age not as just two separate pieces of information but as Belonging to a person or a patient, right? I said that our metaphor here was we're doing like patients at a hospital So what we can and should do at this point is create a data object It's gonna hold this information. Okay together name and age. So Let's do that. Let's make a new plain old Java object So I'm clicking on my package up here And I'm gonna make a new right-click new Java class and I'll call it patient. Okay. No everything else I'm just gonna leave as it is Okay, that's me if I want to add this file to get sure why not Okay, so what constitutes a patient a patient has two attributes their name and their age So let's define those Good practice is to make your class attributes private unless there's a really good reason not to do that so the name and An integer for the age so What we will know kind of looking ahead what I can tell you is that firestore will provide with up to us some mechanisms for automatically converting a good old Java object Into the form of data that firestore needs to store. Okay So if we construct this Java object correctly, we can create these objects and the firestore will just store them for us also later when we query the firestore it will Automatically create these objects for us. Right. So we're dealing with objects instead of like lists of names and ages and things like that's much more convenient So what we need to do though for that all that to work is we need to create two Constructors for the patient class and no argument constructor an empty constructor and an all argument constructor Android Studio makes us really easy If you are in here, you can right-click and Go to generate Okay, or hit all insert or option insert generate Constructor Okay First let's make the no arguments constructor select none down here and There you go. You have a no argument constructor kind of your default constructor All right, let's make the all argument constructor. All right clicked again generate Pick the constructor and then select all of these right multi-select everything click, okay Voila generated the code for you The other thing you need to do is you need to generate the getters and setters for all of your variables Okay, the way to do that once again, right-click generate Getter and setter Select them all Okay, and there you go The naming convention here is really important. Okay the name here Set name and this it's gonna be based off your name here These things are important. So Sticking with the defaults is fine, but you cannot rename these getters and setters if you do it's gonna break things All right. All right. So now we've got our patient. Let's go Back into our activity our firestore list activity and let's make a new patient Right based on this data. So my new patient P is Let's use one of those constructors The all argument constructor took the name first followed by the age New patient there we go Okay, so now I'm creating a new patient and we can check this out instead of calling This let's call it with P dot get name and P dot get age It's probably it's gonna be mad again because this is an integer Integer dot two string, but I can convert it back to a string I'm just you know, this is all kind of necessary. Oh, no, I don't need to do it Java smart enough now Okay, so when I can cat name This it should be fine Let's rerun this run our app again. All right, let's type something in here And here it is so I'm getting my output. This is good All I've changed here is I'm putting the data into a patient object and and printing it out All right, but this was an important step. Okay, so now we want to put this patient Information whenever we click submit up into the cloud fire store Okay, so you need to have a cloud fire store project going on and working here If you've already done the fire base example projects, you should have one Here's my cloud fire store in my project I already have a few collections a collection is a group of related objects, okay So I've you I have people and plants for my other fire base Example projects. I have location recordings from something else I'm gonna make a new collection called patience. Now. I don't need to do that ahead of time I can I want to put all these new things Into my patients collection, but there's no reason you don't have to come in here and create it ahead of time Right fire store will do it for you The first time you put the first patient into the collection, right? So but let's do that Let's put the patient up there. What is that going to look like? So here's the call that you need to make First thing is we need to get an instance or the instance of the fire store Database connection. Okay, you should make this a class variable. So I'm here at the top of my class in fire base and I'm gonna do private fire base fire store I'll call it MDB right my database and You initialize it by calling fire base Fire store dot get instance. Okay, so this get instance call Um Magically initializes the connection to the fire store for you and as long as you have your Google services dot json file In your app folder. This is gonna work. Just fine. Okay, so this initializes This object for the fire base fire store and now we use this object to put things in the database Delete things from the database query the database, etc. Etc. All right, so let's go down here And let's put something in our database. All right, so I'm back in the submit click And instead of just logging the thing. I actually want to put data in. All right So the way it does this let's we call let's get our fire store object MDB The first thing you do is you call a method called Collection and you give it the name as a string of the collection. You want to put it in All right, so right now I have people collection. I have plants. I have location recordings. I want a new one I want to call it patience So I put the string that I want in there. Okay Alright, so that says the this is kind of a strange syntax I'm gonna talk more about it in a second So now I have a reference to this and now what I call is dot add And I need to add the thing that I care about. Well, the thing that I care about is this patient, right? Add P Okay That's it That should get our data up in the database if we've done everything correctly. Let's go ahead and Apply changes Rerun this thing if we need to Okay, adding I got a rerun the app. It's fine Okay, going into my fire store and list view Now I should still get the log statement to make sure that we're seeing that Okay, submit Okay, submitted app didn't crash. That's always a good sign. Let's go over to our fire store. Okay Let me refresh this page Okay, so now here I've got a patient's collection and It's created one document in it and that document has the name Lucas with the age 21 So our data is actually going up there. That's great If I submit again It will actually, you know add to the collection see I've got two of these now man Let's make someone that looks different Submit him. All right So now I've got three people in my collection, right? So data is going up. That's great This syntax is called like the builder pattern Where you're chaining all these method calls and kind of adding more functionality on here now One of the things you should always do right is every User action should have tangible feedback to it So what we want to do To give feedback to know that everything is going well is to add Success and failure listeners to this call this call to add is an asynchronous call What does that mean? If you remember the Android user interface thread is one thread and Anything any long running process that you would do here Like say you had a for loop or a while loop that was infinite if you had a while loop here that was infinite It would block the thread right the thread There's only one UI thread if you do something long running or silly in here your app will freeze That's what it'll look like anyway Add is an asynchronous call which means it forks off a background thread to go and put the data up there Okay, so Android Let's you know just for the sake of argument You don't have to follow along. Let's just for the sake of argument say that we have a print statement after here This line will execute and this line will immediately execute after it Android doesn't sit around waiting for add to happen It doesn't wait for the internet connection to open for the data to be sent for the response to come back It doesn't do any of that it immediately goes fires off a background thread and that background thread does its thing Putting the date up in the cloud But then this line of code goes it's not waiting on that background thread. It's an asynchronous call Okay, so if we want to get the result of one of these asynchronous calls we have to Or do something when it finishes we have to add and A listener to it, right? This is the mechanism that uses so we're gonna add an on success listener And let me format this a little bit better So you can see what we're doing right? I'm just doing some formatting. So adding some an on success listener I'm gonna create an anonymous inner class right here To do this new on success listener, right? I'm making extensive use of auto-complete here. So I type new on I get the on success listener I'm that's the one I want hit enter and it fills in some information for me Okay, so what do you want to do now? This method is gonna get fired off whenever the data was Successfully connected to the firestore and shoved into the firestore. So what do you want to do? Well, let's make a log statement. That's one thing Let's make a log statement I'm in the firestore activity the first part of the log statement should be the name of the activity I'm getting it to the point now where I'm using the string twice So that means that's a pretty good candidate to put it in a Field or reusable Variable so let me create that private static final string Tag is the convention This right so now I can reuse this tag over and over and over The static final part of this basically means this is a constant. It's never gonna change. So now instead of Putting firestore activity here every time I want to Log something. I'm just gonna put reference that tag and what do I want to say? Let's just say patient entry added successfully I'm gonna quickly rerun, okay, and you know what let's So that's useful for me the developer, but let's tell the user something too Quick and easy way to tell the user something is to give them a toast right a little quick pop up So let's make a toast toast Dot make text. Let's call this thing right here Okay toast dot make text first thing the toast needs is a context the context is usually the activity because we are inside an Anonymous inner class right here. We have to refer to the context in a slightly different way We can't just say this Normally you would say this but this is not an activity. It does not have context. It's one of these successful listener things So the trick is you got to refer to your activity firestore. That's wrong on firestore list activity Dot this okay Toast make text. What do you want to say? Let's say the same thing we say in the long file patient Entry added hooray, and then the final parameter to make text is how long you want it to show That's specified using a constant. Let's do short And then the final step here is to call dot show. I don't make it actually pop up All right, let's rerun this Okay, go to our firestore dot list view to put some data in Bob is 55 and submit and if everything goes well, we're gonna see two things. We're gonna see two log statements down here One that says submitted Bob 55 and then patient entry added successfully And we will see a toast down at the bottom of the screen here. That says patient entry added There it is real tiny. You notice there's a delay All right, let's let me clear this out Watch this again. Let's submit Joe. He's 22 There's a delay here, right? So when I click submit immediately, it'll say submitted name blah blah blah down here But this success listener doesn't fire until all those asynchronous calls happen The network connection is open the data is transmitted over the intertubes It's stored in the firestore and firestore sends a successful response, right? So one log message is gonna show up immediately and then the toast and the second log message will show up slightly Later, you know a second or two. Let's watch again That time they were really fast. Okay, so but nonetheless, there was still a little bit of a delay Alright, so we got data in the firestore next video Actually, you know what before we continue. I Forgot this stuff can fail. So let's add a failure listener here Failure listeners look a lot like success listeners So I'm chaining again, right? I'm dot add on success listener after that method. I'm gonna call add a failure listener and New on failure listener. I'm gonna make another anonymous inner class That is a failure listener. What do we want to do? Let's just tweak what we did for the success instead of the failure. Let's log a warning Patient could not add so This log level is at the debug level warnings are a higher level of priority You can use that to your advantage when you're filtering things out Could not add patient And then what do we want to say to the user? Something okay, you should handle both of these gracefully in a way, right? So That's it for now next video. We'll talk about Forgot my semicolon there again next video We'll talk about getting the data out of the firestore now that it's up there But this is a an example just using this ad mdb.collection.add and Gets it up there. Now. I have this tied to a submit button, right? I'm in on submit You can tie this to any event you want The user tapping on a card a value changing in an edit box You can attach this to anything, right? The key is in this mdb.collection.add We define a success listener. We define a failure listener and Importantly we defined a patient object Which is going to be the model for the information we want to put up there, okay next time We'll talk about getting the data out of the firestore