Welcome back.
Now let's create the view model here.
Inside this package create another package named as view model.
Inside the View Models package.
Create a new Kotlin class.
Name it as Firebase View model.
Or you can join Firebase location directions and all the repositories and all the methods and functions
and operations in one view model like this.
Named as map View model.
Okay, this is for simplicity and not being confused okay.
So after the repository we need to create the view model.
This view model.
This class will be annotated with hilt view model.
Did you remember this annotation.
It's very common and we use it in the previous videos.
Hilton ViewModel is an annotation you put on your ViewModel class to telehealth, a dependency injection
library that this ViewModel needs to be created by.
Hilt automatically provide its dependencies like repositories via constructor injection.
Why we use it so there is no need to manually create ViewModel factory's dependencies are injected automatically.
Works seamlessly seamlessly with Jetpack Compose and Hilt ViewModel function.
Okay, so this is the idea behind the usage of hilt ViewModel annotation.
It's an annotation to put your.
You put it on your ViewModel class to tell health that this ViewModel needs to be created by hilt,
automatically provide its dependencies like repositories via constructor injection.
Okay, so this is the hilt ViewModel annotation.
Let's continue with this class.
And here we need to use another uh in another annotation called inject annotation okay.
The constructor private val marker repository.
It's of type marker repository okay Alt+ enter to import it.
And here we go.
So this hilt ViewModel will automatically provide its dependencies.
And in this case it's a marker repository via constructor injection.
The next step is to tell Android Studio that this map view model is a ViewModel.
By extending from View model class.
Okay, this inherits from the ViewModel class.
Now we need to create the function and marker, Which we'll call the repository dot add marker function.
So function add marker takes four parameters the marker name on success and on failure callbacks that
let lung it's of type Google maps model.lat long string.
Then we need another parameters which are the callbacks on success.
It's a lambda expression that takes nothing and returns nothing on failure takes exception and return
nothing.
Okay, this is the add marker.
Now we need to define the body of this function.
Start with view model scoop dot launch.
This launches a coroutine bound to the view models lifecycle.
If the view model is cleared, the coroutine is automatically canceled.
It keep or it keeps work off the main thread, so this will be done in the background thread.
What happens here?
Try to call repository dot, add marker and insert marker with the name.
If it completes successfully, call Onsuccess callback to notify your UI if anything fails.
No internet.
For example, go and execute the catch block, catch the exception and call the Onfailure function with
the error so your UI can show an error message.
The control flow of the flow of control.
In this case UI calls ViewModel dot add marker a ViewModel launches a coroutine.
Calls the repository to add a marker in Firebase.
Wait for completion if it's success.
Go and execute Onsuccess function and callback.
Show a toast message or update UI or make a alert dialog if failed.
Go and execute the Onfailure callback function called with the error.
This is a good way for separation of concerns.
Lifecycle safety and flexibility.
So ViewModel handles business logic.
Repository handles data storage UI just reacts to success or failure.
ViewModel.
Safety for the ViewModel scope prevents memory leaks.
You can easily swap marker repository implementations for testing.
Also, you can unit test map a ViewModel by mocking the repository.