Don't Do THIS Jetpack Compose Mistake

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ก.ย. 2022
  • ⭐ Get certificates for your future job
    ⭐ Save countless hours of time
    ⭐ 100% money back guarantee for 30 days
    ⭐ Become a professional Android developer now:
    pl-coding.com/premium-courses...
    💻 Let me be your mentor and become an industry-ready Android developer in 10 weeks:
    pl-coding.com/drop-table-ment...
    Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!
    pl-coding.com/newsletter
    Join this channel to get access to perks:
    / @philipplackner
    Join my Discord server:
    / discord
    Regular programming advice on my Instagram page: / _philipplackner_
    Checkout my GitHub: github.com/philipplackner
    You like my free content? Here you can buy me a coffee:
    www.buymeacoffee.com/philippl...

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

  • @PhilippLackner
    @PhilippLackner  ปีที่แล้ว +20

    Because I've read this in many comments now:
    Of course, putting this as state in the ViewModel works, too (and for filtering I'd honestly also do this myself).
    But, that doesn't change the fact that doing this is a mistake (I'm often doing code reviews and saw this one very often). Also, there are scenarios where you can't simply put this in the viewmodel, e.g. If you need the context when mapping something to string resources for example.

  • @maximooze3196
    @maximooze3196 ปีที่แล้ว +1

    Nice vid philipp, I hope you make some shorts about optimizing re-comipistions with "donut scoping", it revolves around the idea of inlining functions

  • @yossimaskin1393
    @yossimaskin1393 ปีที่แล้ว +8

    I prefer another approach in this case.
    I prefer preparing those 2 lists in the VM and move logic to there

  • @tashi7160
    @tashi7160 ปีที่แล้ว +18

    no logic other than a simple if in the ui. let vm drive the state.

    • @PhilippLackner
      @PhilippLackner  ปีที่แล้ว +6

      Yeah with the filter example I chose I'd also probably do that, but there are definitely cases where you can't/shouldn't do this in the ViewModel (imagine mapping something to string resources and you need the context)

    • @ajailani4
      @ajailani4 ปีที่แล้ว

      Agree, because ViewModel acts as a state holder. And I also agree with Philipp, there are another scenarios that we'll be forced to handle the logic in the UI

  • @meidy3795
    @meidy3795 ปีที่แล้ว +2

    This one of the helpful things i need to do to optimize the apps I build, thanks!

  • @CodeWithNiks101
    @CodeWithNiks101 ปีที่แล้ว +1

    It was really helpful, thanks for sharing

  • @kyleMcBurnett
    @kyleMcBurnett ปีที่แล้ว

    Thank you so much. Wish I had known this early

  • @haykmkrtchyan7093
    @haykmkrtchyan7093 5 หลายเดือนก่อน

    Also we can call partition on our list and get both done and undone items list

  • @safionweb
    @safionweb ปีที่แล้ว

    Amazing video :)

  • @st4849
    @st4849 ปีที่แล้ว +3

    I'd just have an unmutable list/map of boolean states in the ViewModel that can be accessed/modified via public methods. That way there's no filtering of any sort and only the item(s) that changed will recompose.

    • @PhilippLackner
      @PhilippLackner  ปีที่แล้ว +1

      That's super unflexible and unreadable though

    • @st4849
      @st4849 ปีที่แล้ว +1

      @@PhilippLackner You clearly prefer to keep the ViewModel clean and I prefer to keep the UI code simple and clean. As that's what you need for best performance. I wouldn't consider doing anything you demonstrated here in my UI code. I thoroughly enjoy watching your videos though and I learned many important things here that I value very much.

  • @JustAmalll
    @JustAmalll ปีที่แล้ว +1

    Hi Phillip, how can i set toolbar color like bootom bar and status bar color like toolbar color ?

  • @carbaj03
    @carbaj03 ปีที่แล้ว +4

    Thank you for sharing this information, it´s very helpful.
    I think you could take the opportunity to say that this kind of logic shouldn't be in your UI framework.

    • @PhilippLackner
      @PhilippLackner  ปีที่แล้ว +1

      There's no general rule that this shouldn't be in the UI (considering that the VM is also part of the UI). Putting it in the VM solves this as well of course.

  • @fars7346
    @fars7346 5 หลายเดือนก่อน

    after applying this my UI loaded more smoothly, cool.

  • @premi_uz
    @premi_uz 6 หลายเดือนก่อน

    thanks

  • @wagnerarcieri
    @wagnerarcieri ปีที่แล้ว +1

    I would be very happy if your courses could be priced by Region.... In Brazil, 99 euros are equal to half a month of paid work... and been unemployed is tricky

  • @patrickrodriguez4683
    @patrickrodriguez4683 ปีที่แล้ว

    Thanks

  • @Joe-qv2jo
    @Joe-qv2jo ปีที่แล้ว

    Can you pls create a video about libraries android provides and some history about different android version changes as it is difficult for a new developers to know what is new and what is not. Thank you

  • @Walker32102
    @Walker32102 ปีที่แล้ว

    Hey, Philipp.
    Can you do a video for Rich text editor like most note application, please?

  • @anudeepananth
    @anudeepananth ปีที่แล้ว +1

    Your expressions in the thumbnail :)

  • @Alchemist10241
    @Alchemist10241 10 หลายเดือนก่อน

    this was exactly what I needed without this I had to recompose a lazy vertical grid in an if else block

  • @mesutemrecelenk5447
    @mesutemrecelenk5447 ปีที่แล้ว

    I made every mistake you said don't do it 😂😂😂. Thanks Philipp

  • @mustafaammar551
    @mustafaammar551 ปีที่แล้ว

    very cool
    you are the best

  • @jepp25907
    @jepp25907 ปีที่แล้ว +1

    Really enjoy your videos! I've been doing Android apps as a hobby for a year or so now and your videos have been a great help!
    Personally, i come from a web-development job so i'm by no means and Android expert. I'm wondering though, stuff like filtering the todos list and generally preparing data for the presentation layer, shouldn't that logic be done in the viewmodel or elsewhere? In my opinion and experience, the UI-layer shouldn't really do any other logic than implementing the UI, and the preparation of data shouldn't be the job of the UI-layer. Again, i'm by no means an app-developer so i'm sure i havent got the best understanding of good app architecture. Would appreciate your thougts on this!

    • @karmus12
      @karmus12 ปีที่แล้ว

      Agree. Filter in the viewmodel and then expose isDone and notDone in state and observe from the ui.

  • @wagnerarcieri
    @wagnerarcieri ปีที่แล้ว

    niceee !

  • @ChrisAthanas
    @ChrisAthanas ปีที่แล้ว +1

    Please always give the source code link!

  • @muhmmedalsadig7706
    @muhmmedalsadig7706 ปีที่แล้ว

    I think this type of logic should be in vm
    And your vm avoid recompostion easily

  • @aliisazadeh6973
    @aliisazadeh6973 ปีที่แล้ว

    continue the KMM on youTube please

  • @oliver_lauritsen_oa
    @oliver_lauritsen_oa ปีที่แล้ว +1

    The whole compose is a mistake!

  • @cularu1
    @cularu1 ปีที่แล้ว

    did you met that Tinder girl?

  • @SriHarshaChilakapati
    @SriHarshaChilakapati ปีที่แล้ว +1

    Would love to know your thoughts regarding this: I've been thinking for a while now, but still I'm not fully convinced that Jetpack Compose is actually an improvement compared to existing View system. When we separate the complexity into two ways, API complexity and implementation complexity, I divide them as follows:
    Android View System - High API Complexity & Medium Implementation Complexity
    Android View System + LayoutInflater - Low API Complexity & Medium Implementation Complexity
    Jetpack Compose - Low API Complexity & High Implementation Complexity
    API Complexity is the complexity of the interface that is exposed to app developers. Here, Compose definitely has got low API complexity, but under the hood, the complexity increases manyfold because one has to understand recompositions and also how the compiler is rewriting the Composables.
    Just the Android View System has high API complexity (I'd argue it is also medium complexity though) as laying out views in code is not that straight forward, XML actually reduces this to low with the use of LayoutInflater. You define layouts declaratively (one time at the layout, and most views don't simply change their positions anyway unless animated) so declarative UIs are actually achieved.
    This reminds me constantly that if in case some bug happens in Compose, say there are too many recompositions, or an edge case in animation which is causing another view to displace or change dimensions, people are gonna have to spend even more time trying to identify the cause of it. This, requires people to understand the implementation, which definitely has a high complexity.
    Would love to know your thoughts. If you think otherwise, I request you to please make a video about this.

  • @dekus80
    @dekus80 ปีที่แล้ว

    I think UI must not do and filters, just show. First part no words, somebody do so? (facepalm) and I don't like "correction" with remaining logic in UI too.

  • @ayoubkhabib
    @ayoubkhabib ปีที่แล้ว

    Roadmap >>>>>> android developer,,,, please?

  • @georgemujuru1232
    @georgemujuru1232 ปีที่แล้ว

    First

  • @andrewdunbar828
    @andrewdunbar828 9 หลายเดือนก่อน

    Don't Make this English mistake of saying "do a mistake".

  • @clamum9648
    @clamum9648 ปีที่แล้ว

    Say I have a screen with 6 textboxes that the user can enter info into. Normally I'd see this represented as far as saving state by having 6 different state variables, one for each textbox (or really, two variables for each textbox, a private one in the ViewModel and a public one in the ViewModel, that the Composeable can access).
    Couldn't I also make a single class, like "MyState", that has 6 fields in it, and then manage my screen's state by just referring to that single state variable that my ViewModel now contains?
    So for example I'd just be referencing MyState.myVarOne, MyState.myVarTwo, ..., MyState.myVarSix rather than myVarOne, myVarTwo, ..., myVarSix. Hopefully that makes sense.