WEBVTT

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

00:01.200 --> 00:04.720
Now let's create the view model here.

00:04.720 --> 00:10.760
Inside this package create another package named as view model.

00:10.760 --> 00:12.480
Inside the View Models package.

00:12.480 --> 00:15.760
Create a new Kotlin class.

00:15.800 --> 00:19.160
Name it as Firebase View model.

00:19.200 --> 00:30.760
Or you can join Firebase location directions and all the repositories and all the methods and functions

00:30.760 --> 00:35.400
and operations in one view model like this.

00:35.400 --> 00:37.960
Named as map View model.

00:38.000 --> 00:42.480
Okay, this is for simplicity and not being confused okay.

00:42.640 --> 00:46.920
So after the repository we need to create the view model.

00:46.920 --> 00:48.240
This view model.

00:48.240 --> 00:54.040
This class will be annotated with hilt view model.

00:54.040 --> 00:56.200
Did you remember this annotation.

00:56.200 --> 01:00.270
It's very common and we use it in the previous videos.

01:00.270 --> 01:08.750
Hilton ViewModel is an annotation you put on your ViewModel class to telehealth, a dependency injection

01:08.750 --> 01:13.710
library that this ViewModel needs to be created by.

01:13.830 --> 01:21.350
Hilt automatically provide its dependencies like repositories via constructor injection.

01:21.350 --> 01:28.910
Why we use it so there is no need to manually create ViewModel factory's dependencies are injected automatically.

01:29.110 --> 01:35.790
Works seamlessly seamlessly with Jetpack Compose and Hilt ViewModel function.

01:35.830 --> 01:42.270
Okay, so this is the idea behind the usage of hilt ViewModel annotation.

01:42.270 --> 01:44.230
It's an annotation to put your.

01:44.350 --> 01:51.630
You put it on your ViewModel class to tell health that this ViewModel needs to be created by hilt,

01:51.870 --> 01:57.670
automatically provide its dependencies like repositories via constructor injection.

01:57.710 --> 02:01.390
Okay, so this is the hilt ViewModel annotation.

02:01.390 --> 02:03.270
Let's continue with this class.

02:03.270 --> 02:10.510
And here we need to use another uh in another annotation called inject annotation okay.

02:10.750 --> 02:16.870
The constructor private val marker repository.

02:16.910 --> 02:22.790
It's of type marker repository okay Alt+ enter to import it.

02:22.790 --> 02:23.990
And here we go.

02:24.230 --> 02:30.470
So this hilt ViewModel will automatically provide its dependencies.

02:30.470 --> 02:35.550
And in this case it's a marker repository via constructor injection.

02:35.590 --> 02:41.230
The next step is to tell Android Studio that this map view model is a ViewModel.

02:41.270 --> 02:45.390
By extending from View model class.

02:45.430 --> 02:49.110
Okay, this inherits from the ViewModel class.

02:49.230 --> 02:59.700
Now we need to create the function and marker, Which we'll call the repository dot add marker function.

02:59.700 --> 03:11.380
So function add marker takes four parameters the marker name on success and on failure callbacks that

03:11.380 --> 03:17.780
let lung it's of type Google maps model.lat long string.

03:18.060 --> 03:25.500
Then we need another parameters which are the callbacks on success.

03:25.660 --> 03:34.340
It's a lambda expression that takes nothing and returns nothing on failure takes exception and return

03:34.380 --> 03:35.100
nothing.

03:35.220 --> 03:37.660
Okay, this is the add marker.

03:37.700 --> 03:41.900
Now we need to define the body of this function.

03:41.900 --> 03:45.940
Start with view model scoop dot launch.

03:45.980 --> 03:50.860
This launches a coroutine bound to the view models lifecycle.

03:51.100 --> 03:57.020
If the view model is cleared, the coroutine is automatically canceled.

03:57.020 --> 04:06.260
It keep or it keeps work off the main thread, so this will be done in the background thread.

04:06.300 --> 04:07.780
What happens here?

04:08.060 --> 04:14.340
Try to call repository dot, add marker and insert marker with the name.

04:14.340 --> 04:23.300
If it completes successfully, call Onsuccess callback to notify your UI if anything fails.

04:23.300 --> 04:24.140
No internet.

04:24.140 --> 04:33.060
For example, go and execute the catch block, catch the exception and call the Onfailure function with

04:33.060 --> 04:38.060
the error so your UI can show an error message.

04:38.060 --> 04:41.100
The control flow of the flow of control.

04:41.380 --> 04:49.220
In this case UI calls ViewModel dot add marker a ViewModel launches a coroutine.

04:49.300 --> 04:54.290
Calls the repository to add a marker in Firebase.

04:54.570 --> 04:57.770
Wait for completion if it's success.

04:57.970 --> 05:01.530
Go and execute Onsuccess function and callback.

05:01.570 --> 05:08.770
Show a toast message or update UI or make a alert dialog if failed.

05:08.970 --> 05:14.690
Go and execute the Onfailure callback function called with the error.

05:14.810 --> 05:19.130
This is a good way for separation of concerns.

05:19.170 --> 05:22.090
Lifecycle safety and flexibility.

05:22.290 --> 05:24.810
So ViewModel handles business logic.

05:24.810 --> 05:30.610
Repository handles data storage UI just reacts to success or failure.

05:30.890 --> 05:31.850
ViewModel.

05:31.890 --> 05:35.530
Safety for the ViewModel scope prevents memory leaks.

05:35.570 --> 05:41.050
You can easily swap marker repository implementations for testing.

05:41.210 --> 05:46.610
Also, you can unit test map a ViewModel by mocking the repository.
