All right, um, let's see one functionality, how our chat is going to work.
So right now we have a local database that we are reading these messages from.
We are not abrogating the local database as yet, because whenever there is a new message from the other
side, we're not updating so we can see it.
And also, as the things are now, there is an issue.
Let's say our user has these messages in a local database and also we have the same messages in our
firebase.
All right.
We have one less here.
But the idea is anyway, the same.
You have, uh, local and cloud database.
So what if our user decides to change the device or lost the device, will replace it with a new one.
Doesn't matter.
If the user logs in on a different device, the local database will be empty.
We can simulate these by deleting the application.
Which will delete the local database as well.
And now if I come and let's stop it and we want to already run our application.
And of course, we need to login because this is just a fresh install application.
Imagine this is a brand new phone for our application, at least.
Let's log-in.
So this takes us to our application and it, of course, gets the information from our fireplace that
we have a recent chat with these any kids, but if I open up, there is no charge here.
So what happened to our local database?
Why we don't get anything from our fireplace to show it.
And the idea is, of course, every time we open our chat with you in case if our local database is
empty, we want to check if there are any old chats on a cloud so we can download them and put them
in our local database.
Maybe we just start a new chat with this person.
We never chat with them before.
And of course, the local database will be empty.
But in any case, we will just want to check if there is some chats on a cloud as well in this case,
so that in case if there are chats, we want to get them and save them locally.
So our database will be populated and will be in synchronization with the cloud firebase.
So what we want to do here is let's go to our Lord chat function here.
And underneath, I want to create some functions that are going to help us achieve these sorts insert
messages who are going to keep some space.
Let's actually put the mark in certain messages.
So this will help us to separate them.
And now we're going to write some private funds.
The first one is going to be Lisson.
Four new charts and we'll get back to this a bit later, but now we want to concentrate on another function
called.
Check for Old Chat's.
All right, so let's say preened.
Check for old chats.
And when do we want to check for old charts, ease once we load everything from our database and encase
our once we access this all local messages in case if there are no messages in database, so the count
of these messages will be zero or the array will be empty.
We want to just in case to check if there are any old messages.
So I will say if all local messages is empty.
And then we want to say check.
For all the shots and let's run now, they should print because we have no local chats, but we have
chats on our firebase.
Open this up, empty everything, and let's try not to check for old charts, because our fireplace
has charts and our local database is empty.
So what we want to do when we are checking for old charts, we want to go to our fireplace.
And we want to get all the charts for the specific chatroom and then we want to download them and save
them in our database.
So let's start from the first step, getting all these charts from our fireplace, how we are going
to do that.
We have these fireplace message listener class.
And we are going to use that, so let's create another function in our Firebrace message listener to
check for old charts for us.
OK, so I'm going to do it at the top here.
I will say funk.
Check for old charts and this function will take a chat room maybe.
Let's take a look and.
I bet he's a string and also.
It will take a collection idea, which is a string as well.
So to understand what's going on here, why we have a document and collection idea is if we go to our
messages here, the structure so we access our messages, then these document I.D., we save each user
has his own chat rooms.
So first we want to check the specific user, which is our user, and then we want to check the chatroom
I.D. So we want to get only these messages that belong to our chatroom.
So this is our document and this is our collection ID.
So we pass the information.
So it's the local user I.D. and the chatroom I.D. We have these two, uh, parameters and then we can
get all the charts belonging to these two parameters.
So here I will say fireplace reference and we want to access our messages, so this way we have access
to the first level here and we jump to the second level.
What we want to select here is whatever belongs to our user, which is the document ID, we say document
and the path.
We just pass our document IP.
And then I will say that I want to also access the collection with the collection ID and we want to
get all the documents that are in this collection.
So I will say get.
Documents.
With our completion and just hit enter, give it a name of Quarrie Snapshot.
And error.
So we want to check if we have some documents from our Querrey snapshot, so we will safeguard led documents.
Equals query snapshot documents.
Else, we want to say preened.
And let's say no documents for old chads, so there is no document.
Locally, we have not messages and on Firebase we have no messages.
And then I want to just return.
If, however, we have some kind of chads in our database, what we want to do is to create local message
from each chad and to show it to our user by actually, no, we want to save them to our real and then
the database will be updated and our user can see them.
So we grab everything to convert it to local message and we save it to our real.
So we say, why are old chads?
All messages actually will be close to document and we say compact map.
And let's give it a name of Querrey.
Document, snapshot.
Not shot, and this is going to be a type of local message.
And I will say a return.
And let's try to convert this query, document, snapshot.
Into a data as which throws and we will say a local message thought self, and this will try to do that
and convert it to an array of our old messages, which will be a local message once we have these old
messages arrive.
We want to sort them by date, so we say old messages.
Oh, my Sergie.
Yes, those sort.
For some reason, it stops.
After completing by and we want to say bye, let's put our curly braces and let's get the date of first
item.
Which is they should be smaller than the date of the second item, so dollar sign one third date, and
this will sort our array in the place.
And once we have sorted array, we want to go through it and save each item in the real.
So I will say for message in old messages, we want to say a real manager shared.
Save to on our message.
All right, this is it, we have the function, so let's call this function.
And we're going to call this function in our.
Let's go back to our chat with controller, listen for old charts, and here I'm going to.
Remove my print, we say Firebase.
Message listener shared listen.
Well, it's common to complete these.
At least for.
Old.
Actually, no, it's check for old charts, check for old charts, document it is going to be our user
current ID.
And collection I.D. is going to be our chat lady, so this way we are going to get all the local all
the old messages and save them locally in our database.
So shall we try?
To see how it works.
I still eat everything up our chat, and you see that everything appears now because we have downloaded
and saved everything locally.
Now every time I open my messages are there.
So it is very cool.
Now, every time our user changes the device, we will get these messages back and you can see the order
is also saved correctly.
So our checking for all chats are working fine, so what we want to do is also to listen if there are
any new chats, because if the other user sends us a message, we are not going to get it.
So we want to listen.
If there are new chants, we want to take it and save it locally as well, so that our chat, you can
show it to us.