WEBVTT

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

00:01.360 --> 00:09.400
As usual, when finishing the repository and repository implementation, we need to create the module

00:09.560 --> 00:14.800
that provides us with the repository and its implementation.

00:15.000 --> 00:19.840
So here inside dependency injection package, create a new object.

00:19.880 --> 00:22.880
Name it as directions module.

00:22.920 --> 00:31.920
As usual, start with module annotation, then install in Singleton component class.

00:31.960 --> 00:36.040
This object provides us with many instances.

00:36.040 --> 00:40.240
For example, let's start with provides annotation.

00:40.280 --> 00:42.280
Singleton annotation.

00:42.440 --> 00:43.640
Create the function.

00:43.640 --> 00:47.320
Provide directions service.

00:47.400 --> 00:56.800
This function returns directions service return retrofit provider dot create directions service.

00:57.040 --> 01:03.290
And here we need to pass the base URL, so base url equals to.

01:03.490 --> 01:11.610
If you remember, we have the constant part which is maps dot Google maps API.

01:11.930 --> 01:19.250
You can include directions, but I include it in the previous uh calls and endpoints.

01:19.250 --> 01:26.810
So here paste the https maps.google.com api.com slash slash API.

01:26.850 --> 01:34.210
Okay, this function is a dagger hilt module that provides singleton instance of directions.

01:34.210 --> 01:36.890
Service for dependency injection.

01:36.930 --> 01:46.130
Provides marks this function as a dagger provider method telling dagger to use this function when an

01:46.130 --> 01:49.370
instance of direction service is needed.

01:49.570 --> 01:57.330
The singleton annotation ensures that only one instance of directions service is created and shared

01:57.330 --> 02:06.540
across the entire application, important for services that maintain no state or are expensive to create.

02:06.580 --> 02:12.420
The return type is directions service the interface that defines an API.

02:12.580 --> 02:13.380
Endpoints.

02:13.420 --> 02:15.420
Let me go back to here.

02:15.540 --> 02:17.940
As I told you in the previous videos.

02:17.980 --> 02:21.100
This this is the endpoint directions JSON.

02:21.100 --> 02:26.660
This is the base url https to Google API.

02:29.060 --> 02:29.620
API.

02:29.900 --> 02:35.180
So this is for this object directions module.

02:35.460 --> 02:39.900
And this part the directions slash JSON.

02:40.020 --> 02:43.580
This is for the directions service interface.

02:43.740 --> 02:45.260
This is the endpoint.

02:45.500 --> 02:46.060
Okay.

02:46.220 --> 02:49.820
So we define inside this object the base URL.

02:49.980 --> 02:55.940
And we defined inside the interface directions service the endpoint.

02:56.060 --> 03:02.710
This will delegate to Retrofit Provider Redirection Service method.

03:02.710 --> 03:10.830
We saw an error in the previous videos providing the base URL for Google Maps API endpoints.

03:10.830 --> 03:13.470
The base URL is defined in one place.

03:13.470 --> 03:15.830
Easy to change for different environments.

03:15.870 --> 03:21.030
All API path endpoints will be relative to this base URL.

03:21.310 --> 03:27.110
Any class can request directions service through constructor injection.

03:27.150 --> 03:32.150
Easy to mock for testing and single responsibility principle.

03:32.150 --> 03:39.870
The base URL endpoints to Google Maps version, version three endpoint and the complete API call is.

03:39.910 --> 03:44.190
Get it from here and you can check this whole response.

03:44.190 --> 03:51.630
So as I told you and the whole sorry call and the whole request, this is the base URL.

03:51.950 --> 03:58.120
This is the endpoint and this is the Destination variable.

03:58.160 --> 04:01.840
Origin variable and key variable.

04:01.880 --> 04:02.400
Okay.

04:02.520 --> 04:05.400
Let's continue with the directions module.

04:05.440 --> 04:08.200
We need to provide the repository.

04:08.200 --> 04:12.120
So provide and singleton annotation.

04:12.160 --> 04:13.040
Function.

04:13.040 --> 04:16.280
Provide directions repository.

04:16.320 --> 04:20.120
We need two variables to be passed the API key.

04:20.360 --> 04:27.600
It's of type string and the directions service which is of type direction service.

04:27.760 --> 04:31.920
Returning directions repository.

04:31.960 --> 04:38.080
Return directions repository implementation and pass.

04:38.080 --> 04:45.840
The API key equals to API key and the direction service as direction service.

04:45.880 --> 04:51.840
And if you remember in the directions repository implementation we need to inject the API key.

04:52.000 --> 05:00.170
So here instead of manually Inject the API key we can use this way.

05:00.210 --> 05:04.170
So provides named maps API key.

05:04.370 --> 05:06.290
Let's create this function.

05:06.330 --> 05:08.970
Provide maps API key.

05:09.170 --> 05:11.130
And this is the string.

05:11.130 --> 05:13.730
Here we need to put the API key.

05:13.770 --> 05:15.370
Go back to the manifest.

05:15.370 --> 05:18.370
Copy this code and paste it here.

05:18.490 --> 05:24.570
That provides annotation, as we said before marks this as a dependency provider method.

05:24.610 --> 05:31.210
Dagger will use this when a string dependency is requested and named.

05:31.370 --> 05:39.170
Annotation creates a qualifier for this specific string instance needed because you might have multiple

05:39.170 --> 05:46.410
string dependencies in your application, and differentiates this API key from other strings.

05:46.690 --> 05:51.290
Simply returns a hard coded Google Maps API key.

05:51.610 --> 05:56.620
In production, you might get this from a secure source, not hard coded.

05:56.860 --> 05:59.380
So why we use named annotation?

05:59.380 --> 06:08.060
If your app needs a different API keys or configuration strings, you might use them named also for

06:08.060 --> 06:15.140
explicit dependencies, making it easier which string is being injected and where.

06:15.220 --> 06:21.860
Also the type safety preventing accidental injecting the wrong string.

06:22.020 --> 06:31.300
When another class needs this API key, you can inject it using the this this key the maps underscore

06:31.340 --> 06:33.300
API underscore key.

06:33.460 --> 06:39.940
So let me go to here inside this directions repository implementation.

06:40.060 --> 06:51.220
Let me use the named annotation and specify the API key maps underscore API underscore key.
