Welcome back.
Do you still recall why we set up this room demo, I guess.
You hopefully did so.
The thing is we have worked through this room demo and you've seen it's has been quite a bit right.
It's quite a long, um well, chapter, you could almost say, even though it's still part of the seven
minute workout chapter.
But realistically speaking, it should have been an own chapter because if we look at it like we want
to use the same approach more or less as we have had in our room demo for this history screen, even
though it's a lot simpler, right?
We're not going to added any entries that we have in our history.
We're not going to update or delete them or anything.
So the only thing that we are going to actually do is, first of all, insert them and then retrieve
them.
But we can learn from our room demo that we had set up, OK, so we will need an entity, then we will
need a DAO.
Like this one this and bringing all that we had, we will need a database and then we will need to have
an adapter, which will not be an item.
Adapter will be a different adapter, I believe.
Well, it would be like a history adapter or history entry adapter or something like that.
And then we can go from there.
I know that this is quite tough, but if you really want to challenge yourself and really learn how
to use the room database, I would recommend that you try to apply as much as you can from the room
demo that we have seen in the last couple of videos.
It was seven videos I believe tried to set up the same approach step by step two, then display entries
of your last couple of exercises that you did.
Because if you look at it, we do have this activity here.
This main activity, which then jumps you over, let's look at it to our exercise activity in what's
with done with the exercise activity, we jump over to the finish activity.
So once done with exercise activity, you could say, OK, let's now make the entry into our database.
So that would be a good spot to do it and then retrieve it.
Inside of our history activity, because that's where we will then need to retrieve the data entries.
OK, so it will be tough, as I said, but if you feel like a real challenge, try it.
If you don't feel like a real challenge, you can just follow along.
But at one point, you will need to take this challenge.
So at one point, when you start building your own applications in the future, you will have to go
through this.
And why not now?
OK.
And also feel free to share your experience of going through this in the comments down below.
OK, so I'm going to show you once again how it's done, but I'm going to do it less in-depth, as I
have done in the demo.
So if you feel like it's too tough at some points, check out the demo once again before going to the
actual seven minute workout.
OK, because the demo was like, I have way more advanced and complete approach, right?
We had everything that we needed with all four major operations, right?
We had the crew operations.
So create, read, update and delete, create, read, update, delete.
If you ever hear those keywords, it's basically those values or those methods that we need to set up.
OK.
So I'd say, let's get started.
OK, so what do we start with, what we do have our exercise model, so we know how every given exercise
has to look like now we will need to have our history entity.
OK, so now let's make sure that we set up the history entity as we have seen, which is generally the
approach that you need to have the entity, then you can set up the doubt and you can set up the database
and then you can go ahead and set up the app, as we have done in our room demo.
So here.
The Dow, while entity first Dow done database, then app, and then you can set up the recycler view
in which you want to display the data, meaning the adapter for it.
And yeah, that's pretty much it.
So let's get started.
The first thing will be our rather simple entity.
So let me go ahead and create a new entity here, which will be a Cochrane class, and I'm going to
use a data class here.
Which I'm going to call his story and to tee.
OK, so this one has to have the ET entity keyword assigned to it.
So why does it not appear here?
Well, the thing is we of course, need to go to our Gradle and make the changes there, so we need
to add the dependencies in order to use.
Our good old room.
So here we need to add our Scotland Captain Scotland kept, which stands for Scotland annotation plug
in.
And then we need to make sure that we add the room dependencies.
So in here pretty much do the same thing as we have done in our room demo.
So let's go over to the room demo, if you recall.
Here we can just go to our cradle and copy the dependencies from there.
So the room version and call routine support we want to have as well.
So let's add all of those lines here into our existing project here.
OK, into our.
7-Minute Workout Project.
And then let's sync it.
Sink it in.
OK, now that it's sink, we can use Antigene, so we need to import anti-Dean.
All right, Andrew.
Room antigene and we can take care of our history antigene.
And I'm going to give it a table name here.
Table name, which will be history table.
OK, so this will be our exercise history.
And then the data class will only have one primary key and it will not be named.
But this time, and we also need to import primary key.
This time it will really just be a date because we don't need to store anything else.
Just a date is going to be enough.
So you could, of course, go ahead and add IDs and have the date as a separate entry, but can also
just simply have the date as a primary key because the date is going to be so unique, it's not just
going to be the date of the week or month or whatever, it's going to be specific to a millisecond.
So in the same millisecond, it's highly unlikely that we will have the same entry so we can just use
it as the primary key because in the same milliseconds to our application, it's not even going to be
fast enough to store multiple pieces of information.
OK, so now let's set up the Dow and the Dow for now really just needs to insert data.
So let's go ahead and create a new Dow file.
Which will be called while have an interface, first of all, and I'm going to call this one his story
doll like so.
OK, and this will be a done deal, so we need to add the key keyword here at Dow, we need to import
Danielle from room and we can then.
Use our insert key word from room again.
Import debt and use our suspend function, which will be answered function.
And what do we need to insert while our history entity, which would just prepared, right?
So this is the same approach as we had with our room demo, if you recall.
So here we had set up the entity and then we have set up the DOL where we added the annotation DAO and
then to insert method with the entity that we have created.
OK, this extra entity?
All right.
So now that we have the history DAO, let's go ahead and set up our database because in our database,
we can now use our DAO.
So let's go ahead and create a new Kotlin file class.
This one will be an abstract class.
I think we don't have that here.
So let's just use a default class here, which I'm going to call history database like so OK.
And this history database will be an abstract class.
Which inherits from the room database Glass, so here room data.
I think it's with a Capital D data base, so we need to import room database for this.
Let's do that.
You see, it's in the room.
And we need to add the database and annotation, so let's check out our database example.
So here we had to improve your database.
Now we need to add this add database annotation with the entity, class name and the version.
OK, so let me do that as well here, which will not use the employee entity, but our history entity.
OK.
And now we need to create a history Dow object.
So let's also just use the same things that we've done here.
We can really just copy this entire thing from our employee database, from the room demo and just make
a couple of changes.
So this is not going to be the employee Dow, but the history Dow, which uses, of course, the history
Dow class.
As its basic basis, then this will be the history database that we create an instance of.
So here we just need to replace employee database with history database.
And of course, he had to name this is a string.
This is very tricky when working with strings.
Always be careful, right?
And then the name, of course, will be history database and not the other one that we had in our previous
example.
So again, we have this companion object with our volatile instance of the history database, which
is at the beginning, a null entry.
So it's empty.
And then we have this get instance method, which will check whether we do have an instance already.
If we do, then just use it otherwise created.
OK, so he just says, OK, whatever the instance was, if it already exists, if it's not null, which
had at the beginning, of course, will be.
But once we get the instance, for example, a second time, then the instance already exists.
So then it will just say, OK, this local instance is going to be the existing instance.
And then we check, OK, is the instance existing or is it null?
If it's null, which will, it will be only at the first run or at the first time that we get the instance,
then create an instance and set it as the global variable.
Otherwise, make sure that you just use the old instance that you had and return it.
OK.
So basically, if it exists, don't do anything.
If it doesn't exist, create it.
So that's what we're doing here with this history database.
So we have done A. With the Dow, we have to enter the database, so now let's go ahead and create the
the app.
OK, and I'm going to call this one workout app.
So another class, which will be a normal class this time, work out and call it workout app.
I'm not sure where the workout is.
One word or probably it's one word, but it's fine as well.
So here this one will inherit from application.
OK.
And now we need to set up the database.
So let's look at our room database example.
And here, let's look at the employee app we see we just created a database of lazily.
So let's do the same thing, but with the history database this time.
All right, now, we have to work out.
And we need to make sure that in our manifest, we set it up as well.
So here for the application, we need to end the Android name and it will be the Dot Workout app, which
is this class that we just created.
And so this class workout app.
OK, so now we just need to make sure that we are storing the exercise that we have finished, and I
think it's going to be best to start the exercise workout in the finish activity because we know once
we are in the finish activity, we have gone through the exercise.
So let's create a method which will take care of adding data or our date to the database.
So here a private function, add to data or to add date.
It will be will be better here to data base.
And let's look at our the room database example once again.
OK, because we had stored it, I think in the main activity, right, we took care of everything there.
So update records add record.
OK, so this was the part you see, we need to pass in the employee Dow.
And this is where we used our call routine in order to then use the insert method using that employee
dial with the employee entity.
So now we need to do the same thing with the history Dow insert and I think its exercise entity or whatever
we call it, with our history entity.
So let's use the history object here.
And then.
We will need to know what we want to store.
OK, so before we can do the life cycle stuff, so life cycle launch, life cycle, scope launch before
we do all of that cold routine stuff and we need to import that, by the way, as well.
This launch, as well as the lifecycle scope which has been added for me automatically the launch and
lifecycle scope are now added as namespaces up there.
And here I want to use my history down to insert, and I want to insert my mystery entity with a date,
which is a string, so now I could do something like test string or something, but it would be better
if it's actually a date.
And in the right format.
So in order to set up the date.
So for now, it will be an empty string.
We need to, of course, prepare to date up here.
So the general structure for a date, if you want to get that would of course, to be, first of all,
to create a calendar instance.
So calendar get instance.
So see will be my calendar.
Of course, you could call it my calendar or so and then get the date time object, which will be simply
see that time.
So time gives me a while.
If you can look at it, it will give me a date object.
So get time will give me a date object, which is this see time will use to get time method of the calendar
class.
And now we can, of course, lock that.
So here you could go ahead and say the date will be and then the message would be an empty string plus
dates time.
OK, let's import log.
This will give us the date that we want the storm, and now we have to set up the date for autumn.
And there is this simple date format class, so let's create it as the we've done this in the past,
I think when we created our calculator like how old you are, calculator?
Think you're right?
So here we need to use the simple data format.
I'm going to use to Android One.
And here you need to define the pattern.
So I'm going to use the pattern of day, day then month of month, which means the month in the sense
of Gen Fab Ma and so forth.
And then two year and four digits.
So year you year the hours, the minutes and seconds.
And I'm going to use the local default settings, so whatever is the default of the phone settings,
so get default.
So if the phone is said to American ours, then we'll use the American ours and so forth.
OK, so this is going to be our simple date format and and we check what the problem here is.
And it seems like I shouldn't have done or shouldn't have used the.
One from Android and the check.
I need to have the right namespace here, which is import Java, text the simple data format.
Okay, so that's where the simple data format is stored.
So it's not going to be this Android One that I have used earlier.
It's actually going to be the Java one.
Oh, it's cool Java stuff.
OK.
So now we can use the simple data format to get the date from the date object into a usable.
The string formats that we can use.
OK, so let's go ahead and create a data object, which will be our while use our simple data format
to format the date time that we have earlier created.
So here we get this data object.
We use this simple date format to make a string out of this date format.
And then, yeah, make a date out of it, which will be a string.
So now we can use this date, we can log it using this entry here.
So first we get the date, then we formed the date with our own pattern.
So here you defined your pattern and then we can now use this date here.
So let's past a date instead of an empty string.
OK, so this will add today to our database.
This line here.
All right.
So we'll call this insert method.
I'm going to lock that as well.
So here it will use this insert suspend function, which is a quote routine function, and then we're
going to log it.
So when do we want to call this add to database function?
Well, we want to do it in the create method right inside of this on create here at the top.
Unfortunately, however, we cannot just call the method without doing any preparation.
So the preparation that we need to do is whenever you want to call a double is to set up a database.
So let's set up the dial here.
Val Dow.
And let's check it out how we've done it in the room demo.
So here you can hold the control key and click on Add Record and then we'll show you when it was used.
So it was used here at the top where we said add record and we needed to pass the employee down to this
and record method, as you recall, because, well, that's what we had here as the parameter.
So now we need to set up the Dow.
How did we do it?
We did it like this.
OK, so let's copy this line and then paste it in here to make the changes that we need to make.
So this one will instead just be a history doll.
Or you could just also call it out if you want to.
Either way is fine.
It will use our history app.
And.
Doesn't find it here.
I think we call it workout app.
Yes.
So it wasn't just this rehab, it was the workout app.
And then inside of workout app, we had this TV.
OK, so this database and now we can call the history down, which will be this abstract function inside
of our history database.
OK.
So now that we have the Dow, we can call our add two database method with that Dow, and that will
basically do what we need to do.
So this will store the entry.
So in order to test this, what I'm going to do is I'm going to go to my.
Exercise activity and make sure that exercise timers, rest timer is one second exercise timer is one
second because otherwise it would take forever for me to test this.
So let's run this real quick and we will not sit on the user interface because that's something we will
have to set up in the next video.
But you see, we have set up everything so far to insert the data.
So we will also, of course, need to set up how to retrieve the data.
And so let's go ahead and start the exercise and it will just jump through it.
I'm going to cut the video here until it's done and while actually we can set up our lock until then.
So there is a lot going on in the Logcat, but what's interesting to us are going to be things like
date.
So we can directly search for the date keyword.
OK.
And we're going to search for errors, which we called it here.
So there are no date errors or not many date errors.
OK, now it's done, actually.
You see it says date, which is this thing here, does log entry with the date time object.
So it's free.
Oct. 15 10 23.
Thirty six GMT plus two.
Well, whatever it tells me, all of this information even telling me which time zone I'm in in the
year of 2021.
So now if I take the former to date, it just says 15 Oct of 20, 21, 10, 23 36.
So you see, it gives me this same format that I have defined here in the simple date format.
So day day 15 and then and which stands for October, like OCD or whatever month, it is the first three
letters of that month.
Then the year that you can see here and then hours, minutes and seconds.
So this h h will give me the hours, I think, in a 24 hour format, which is something I really love
as a German.
But if you are from a different country, maybe you like this and stuff.
So it really is something you can play around with here in the date format.
OK, and then we have the former to date.
Which we don't see yet, as it seems former to date.
Oh yeah, that was the former to date.
I'm sorry.
Yes, because that's where we used it.
And then that's where we locked the date that we added.
So now we should have it in the database.
How do we find this out that you recall?
Well, that's a cool thing.
The idea gives us this app inspection here.
And.
After a while, it takes the process.
And then after a while, it will display the database inspector at the history table so you can click
on the history table.
You see it only stores to date.
And once you click on it, I had to wait a little bit and double clicked at once again, so just to
make sure that it displays it and you see it created this entry of 15th of October 2021 at 10:23.
OK, so we have the entry in our database now that we have finished the exercise or the workout.
And now let's go ahead to the next video where we're going to retrieve the data and displayed.
OK.
So I hope you managed parts of it yourself.
Or at least you now know that it's always the same process, right?
It's always the same structure that you need to go through the same steps that you need to go through
in order to store something in the database.
So I know that it's still tough, but once you've done it a couple of times, it will become a lot more
natural and you will just know how to do it.
And even if you don't know how to do it from scratch or without any assistance, you can always just
open up the demo that we have set up and use the same approach that we have used there.
All right, so see you in the next video.