WEBVTT

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

00:01.240 --> 00:08.960
We need to handle the possible states of asynchronous operation like loading success and failure.

00:09.080 --> 00:14.520
For this purpose, I'm going to create a new package inside the data package.

00:14.560 --> 00:16.600
Name it as util.

00:16.760 --> 00:26.120
Inside this util, create a new Kotlin class, name it as response and make it as a sealed class.

00:26.120 --> 00:34.760
So this is the class response sealed class and the type of parameter is out t.

00:35.040 --> 00:37.960
Don't worry, we're gonna clarify everything here.

00:37.960 --> 00:44.120
We have a data object loading returning type or its type response.

00:44.320 --> 00:45.160
Nothing.

00:45.320 --> 00:55.160
Data class success val data its response and data class failure val error exception.

00:55.160 --> 00:57.040
Its of type exception.

00:57.080 --> 00:58.960
The response is nothing.

00:59.120 --> 01:08.640
This data class response represents the possible states of an asynchronous operation or API response.

01:09.880 --> 01:19.680
Our D indicates that the type parameter t is covariant, so you can use response of type dog.

01:19.800 --> 01:28.600
Response of type animal where response of any, for example any variant.

01:28.640 --> 01:35.920
So the dog where animal is expected can be replaced by this.

01:35.920 --> 01:42.920
And when using T we are using a generic wrapper for operation results.

01:42.960 --> 01:46.480
Again guys out T this is the first time we use.

01:46.480 --> 01:56.360
It indicates that the type parameter t is covariant, where you can use a response of type dog where

01:56.360 --> 02:00.200
response of type animal is expected.

02:00.360 --> 02:09.640
Okay, this is the main idea behind using T, because we can pass anything that inherits from the main

02:09.680 --> 02:15.160
t, we have three possible states loading, success and failure.

02:15.320 --> 02:16.120
Loading.

02:16.200 --> 02:17.880
Operation is in progress.

02:17.880 --> 02:21.440
No data available yet and nothing.

02:21.480 --> 02:26.440
Type means it contains no value, so let me write it down.

02:26.480 --> 02:27.280
Success.

02:27.440 --> 02:35.040
Operation completed successfully contains the result data of type T and.

02:35.080 --> 02:40.600
For example, you can make it a success of user data success a list of items.

02:40.840 --> 02:46.960
And in this application we need success of list of items.

02:46.960 --> 02:52.480
So here it contains the result data of type T failure.

02:52.680 --> 03:00.160
Operation failed contains an operation exception explained what went wrong.

03:00.400 --> 03:01.200
Nothing.

03:01.320 --> 03:08.840
Type indicates that no no success data and it doesn't contain any data.

03:08.840 --> 03:12.680
So we used the sealed class response.

03:12.880 --> 03:18.840
And this is a pattern that is widely used in Android MVVM architectures.

03:18.840 --> 03:26.680
And it provides a clean type safe way to handle asynchronous operations and UI states.
