Useful Wrapper class That I use on a Daily Basis! - RequestState()
ฝัง
- เผยแพร่เมื่อ 15 มี.ค. 2024
- 🏆 My Online Courses
stevdza-san.com
📝 Writing on Medium
/ stevdza-san
☕ Let's get a coffee. You're paying! :)
ko-fi.com/stevdza_san
💻 Github
github.com/stevdza-san
📸 Instagram
/ stevdza_san
Source Code: gist.github.com/stevdza-san/c...
You can use inline value classes for the success and error cases just to avoid additional object creation :)
+ You can define contract in your isLoading, isSuccess, and isError functions so you get the benefits of Kotlin smart casting. Then you don't even need these getSuccessData, and getSuccessDataOrNull functions, the compiler forces you to check before accessing success data.
That's a good suggestion, thanks! :)
How make this?
inline class Success(val data: T) : RequestState()
inline class Error(val message: String) : RequestState()
@@pauloCosteira Instead of using the "data" modifier in front of the class, just use the "value" modifier and annotate the class with @JvmInline annotation. But, there's a limitation that you only have a single property in the constructor since the value will be inlined.
Thanks for share your knowledge with us, i really learn a lot with your videos
Thanks! Very nice one.
Great tutorial. Thanks.
Great Creation
There's also some mapping should be applied in a domain layer because we may get a list of GraphQL objects instead of a String and we sure don't want to expose them up to the UI layer. Great tutorial, thanks 🙌
Really nice
nice, very helpful and save time
Glad you think so!
awesome
Very good. All that was missing was the empty state.
Great video! Have you considered casting with as? to handle runtime exception?
Yes, that's also a good approach.
Thank you Stevdza for sharing your RequestState solution! I would love to know how this would work with more complicated error cases. For example in a "login" scenario, you could have multiple types of errors: no internet, incorrect credentials, etc. In this case, a message string would not be enough to differentiate the error case in the ViewModel. Also, this way the messages cannot be localised. Would love to hear your opinion on this.
You could add a Throwable/Exception as a parameter instead of the String.
Yes, that could work! The only downside would be we would be exposing very specific "data layer" entities to our ViewModel, such as RetrofitExceptions for example. Arguably, the ViewModel shouldn't know about HTTP error codes, database errors and so on. How would you suggest we get around this issue?
@@pelealexandru Exception is the parent of all other ones.
@@pelealexandru Maybe you can create a BusinessException/ServiceException classes at core package and then you can propagate them to upper levels
Why do you think about localization when your data will come from the server??
Will be very useful if sealed class can be used from another package
Thank you Stevdza for sharing. How call services POST? and in function make DTO ex: val dto = DTO(name="a", lastname="b") how implent functions ?
Where can we write a mapping function to handle successful data mapping?
The situation is as follows: we enter the login data and send it to the server, at the time of this we show the progress indicator, and if the data is incorrect, then we stay on this screen and show the previously entered data. How do I do this using your wrapper?
Once a noob, always be a noob. Use your mind 😊
@@akashkumardas6521 okay)
Question, will this violate clean architecture principles if we're referencing the UI (Composable functions) in the RequestState class?
You can also create an extension function if that's the case.
Thanks!@@StevdzaSan
I have doubt. question will be very complicate. In xml with mvvm architecture we use single state for state management like loading, success, error, how can in jetpack compose
do you have compose multiplatform course?
I'm currently working on one. But until then you can check out this tutorial: th-cam.com/video/1TLk36FdmMA/w-d-xo.html
@@StevdzaSan please pay attention to situations that we need to write spicific platform code like image uploading , map implementing , permissions
I'm using a similar approach when loading and showing lists but I'm experiencing a glitch where the list is not shown from the top. When loading, the main LazyColumn is hidden then I show a shimmer layout to indicate loading. After loading, the LazyColumn will be shown but at times it doesn't show the first item. Can you help me with this?
If you're using Scaffold, you need to use it to calculate the top padding on the lazy column. Since the first item is probably hidden behind the Scaffold's TopAppBar.
@@StevdzaSan Thanks for the answer, however, my lazy column is not directly inside the Scaffold. It's inside a Column with some filters above before it.
It is inside a Box Animated Content along with the Loading Layout Composable since I'm using PullToRefresh.
Btw, the data is from paging library.
While the lazyPagingItems is Refreshing, the Loading Layout will be displayed instead of the LazyColumn.
I've tested loading the items by selecting a filter that shows the same items but at times it displays the 2nd item instead of the first
Please Make Video About how i can Create or design like MIT Scrach Block In my android app In kotline please
First comment from Pakistan 😂
If you put this class on the domain layer, it's not so good to share ux dependency with it
You can also define the Error type and DisplayResult composable generic and you can pass the data directly to relevant composable
Something like this
sealed class Result {
data object Idle: Result()
data object InProgress: Result()
data class Success(val data: T): Result()
data class Error(val error: Error): Result()
@Composable
fun DisplayResult(
onIdle: @Composable () -> Unit,
onLoading: @Composable () -> Unit,
onSuccess: @Composable (data: Data) -> Unit,
onError: @Composable (error: Error) -> Unit
) {
when(this) {
is Result.Error -> {
onError(this.error)
}
Idle -> {
onIdle()
}
InProgress -> {
onLoading()
}
is Success -> {
onSuccess(this.data)
}
}
}
}
Thanks for the suggestion, I have already updated the source code, tho. You can check it out. Btw, I have received feedback that UI dependencies shouldn't be placed inside a domain layer. For that case, you could also use an extension function instead of adding the composable directly in the class. 👍
@@StevdzaSan makes a lots of sense :)
Anyone want to contribute to Simple Android App??