Welcome back.
We need to handle the possible states of asynchronous operation like loading success and failure.
For this purpose, I'm going to create a new package inside the data package.
Name it as util.
Inside this util, create a new Kotlin class, name it as response and make it as a sealed class.
So this is the class response sealed class and the type of parameter is out t.
Don't worry, we're gonna clarify everything here.
We have a data object loading returning type or its type response.
Nothing.
Data class success val data its response and data class failure val error exception.
Its of type exception.
The response is nothing.
This data class response represents the possible states of an asynchronous operation or API response.
Our D indicates that the type parameter t is covariant, so you can use response of type dog.
Response of type animal where response of any, for example any variant.
So the dog where animal is expected can be replaced by this.
And when using T we are using a generic wrapper for operation results.
Again guys out T this is the first time we use.
It indicates that the type parameter t is covariant, where you can use a response of type dog where
response of type animal is expected.
Okay, this is the main idea behind using T, because we can pass anything that inherits from the main
t, we have three possible states loading, success and failure.
Loading.
Operation is in progress.
No data available yet and nothing.
Type means it contains no value, so let me write it down.
Success.
Operation completed successfully contains the result data of type T and.
For example, you can make it a success of user data success a list of items.
And in this application we need success of list of items.
So here it contains the result data of type T failure.
Operation failed contains an operation exception explained what went wrong.
Nothing.
Type indicates that no no success data and it doesn't contain any data.
So we used the sealed class response.
And this is a pattern that is widely used in Android MVVM architectures.
And it provides a clean type safe way to handle asynchronous operations and UI states.