WEBVTT

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

00:01.200 --> 00:03.280
Let's continue with the ViewModel.

00:03.280 --> 00:10.120
And under this companion object, let's create some variables and instances that we're going to use

00:10.120 --> 00:11.760
in our application.

00:11.800 --> 00:21.440
Late initiate var interpreter which is of type interpreter from TensorFlow private var object detector.

00:21.440 --> 00:26.200
It's an instance of the class that we created before object detector.

00:26.200 --> 00:37.320
And if this initialized set to zero var labels it's of list string equals to empty list okay.

00:37.360 --> 00:42.520
Now private val state equals to mutable state.

00:42.520 --> 00:46.400
Flow of response of type unit.

00:46.440 --> 00:50.080
Mutable state of Alt+ enter to import it.

00:50.080 --> 00:55.040
And here we need to set it the response dot loading.

00:55.080 --> 00:57.200
Don't worry, we're going to clarify everything.

00:57.360 --> 01:04.360
Val initial state equals to state dot as state flow.

01:04.400 --> 01:06.520
What we did here.

01:06.520 --> 01:14.170
Let me go back to understand this code to the util class package response class.

01:14.170 --> 01:15.810
We have this sealed class.

01:15.970 --> 01:18.130
We have three states.

01:18.170 --> 01:20.610
Loading success and failure.

01:20.810 --> 01:22.890
Back to the detection ViewModel.

01:23.050 --> 01:31.570
Here we created a mutable state holder with an initial value of response dot loading.

01:31.570 --> 01:39.450
So this is the initial value response is a sealed class that we created before representing different

01:39.450 --> 01:39.970
states.

01:40.010 --> 01:49.570
Loading success and failure unit here means this state doesn't carry any additional data, and the state

01:49.610 --> 01:57.530
dot as Stateflow converts the mutable flow to a read only state flow.

01:57.770 --> 02:04.010
External components can observe state changes, but can't modify the state directly.

02:04.170 --> 02:13.650
This pattern is commonly used in MVC architecture to expose UI state for ViewModel to UI to UI layer

02:13.810 --> 02:22.900
from a ViewModel to UI layer, allowing the ViewModel to update state internally and preventing the

02:22.900 --> 02:27.060
UI from accidentally modifying the state.

02:27.060 --> 02:31.700
And similar to this response, we're going to do with params.

02:31.700 --> 02:36.500
So the detection parameters the score threshold is 0.6.

02:36.500 --> 02:38.580
We're going to get it in the same way.

02:38.780 --> 02:47.860
So private val params equals to mutable state flow of detection parameters.

02:47.860 --> 02:53.740
Then val parameters equals to params dot as state flow.

02:53.780 --> 02:56.020
This is the same pattern as above.

02:56.060 --> 03:07.900
Let's continue with var detection list equals to mutable state of list of detection object and set the

03:07.900 --> 03:11.380
initial value as an empty list or the list of.

03:11.420 --> 03:12.420
Okay here.

03:12.540 --> 03:21.660
If you see the the difference we used mutable state of instead of mutable state flow because this doesn't

03:21.660 --> 03:25.700
depend on a an object, a custom object that we create.

03:25.740 --> 03:28.500
It's a list of custom objects.

03:28.500 --> 03:32.220
But here mutable state flow is detection parameters.

03:32.420 --> 03:35.920
And the above is response class.

03:35.960 --> 03:37.720
Okay so this is the difference.

03:37.800 --> 03:42.480
Now let's create a new function called initialize.

03:42.520 --> 03:45.480
We need to start loading the model.

03:45.560 --> 03:51.840
So here inside this initialize function use try state dot value.

03:51.880 --> 03:57.520
We need to get the state equals to response and modify it as loading.

03:57.640 --> 04:03.920
Okay so I am modifying the state to response loading I am making it as loading.

04:03.920 --> 04:08.040
So here the current state of the app is loading.

04:08.080 --> 04:18.720
The model val model byte buffer equals to load model, which is the function we're going to create in

04:18.760 --> 04:19.760
the next video.

04:19.840 --> 04:20.320
Okay.

04:20.480 --> 04:27.680
So here we need to load the model and pass the model file name model file name.

04:27.680 --> 04:32.760
So we're going to skip this initialize function and start building the load model.

04:32.760 --> 04:37.800
And the other functions that we're going to use it inside this initialize function.
