WEBVTT

00:00.080 --> 00:03.360
We created the ViewModel and the repository.

00:03.360 --> 00:11.560
Since we are using hilt for dependency injection, we need to create a module to tell hilt how to create

00:11.560 --> 00:14.960
and provide dependencies to the rest of your app.

00:15.160 --> 00:22.320
Please refer back to the previous sections and dependency injection sections, and especially dealing

00:22.320 --> 00:30.480
with third party libraries like Firebase Retrofit and uh, and others in order to learn how to create

00:30.480 --> 00:31.080
modules.

00:31.080 --> 00:32.640
Why we use modules.

00:32.640 --> 00:36.840
But don't worry I'll I'm going to clarify everything.

00:36.840 --> 00:39.560
So please pay attention with me.

00:39.560 --> 00:46.400
Since we are dealing with third party libraries like Firebase, we need to create modules to tell hilt

00:46.440 --> 00:50.440
how to create and provide dependencies to the rest of your app.

00:50.680 --> 00:57.920
Inside this package, create a new package named as die deferring for dependency injection.

00:57.960 --> 01:00.440
New Kotlin object.

01:00.480 --> 01:03.800
Name it as Firebase module.

01:03.840 --> 01:09.770
Annotate it with module annotation and install in.

01:09.810 --> 01:14.530
Set the singleton component class Alt+.

01:14.530 --> 01:21.650
Enter singleton, not single component, singleton component and imported module.

01:21.690 --> 01:31.930
This object contains methods that provide dependencies, so the Firebase module contains methods that

01:31.930 --> 01:33.650
provide dependencies.

01:33.850 --> 01:36.170
Install in annotation.

01:36.530 --> 01:40.730
This says where these dependencies live.

01:40.970 --> 01:48.730
Singleton component means these dependencies exist for the lifetime of your application.

01:49.010 --> 01:50.530
They are singletons.

01:50.770 --> 01:57.210
One shared instance of your whole application the Firebase module.

01:57.330 --> 01:59.370
This is the Kotlin object.

01:59.370 --> 02:01.450
It's a singleton itself.

02:01.450 --> 02:04.930
No need to instantiate Firebase module.

02:04.930 --> 02:10.690
Now we need to create the functions that this object provide.

02:10.730 --> 02:14.770
We have the Firebase and the repository.

02:15.050 --> 02:18.530
So let's start with Firebase Firestore.

02:18.570 --> 02:24.810
Function provide Firebase Firestore and return Firebase Firestore.

02:24.850 --> 02:26.490
Get instance Alt+.

02:26.490 --> 02:27.850
Enter to import it.

02:28.010 --> 02:34.010
And by the way thanks for the AI code completion and Gemini.

02:34.210 --> 02:37.370
Okay this is a tool in Android Studio.

02:37.530 --> 02:49.410
Then we need to add a new function called Provide Marker Repository Firestore and returns a marker repository

02:49.530 --> 02:56.650
equals to marker repository implementation and pass via store.

02:56.810 --> 02:59.730
In this object we created two functions.

02:59.730 --> 03:02.730
The first function provide Firebase Firestore.

03:03.050 --> 03:11.090
Returning type Firebase Firestore and it it calls Firebase Firestore dot get instance.

03:11.130 --> 03:16.490
This this is the standard Firebase SDK way to get the singleton.

03:16.650 --> 03:26.500
And here we provide marker repository Passing a parameter of type Firestore returning marker repository

03:26.700 --> 03:32.220
and this equals to the marker repository and pass the firestore instance to it.

03:32.220 --> 03:40.420
So I am calling this instance of marker repository and marker repository implementation and passing

03:40.660 --> 03:44.100
the Firebase instance to it okay.

03:44.140 --> 03:52.900
So those are the two functions we need to provide in order to tell hilt how to create and how to provide

03:52.900 --> 03:53.460
them.

03:53.460 --> 03:58.460
We need to annotate them with two annotations.

03:58.660 --> 04:01.620
The first one is called provides.

04:01.820 --> 04:10.100
And by the way I talked about provides annotation and singleton annotation a lot in the previous videos.

04:10.100 --> 04:16.140
So please go back to learn more about singletons and provides provides.

04:16.140 --> 04:22.780
This function tells hilt how to create Firebase Firestore.

04:22.940 --> 04:29.230
Singleton annotation health will create one instance for the whole app.

04:29.630 --> 04:31.190
The function body.

04:31.310 --> 04:32.190
Return.

04:32.230 --> 04:32.870
Firebase.

04:32.870 --> 04:33.430
Firestore.

04:33.630 --> 04:38.110
Get instance the standard Firebase SDK way to get the singleton.

04:38.110 --> 04:45.310
So anywhere you ask for Firebase Firestore hilt gives you this instance.

04:45.510 --> 04:55.070
Again guys anywhere you ask for Firebase Firestore instance hilt will provide you with this instance

04:55.270 --> 04:55.830
okay.

04:56.070 --> 04:58.590
This is the concept behind it.

04:58.630 --> 05:06.750
Now let's create those two uh those two annotations in function.

05:06.990 --> 05:09.790
And the function provide marker repository.

05:10.030 --> 05:15.750
So provides and singleton again guys provides annotation.

05:15.790 --> 05:20.310
This function tells hilt how to create a marker repository.

05:20.470 --> 05:25.630
Firebase Firestore function and parameter.

05:25.830 --> 05:29.830
Hilt sees that this parameter is needed.

05:30.070 --> 05:39.190
It automatically calls provide firebase store function to get it equals to marker repository implementation

05:39.190 --> 05:46.190
creates a new instance of your repository implementation passing Firestore into the constructor.

05:46.190 --> 05:47.910
Singleton annotation.

05:47.950 --> 05:55.310
Again, only one marker repository implementation exists in the app.

05:55.310 --> 06:01.670
So anywhere you inject marker repository hilt uses this function to build it.

06:01.710 --> 06:02.270
Okay.

06:02.630 --> 06:11.190
So to get Firebase Firestore use Firebase Firestore dot get instance to get marker repository.

06:11.350 --> 06:15.870
Build marker repository implementation with the Firestore instance.

06:16.070 --> 06:25.430
Hill takes care of the order of creation the scooping singleton lifetime and passing dependencies automatically.
