Useful Wrapper class That I use on a Daily Basis! - RequestState()

  Рет қаралды 5,853

Stevdza-San

Stevdza-San

4 ай бұрын

🏆 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...

Пікірлер: 53
@UsmonWasTaken
@UsmonWasTaken 4 ай бұрын
You can use inline value classes for the success and error cases just to avoid additional object creation :)
@UsmonWasTaken
@UsmonWasTaken 4 ай бұрын
+ 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.
@StevdzaSan
@StevdzaSan 4 ай бұрын
That's a good suggestion, thanks! :)
@pauloCosteira
@pauloCosteira 4 ай бұрын
How make this?
@pauloCosteira
@pauloCosteira 4 ай бұрын
inline class Success(val data: T) : RequestState() inline class Error(val message: String) : RequestState()
@UsmonWasTaken
@UsmonWasTaken 4 ай бұрын
@@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.
@theg4meover988
@theg4meover988 4 ай бұрын
Thanks! Very nice one.
@juancisneros5197
@juancisneros5197 4 ай бұрын
Thanks for share your knowledge with us, i really learn a lot with your videos
@jaelsonwagner
@jaelsonwagner 4 ай бұрын
Great tutorial. Thanks.
@gvharish9894
@gvharish9894 4 ай бұрын
Great Creation
@haykmkrtchyan7093
@haykmkrtchyan7093 4 ай бұрын
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 🙌
@PrashantSingh-d97
@PrashantSingh-d97 4 ай бұрын
Really nice
@ahmadab9666
@ahmadab9666 4 ай бұрын
nice, very helpful and save time
@StevdzaSan
@StevdzaSan 4 ай бұрын
Glad you think so!
@conamobile87
@conamobile87 4 ай бұрын
awesome
@leonidas_30052
@leonidas_30052 4 ай бұрын
Very good. All that was missing was the empty state.
@user-dk5cr6bh9w
@user-dk5cr6bh9w 4 ай бұрын
Great video! Have you considered casting with as? to handle runtime exception?
@StevdzaSan
@StevdzaSan 4 ай бұрын
Yes, that's also a good approach.
@crazy4028
@crazy4028 4 ай бұрын
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?
@akashkumardas6521
@akashkumardas6521 4 ай бұрын
Once a noob, always be a noob. Use your mind 😊
@crazy4028
@crazy4028 4 ай бұрын
@@akashkumardas6521 okay)
@dayona2513
@dayona2513 4 ай бұрын
Will be very useful if sealed class can be used from another package
@lakkidev8280
@lakkidev8280 4 ай бұрын
Where can we write a mapping function to handle successful data mapping?
@alejandrogallego5419
@alejandrogallego5419 4 ай бұрын
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 ?
@pelealexandru
@pelealexandru 4 ай бұрын
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.
@StevdzaSan
@StevdzaSan 4 ай бұрын
You could add a Throwable/Exception as a parameter instead of the String.
@pelealexandru
@pelealexandru 4 ай бұрын
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?
@StevdzaSan
@StevdzaSan 4 ай бұрын
@@pelealexandru Exception is the parent of all other ones.
@ahmetozcan2278
@ahmetozcan2278 4 ай бұрын
@@pelealexandru Maybe you can create a BusinessException/ServiceException classes at core package and then you can propagate them to upper levels
@simonsarhin2114
@simonsarhin2114 4 ай бұрын
Why do you think about localization when your data will come from the server??
@nikolaymiroshnychenko7712
@nikolaymiroshnychenko7712 4 ай бұрын
Question, will this violate clean architecture principles if we're referencing the UI (Composable functions) in the RequestState class?
@StevdzaSan
@StevdzaSan 4 ай бұрын
You can also create an extension function if that's the case.
@nikolaymiroshnychenko7712
@nikolaymiroshnychenko7712 4 ай бұрын
Thanks!@@StevdzaSan
@meetb26
@meetb26 4 ай бұрын
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
@jamesdavenavor6247
@jamesdavenavor6247 3 ай бұрын
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?
@StevdzaSan
@StevdzaSan 3 ай бұрын
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.
@jamesdavenavor6247
@jamesdavenavor6247 3 ай бұрын
​@@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.
@jamesdavenavor6247
@jamesdavenavor6247 3 ай бұрын
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
@SketchIDE
@SketchIDE 4 ай бұрын
Please Make Video About how i can Create or design like MIT Scrach Block In my android app In kotline please
@_hudeifa23
@_hudeifa23 3 ай бұрын
do you have compose multiplatform course?
@StevdzaSan
@StevdzaSan 3 ай бұрын
I'm currently working on one. But until then you can check out this tutorial: kzfaq.info/get/bejne/Z7p8npZnq8rRfXU.html
@_hudeifa23
@_hudeifa23 3 ай бұрын
@@StevdzaSan please pay attention to situations that we need to write spicific platform code like image uploading , map implementing , permissions
@kacetal
@kacetal 4 ай бұрын
If you put this class on the domain layer, it's not so good to share ux dependency with it
@forester1
@forester1 4 ай бұрын
First comment from Pakistan 😂
@ahmetozcan2278
@ahmetozcan2278 4 ай бұрын
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) } } } }
@StevdzaSan
@StevdzaSan 4 ай бұрын
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. 👍
@ahmetozcan2278
@ahmetozcan2278 4 ай бұрын
@@StevdzaSan makes a lots of sense :)
@error-code-511
@error-code-511 4 ай бұрын
Anyone want to contribute to Simple Android App??
Svelte 5's Secret Weapon: Classes + Context
18:14
Huntabyte
Рет қаралды 15 М.
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 8 МЛН
MISS CIRCLE STUDENTS BULLY ME!
00:12
Andreas Eskander
Рет қаралды 20 МЛН
Everything you need to know about Kotlin 2.0 🟣
11:05
Stevdza-San
Рет қаралды 62 М.
MVVM vs. MVI - Understand the Difference Once and for All
18:40
Philipp Lackner
Рет қаралды 36 М.
Lottie Animation using Jetpack Compose - Android Studio Tutorial
3:28
Daniel Atitienei
Рет қаралды 1,2 М.
Custom Navigation Drawer component with Jetpack Compose
6:01
Stevdza-San
Рет қаралды 6 М.
THIS is the BEST Way to Write HTTP Services in Golang
13:53
Nobody Cares About Your Coding Projects
11:02
Tariq10x
Рет қаралды 50 М.
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Dylan Beattie
Рет қаралды 128 М.
derivedStateOf VS. remember(key) - THIS is Really the Difference 🤯
14:46
Why Agent Frameworks Will Fail (and what to use instead)
19:21
Dave Ebbelaar
Рет қаралды 35 М.