Welcome back.
After we created this directions module, let's move to the map view model and inject the constructor
with this repository.
So here private val directions repository and import the directions repository.
Scroll down in order to create the functions for the directions package.
So here directions API we need to fetch the route to marker and the multiple route to markers okay.
So first of all private Val route which is of type mutable state flow list of lat long and the route
points.
So let me name it route points.
This route points.
Here we are creating an observable data stream for tracking route points.
This is very important because we need to keep tracking for the root points.
Why we are doing this pattern here.
Underscore root points.
Private banking.
Property conversion, a convention with underscore and mutable state.
Flow a mutable container that holds a state and emits updates to collectors returning a or of type list.
Lat long type represents nullable list of geographic coordinates initialized with null.
So this is a public read only state flow exposes read only version to external components as state flow,
and as state flow function converts mutable flow to immutable.
Consumers can observe changes but can't modify directly.
So this is a very important note.
And it's commonly used this this pattern commonly used in many applications, and we talked about it
in the previous videos so consumers can observe changes but can't modify directly.
Then create a new function, name it as fetch to or from current position to marker.
Taking a marker as a lat long parameter.
And inside this function, we need to use the ViewModel scope in order to call the directions dot get
route.
If you remember from the directions repository implementation, we have two functions.
Get route for multiple markers and get route.
Here we inside this get route, we are fetching the data from the origin to the destination.
The origin would be the current position of the user and the destination is the selected marker.
So here inside this function.
Let's start with ViewModel scoop dot launch.
Also, if you remember, we made those functions as suspend functions inside this directions repository
implementation.
So for this purpose we need to call them from the ViewModel scoop.
This ViewModel scoop start with location repository dot user location dot value.
And here we are executing the let block.
If the location repository dot user location not equals to the not equals to the uh to null, then we
need to execute this block.
What we need to create.
We need to set the current location as a parameter of this lambda expression and go and create val root
equals to directions.
Data or directions repository dot get root.
Inside this function pass the origin and destination.
What is the origin?
The current location and what is the destination?
The marker that is passed as a parameter for this function.
Then let me check if this root doesn't equal to null.
What we need to do.
We need to set the root points dot value equals to the root.
Otherwise, display a log message with tag as tag and could not calculate root or root is null.
Then we need to check for running.
So run log v location or user location is not.
So this is how we implement this function.
Fetch route from current position to marker.
This function takes the marker that the destination marker for uh where we need to go.
We need to go to that destination marker.
And here we are launching the ViewModel scope in order to execute the location repository, get route
because it's a suspend function.
And we need to get the user location.
So we need to fetch and call this route the origin, the current location of the user.
The destination is the marker that it's passed as a parameter.
If the route is not null, go and store it inside the variable route points.
If it is not, if it is null, go and and display.
The route is null.