Well, welcome back.
In the last week, we inserted some data to our SQL database, and now it's time to retrieve that data
and we're going to use it as an observer.
And in order to use an observer, we need to go to our fastest Dow.
And here I'm going to create a new function that will allow me to get all dishes, because this function
here is there to insert a dish.
But now let's get all the dishes.
And in order to get all the dishes, we are going to use a query.
And here's why.
It's important that you have a basic understanding of sequel.
OK, so I'm going to add this query here.
And query is select star from first dish table ordered by ID.
So well that will do is it will select all elements that are inside of the table called Favorite Dishes
Table and it will order it by its ID.
And in our case the idea is basically just going to be the order in which it was created.
OK, the dish was added to our table and I'm going to call this function, get all dishes a list and
it will be of type flow list five dish or that's what it will return basically.
So we are going to use Ferlo.
Now, the idea is that when data changes, do you really want to take some action, such as displaying
the updated data in the UI?
And this means you have to observe the data.
So when it changes, you can react.
And now to observe data changes, we will use flow from the cutline xcode routine's.
So this is from Cutline X called Routine's.
So if you want to use this flow type, then you need to define that keyword here.
In the return value of the method, description and room generates all necessary code to update the
flow when the data is updated.
OK, so that's the power of using flow.
A flow is an async sequence of values.
So flow produces values one at a time instead of all at once.
And that can generate values from async operations like network request, database calls or other async
code.
And it supports core routines throughout its API so you can transform a flow using all routine's as
well.
OK, so now let's use this in action, and therefore I'm going to go to my repository fetish repository
and here we also need to set up a new entry, which will be all dishes list.
Now, this is of type flow and it's a list of our fetish.
Now you need to import flow for this to work.
So hover overflow.
And we're going to use Coplin Xcode Routine's flow.
OK, and this will be set directly as five dishes.
Daou that get all dishes list.
OK, so we'll call this query that we have prepared here.
So you can already see here at our insert, we didn't even have to define a query, it knew directly
what to do because it knew that it needed to insert fetish or our fetish object type, which contains
all of these values, and we even defined in which entity we want to distort.
So in which table we wanted to store that data so that it basically knows insert fetish into the table.
Okay, so we didn't need to define that ourselves.
And for the query where we want to get all of the data, well, that's where we need to use equal.
But now we can just go ahead and use this function, get all dishes list in order to get all of the
dishes.
OK, so all this year's list will call this faddish method, get all this list.
Now we can use this variable inside of our model.
OK, so inside of our fetish view model.
So here I'm going to use the life data.
OK, so life data is a very powerful tool as well.
It comes with a model, so it's very useful in combination with few models.
So here, as you see, we are creating a new value when you variable of type life data, which internally
uses a list of fiftyish and it will call repository, all dishes list as life data, even though this
is not a method, but it will use it as live data.
So using live data and caching with all the returns has several benefits, we can put an observer on
the data instead of calling for changes and only update the UI when the data actually changes.
The powerful part here is that repository is completely separated from the UI through the View model.
So this makes our application in general way more efficient.
And that's really the idea of using this whole concept.
Right?
It's on one hand, of course, that you have a clean way of building an architecture around it and it's
easy to adjust in the future if you need to make updates to your project and at the same time, it makes
your application general significantly more efficient.
OK, now inside of our all dishes fragment, that's the point where we want to display this thing,
right?
So all dishes fragment.
This was the fragment that is displayed here, this home fragment.
That's the point where I now want to display the data, of course.
But before we display the data, we are going to, first of all, just show it in our lolcat.
OK, so the whole UI setup is what we're going to do later.
So here we need to set up our view model so that we can use it inside of our all this fragment.
I guess I'm going to create a private vol and fav dish.
You model this one here, which will be off type fiftyish view model, so that's the model we created
to use the by keyword using view models here once again, as we've done in the last video.
And here, I'm not going to define what kind of.
View models that will be important.
I will use my view models, factory fiftyish model factory here.
So this view model factory needs to have a repository.
And where do we get that repository from?
Well, we need to use require activity dot application.
As Favish application.
And then we can use the repository from it.
OK, so this will be our five day review model that we now can use in our application.
So where I'm going to call it is going to be in on view created.
OK, so here I'm going to override this function on view created, which will be called once the view
is fully created.
And I'm just going to go ahead and use my view model.
So here I am, five dish view model to call all dishes list and observe it.
So here we are adding an observer which will observe the view lifecycle owner or we need to pass your
lifecycle owner.
So this is now an observer on our life data returned by to get all dishes list and the unchanged method
fires when the observer data changes and activity is in the foreground.
So this basically means is if the data changes, the observer will be aware of that and it will make
changes to the UI so it will adjust the UI, so to speak.
So as you see, we get a list of favorite dishes so it will pass the dishes to us.
OK, and now we need to make sure that the dishes aren't empty.
And if they aren't, and then we're going to run a Falu item, then it.
Which will be our.
Log entry, so I'm just going to for now use the log entry for my dish title.
So what is this EITE and what is this item?
Well, an item is part of the it.
What is it?
Well, you can see it here, Ingrey.
It says it's a list of favorite dishes.
So now every single item inside of that list, all in it is going to be a favorite dish.
So now what we can do is we can log it.
For example, I'm going to call this little dish title and I'm going to use the item thought ID and
the item title to basically display whatever we have entered.
OK, so for now, we're not going to display in the UI itself, but we're just going to use the Lochhead
for it.
And at this point we can run our application and see what's going to happen.
So we can see here the observer is going to take care of everything for us at this point.
And now let's go ahead.
And actually we have created an entry already.
We don't need to create a new entry.
Let's go over to our Lockette and search for the dish.
Yeah.
And move to Dashboard back home because we needed to trigger this event where this on view created is
called, and you could see here it is in fact called for us.
OK, so here it says this title is one Homer Pie.
OK, that was the one that we created in the last video.
And one is going to be the ID.
So we have one entry so far inside of our dishes.
Now let's try it again.
Let's create a new entry.
And let's use Holmer once again.
This one will be.
New meal, OK, at the tip, well, let's select something, dinner chicken.
One, two, three.
As the ingredients, cooking time, something and cooking directions will also be one.
It doesn't really matter.
What's interesting for us is that now we see we have homemade pie as well as new meal.
Okay, so our entries are there.
They are in the database and our home fragment is aware of it.
And now we just need to adjust the home fragments so that it displays the meals.
All right.
So that's the idea of it.
That's what we're going to set up in the next video.
So coming back to this image here, you can see we're using the view model, which contains the life
data and the UI is notified of changes using observation.
So now our UI controller is, in fact, observing.
And where is it observing?
Well, it's observing right here where we added the observer.
So in on you created we are observing and we're just saying, OK, if there are any changes, please
let me know.
And well, in our case, just use this for loop for now.
OK, so basically that is the whole flow that we have now.
So now we just need to take care of the UI controller to actually display the data and well, forward
on UI events.
OK, so now we have this complete flow into procreated.
We have the room database with the Dow as well as the entity we are using the repository which is communicated
with via the View model, and then it communicates to the door to room database.
OK, so that's how everything is connected and that's how we have build up the architecture.
So now we can actually work with it and very easily make changes to our application.
OK, so let's go ahead and take care of the you want to see in the next video.