Welcome back.
Let's continue with the ViewModel.
And under this companion object, let's create some variables and instances that we're going to use
in our application.
Late initiate var interpreter which is of type interpreter from TensorFlow private var object detector.
It's an instance of the class that we created before object detector.
And if this initialized set to zero var labels it's of list string equals to empty list okay.
Now private val state equals to mutable state.
Flow of response of type unit.
Mutable state of Alt+ enter to import it.
And here we need to set it the response dot loading.
Don't worry, we're going to clarify everything.
Val initial state equals to state dot as state flow.
What we did here.
Let me go back to understand this code to the util class package response class.
We have this sealed class.
We have three states.
Loading success and failure.
Back to the detection ViewModel.
Here we created a mutable state holder with an initial value of response dot loading.
So this is the initial value response is a sealed class that we created before representing different
states.
Loading success and failure unit here means this state doesn't carry any additional data, and the state
dot as Stateflow converts the mutable flow to a read only state flow.
External components can observe state changes, but can't modify the state directly.
This pattern is commonly used in MVC architecture to expose UI state for ViewModel to UI to UI layer
from a ViewModel to UI layer, allowing the ViewModel to update state internally and preventing the
UI from accidentally modifying the state.
And similar to this response, we're going to do with params.
So the detection parameters the score threshold is 0.6.
We're going to get it in the same way.
So private val params equals to mutable state flow of detection parameters.
Then val parameters equals to params dot as state flow.
This is the same pattern as above.
Let's continue with var detection list equals to mutable state of list of detection object and set the
initial value as an empty list or the list of.
Okay here.
If you see the the difference we used mutable state of instead of mutable state flow because this doesn't
depend on a an object, a custom object that we create.
It's a list of custom objects.
But here mutable state flow is detection parameters.
And the above is response class.
Okay so this is the difference.
Now let's create a new function called initialize.
We need to start loading the model.
So here inside this initialize function use try state dot value.
We need to get the state equals to response and modify it as loading.
Okay so I am modifying the state to response loading I am making it as loading.
So here the current state of the app is loading.
The model val model byte buffer equals to load model, which is the function we're going to create in
the next video.
Okay.
So here we need to load the model and pass the model file name model file name.
So we're going to skip this initialize function and start building the load model.
And the other functions that we're going to use it inside this initialize function.