WEBVTT

00:00.080 --> 00:01.120
Welcome back.

00:01.160 --> 00:09.040
After we created this directions module, let's move to the map view model and inject the constructor

00:09.040 --> 00:10.840
with this repository.

00:10.840 --> 00:19.640
So here private val directions repository and import the directions repository.

00:19.640 --> 00:25.080
Scroll down in order to create the functions for the directions package.

00:25.280 --> 00:35.880
So here directions API we need to fetch the route to marker and the multiple route to markers okay.

00:36.080 --> 00:45.800
So first of all private Val route which is of type mutable state flow list of lat long and the route

00:45.840 --> 00:46.600
points.

00:46.760 --> 00:49.400
So let me name it route points.

00:50.040 --> 00:52.000
This route points.

00:52.000 --> 00:57.560
Here we are creating an observable data stream for tracking route points.

00:57.760 --> 01:04.330
This is very important because we need to keep tracking for the root points.

01:04.330 --> 01:07.410
Why we are doing this pattern here.

01:07.450 --> 01:09.050
Underscore root points.

01:09.050 --> 01:10.250
Private banking.

01:10.250 --> 01:15.730
Property conversion, a convention with underscore and mutable state.

01:15.730 --> 01:24.330
Flow a mutable container that holds a state and emits updates to collectors returning a or of type list.

01:24.370 --> 01:31.690
Lat long type represents nullable list of geographic coordinates initialized with null.

01:31.850 --> 01:43.290
So this is a public read only state flow exposes read only version to external components as state flow,

01:43.290 --> 01:49.970
and as state flow function converts mutable flow to immutable.

01:49.970 --> 01:55.410
Consumers can observe changes but can't modify directly.

01:55.410 --> 01:58.010
So this is a very important note.

01:58.170 --> 02:05.690
And it's commonly used this this pattern commonly used in many applications, and we talked about it

02:05.690 --> 02:13.610
in the previous videos so consumers can observe changes but can't modify directly.

02:13.610 --> 02:24.810
Then create a new function, name it as fetch to or from current position to marker.

02:25.010 --> 02:29.130
Taking a marker as a lat long parameter.

02:29.130 --> 02:39.370
And inside this function, we need to use the ViewModel scope in order to call the directions dot get

02:39.530 --> 02:40.210
route.

02:40.250 --> 02:46.330
If you remember from the directions repository implementation, we have two functions.

02:46.370 --> 02:50.330
Get route for multiple markers and get route.

02:50.450 --> 02:58.690
Here we inside this get route, we are fetching the data from the origin to the destination.

02:58.690 --> 03:06.620
The origin would be the current position of the user and the destination is the selected marker.

03:06.780 --> 03:10.020
So here inside this function.

03:10.300 --> 03:14.420
Let's start with ViewModel scoop dot launch.

03:14.460 --> 03:23.220
Also, if you remember, we made those functions as suspend functions inside this directions repository

03:23.220 --> 03:24.180
implementation.

03:24.340 --> 03:28.860
So for this purpose we need to call them from the ViewModel scoop.

03:28.900 --> 03:38.940
This ViewModel scoop start with location repository dot user location dot value.

03:38.940 --> 03:43.140
And here we are executing the let block.

03:43.340 --> 03:53.220
If the location repository dot user location not equals to the not equals to the uh to null, then we

03:53.220 --> 03:55.860
need to execute this block.

03:56.100 --> 03:57.620
What we need to create.

03:57.660 --> 04:08.540
We need to set the current location as a parameter of this lambda expression and go and create val root

04:08.580 --> 04:11.380
equals to directions.

04:11.780 --> 04:16.740
Data or directions repository dot get root.

04:16.820 --> 04:21.260
Inside this function pass the origin and destination.

04:21.260 --> 04:22.580
What is the origin?

04:22.860 --> 04:25.980
The current location and what is the destination?

04:26.260 --> 04:30.660
The marker that is passed as a parameter for this function.

04:30.660 --> 04:37.540
Then let me check if this root doesn't equal to null.

04:37.700 --> 04:38.900
What we need to do.

04:39.060 --> 04:45.540
We need to set the root points dot value equals to the root.

04:45.580 --> 04:56.460
Otherwise, display a log message with tag as tag and could not calculate root or root is null.

04:56.500 --> 05:00.020
Then we need to check for running.

05:00.180 --> 05:06.230
So run log v location or user location is not.

05:06.350 --> 05:09.670
So this is how we implement this function.

05:09.670 --> 05:12.430
Fetch route from current position to marker.

05:12.670 --> 05:19.390
This function takes the marker that the destination marker for uh where we need to go.

05:19.750 --> 05:22.110
We need to go to that destination marker.

05:22.390 --> 05:30.630
And here we are launching the ViewModel scope in order to execute the location repository, get route

05:30.630 --> 05:32.630
because it's a suspend function.

05:32.790 --> 05:36.030
And we need to get the user location.

05:36.030 --> 05:43.190
So we need to fetch and call this route the origin, the current location of the user.

05:43.230 --> 05:47.590
The destination is the marker that it's passed as a parameter.

05:47.590 --> 05:52.950
If the route is not null, go and store it inside the variable route points.

05:52.950 --> 05:57.470
If it is not, if it is null, go and and display.

05:57.470 --> 05:59.270
The route is null.
