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

ความคิดเห็น • 53

  • @UsmonWasTaken
    @UsmonWasTaken 4 หลายเดือนก่อน +13

    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 หลายเดือนก่อน +2

      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.

  • @juancisneros5197
    @juancisneros5197 4 หลายเดือนก่อน

    Thanks for share your knowledge with us, i really learn a lot with your videos

  • @theg4meover988
    @theg4meover988 4 หลายเดือนก่อน

    Thanks! Very nice one.

  • @jaelsonwagner
    @jaelsonwagner 4 หลายเดือนก่อน

    Great tutorial. Thanks.

  • @gvharish9894
    @gvharish9894 4 หลายเดือนก่อน

    Great Creation

  • @haykmkrtchyan7093
    @haykmkrtchyan7093 4 หลายเดือนก่อน +1

    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 หลายเดือนก่อน +1

    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.

  • @pelealexandru
    @pelealexandru 4 หลายเดือนก่อน +1

    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??

  • @dayona2513
    @dayona2513 4 หลายเดือนก่อน

    Will be very useful if sealed class can be used from another package

  • @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 ?

  • @lakkidev8280
    @lakkidev8280 4 หลายเดือนก่อน

    Where can we write a mapping function to handle successful data mapping?

  • @crazy4028
    @crazy4028 4 หลายเดือนก่อน +2

    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 หลายเดือนก่อน +2

      Once a noob, always be a noob. Use your mind 😊

    • @crazy4028
      @crazy4028 4 หลายเดือนก่อน

      @@akashkumardas6521 okay)

  • @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 หลายเดือนก่อน +1

      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

  • @_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: th-cam.com/video/1TLk36FdmMA/w-d-xo.html

    • @_hudeifa23
      @_hudeifa23 3 หลายเดือนก่อน

      @@StevdzaSan please pay attention to situations that we need to write spicific platform code like image uploading , map implementing , permissions

  • @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

  • @forester1
    @forester1 4 หลายเดือนก่อน +1

    First comment from Pakistan 😂

  • @kacetal
    @kacetal 4 หลายเดือนก่อน

    If you put this class on the domain layer, it's not so good to share ux dependency with it

  • @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 หลายเดือนก่อน +1

      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??