WEBVTT

00:00.080 --> 00:03.080
Congratulations for this big success.

00:03.240 --> 00:13.800
We succeeded in inserting San Francisco marker inside Marker's collection in Firebase Firestore database.

00:14.080 --> 00:16.640
Now we need to fetch the markers.

00:16.640 --> 00:21.120
We need to get all markers in the database.

00:21.320 --> 00:25.600
So for this we need to create a new function.

00:25.600 --> 00:28.120
Let's go to the repository.

00:28.120 --> 00:34.680
So whenever you have any new function you need to create it in repository.

00:34.720 --> 00:43.800
As said before the interface marker repository defines what you can do add marker, get markers, and

00:43.800 --> 00:44.440
so on.

00:44.640 --> 00:50.920
Inside marker repository implementation we define how you do it.

00:50.920 --> 01:02.160
So the first step here is to create and use suspend function called get all Markers from Firestore.

01:02.200 --> 01:05.960
This function returns a list of.

01:06.320 --> 01:12.240
We can create lat long to make it simple to display latitude and longitude.

01:12.440 --> 01:16.360
But I prefer to make it as customized.

01:16.360 --> 01:25.840
So we're going to create a new class a new data class that it is a customized and display latitude longitude

01:26.000 --> 01:28.440
plus title plus document ID.

01:28.680 --> 01:32.800
So we can add more variables to the marker.

01:32.840 --> 01:42.200
Since we upload inside the add marker, the latitude and the name latitude and longitude and the name.

01:42.200 --> 01:48.840
So we have three things marker latitude, marker longitude, and marker name.

01:48.840 --> 01:57.200
If we specify lat long like this, we only get two parameters the latitude and longitude okay.

01:57.400 --> 02:05.990
So we don't need like this We need to get three parameters the ID or the title latitude and longitude.

02:06.030 --> 02:06.510
Okay.

02:06.670 --> 02:12.390
For this let's go to create here Inside Utilities package.

02:12.470 --> 02:14.750
Create a new Kotlin class.

02:14.950 --> 02:18.350
Name it as destination marker.

02:18.390 --> 02:20.750
We can make it as data class.

02:20.910 --> 02:34.590
So data class and define the parameters val id it's of type string val latitude double val longitude

02:34.750 --> 02:37.310
double val title.

02:37.350 --> 02:39.190
It's of type string.

02:39.310 --> 02:45.910
Also we can add a function to convert to lat long if needed.

02:46.190 --> 02:48.150
So let long.

02:48.190 --> 02:53.470
And here put latitude and longitude as parameters okay.

02:53.630 --> 03:00.110
Import class lat long from Google Maps Okay, so remove this.

03:00.150 --> 03:05.070
I always make mistakes here and import from Google Maps model.

03:05.070 --> 03:05.750
Lat long.

03:05.790 --> 03:07.670
Okay, this is if needed.

03:07.670 --> 03:10.590
This is optional to convert it to lat long.

03:10.630 --> 03:13.830
Okay back to the marker repository.

03:13.830 --> 03:18.110
And here it's a list of destination marker.

03:18.150 --> 03:21.990
Now let's move to the marker repository implementation.

03:21.990 --> 03:24.630
And let's implement the method.

03:24.630 --> 03:27.630
Get all markers from Firestore.

03:27.910 --> 03:28.350
Here.

03:28.350 --> 03:38.510
Inside the implementation we need to define how to do how to get all markers from Firestore.

03:38.630 --> 03:49.150
So for this we need to get val result equals to Firestore dot collection markers.

03:49.150 --> 03:57.460
Firebase which is the instance we passed as a parameter that collection markers Dot.

03:57.500 --> 03:58.260
Get.

03:58.300 --> 03:59.020
Dot.

03:59.220 --> 03:59.980
Await.

03:59.980 --> 04:01.940
This is a suspend function.

04:02.060 --> 04:04.980
Get all markers from Fhir store.

04:05.180 --> 04:08.140
Reads all marker documents from Firestore.

04:08.140 --> 04:14.300
Markers collection maps them to a list of destination markers object.

04:14.300 --> 04:21.780
So the returning type of this function is a list of destination markers and the result here.

04:21.940 --> 04:27.220
Access the markers collection in your Firebase Firestore database.

04:27.420 --> 04:30.060
Dot get tells Firestore.

04:30.100 --> 04:32.700
Get all documents in this collection.

04:32.940 --> 04:37.900
Await is a function used to return a task.

04:37.900 --> 04:45.420
And since Firestore returns a task which is asynchronous, await suspends the coroutine until the data

04:45.420 --> 04:46.340
is loaded.

04:46.540 --> 04:52.460
This makes the code look asynchronous and safe to call in Co-routines.

04:52.500 --> 04:58.380
After this line result holds a query snapshot containing all the documents.

04:58.380 --> 05:06.820
So if I hover here, result is a query snapshot containing all the your documents.

05:06.860 --> 05:15.660
Now we need to get every document inside our Firebase Firestore collection and map every document to

05:15.740 --> 05:19.100
a destination marker instance.

05:19.220 --> 05:24.980
For this reuse return result dot map.

05:24.980 --> 05:35.060
Here I can use the map function in order to map every document to a destination marker for every document

05:35.060 --> 05:36.300
document.

05:36.340 --> 05:45.940
Go and create a destination marker and set for the parameters of this destination marker.

05:46.260 --> 05:48.660
The document data.

05:48.700 --> 05:49.940
Let me show you guys.

05:50.100 --> 05:52.500
At first we need to specify the ID.

05:52.540 --> 05:54.460
I'll use document query.

05:54.500 --> 05:57.810
Document snapshot.id latitude.

05:58.050 --> 06:04.930
It's document dot get double and the field is latitude.

06:04.930 --> 06:06.810
Please pay attention here.

06:06.970 --> 06:11.010
Here we have latitude field and longitude.

06:11.050 --> 06:12.610
This is case sensitive.

06:12.610 --> 06:16.890
So please write them exactly as your database.

06:17.010 --> 06:21.490
If it is not found go and put it 0.0.

06:21.610 --> 06:24.890
Longitude equals to document dot.

06:24.930 --> 06:29.930
Get double the field longitude 0.0.

06:29.970 --> 06:36.490
The last thing we need to get is document dot get string and the title.

06:36.610 --> 06:42.690
If it is null and not found, go and create an empty string.

06:42.730 --> 06:50.450
In the next video, we're gonna modify our view model and test our code to get the markers in the collection.
