Welcome back.
As usual, when finishing the repository and repository implementation, we need to create the module
that provides us with the repository and its implementation.
So here inside dependency injection package, create a new object.
Name it as directions module.
As usual, start with module annotation, then install in Singleton component class.
This object provides us with many instances.
For example, let's start with provides annotation.
Singleton annotation.
Create the function.
Provide directions service.
This function returns directions service return retrofit provider dot create directions service.
And here we need to pass the base URL, so base url equals to.
If you remember, we have the constant part which is maps dot Google maps API.
You can include directions, but I include it in the previous uh calls and endpoints.
So here paste the https maps.google.com api.com slash slash API.
Okay, this function is a dagger hilt module that provides singleton instance of directions.
Service for dependency injection.
Provides marks this function as a dagger provider method telling dagger to use this function when an
instance of direction service is needed.
The singleton annotation ensures that only one instance of directions service is created and shared
across the entire application, important for services that maintain no state or are expensive to create.
The return type is directions service the interface that defines an API.
Endpoints.
Let me go back to here.
As I told you in the previous videos.
This this is the endpoint directions JSON.
This is the base url https to Google API.
API.
So this is for this object directions module.
And this part the directions slash JSON.
This is for the directions service interface.
This is the endpoint.
Okay.
So we define inside this object the base URL.
And we defined inside the interface directions service the endpoint.
This will delegate to Retrofit Provider Redirection Service method.
We saw an error in the previous videos providing the base URL for Google Maps API endpoints.
The base URL is defined in one place.
Easy to change for different environments.
All API path endpoints will be relative to this base URL.
Any class can request directions service through constructor injection.
Easy to mock for testing and single responsibility principle.
The base URL endpoints to Google Maps version, version three endpoint and the complete API call is.
Get it from here and you can check this whole response.
So as I told you and the whole sorry call and the whole request, this is the base URL.
This is the endpoint and this is the Destination variable.
Origin variable and key variable.
Okay.
Let's continue with the directions module.
We need to provide the repository.
So provide and singleton annotation.
Function.
Provide directions repository.
We need two variables to be passed the API key.
It's of type string and the directions service which is of type direction service.
Returning directions repository.
Return directions repository implementation and pass.
The API key equals to API key and the direction service as direction service.
And if you remember in the directions repository implementation we need to inject the API key.
So here instead of manually Inject the API key we can use this way.
So provides named maps API key.
Let's create this function.
Provide maps API key.
And this is the string.
Here we need to put the API key.
Go back to the manifest.
Copy this code and paste it here.
That provides annotation, as we said before marks this as a dependency provider method.
Dagger will use this when a string dependency is requested and named.
Annotation creates a qualifier for this specific string instance needed because you might have multiple
string dependencies in your application, and differentiates this API key from other strings.
Simply returns a hard coded Google Maps API key.
In production, you might get this from a secure source, not hard coded.
So why we use named annotation?
If your app needs a different API keys or configuration strings, you might use them named also for
explicit dependencies, making it easier which string is being injected and where.
Also the type safety preventing accidental injecting the wrong string.
When another class needs this API key, you can inject it using the this this key the maps underscore
API underscore key.
So let me go to here inside this directions repository implementation.
Let me use the named annotation and specify the API key maps underscore API underscore key.