How to Share ViewModels in Compose Multiplatform (with Dependency Injection!)

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 พ.ค. 2024
  • In this video I'll show you how to share ViewModels in Compose Multiplatform (with Dependency Injection!), enjoy!
    💻 Let me be your mentor and become an industry-ready Android developer in 10 weeks:
    pl-coding.com/drop-table-ment...
    ⭐ Courses with real-life practices
    ⭐ Save countless hours of time
    ⭐ 100% money back guarantee for 30 days
    ⭐ Become a professional Android developer now:
    pl-coding.com/premium-courses...
    Get my FREE PDF about 20 things you should never do in Jetpack Compose:
    pl-coding.com/jetpack-compose...
    Regular programming advice on my Instagram page: / _philipplackner_
    Join my Discord server:
    / discord
    Get the source code for this video on GitHub:
    github.com/philipplackner/Jet...
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @Fo0nT
    @Fo0nT 2 หลายเดือนก่อน +46

    Next video: Room for KMP

  • @ahmadhamwi4081
    @ahmadhamwi4081 2 หลายเดือนก่อน +13

    Thank you for your efforts into making this video! Really appreciate it!

  • @user-vs9ey3bd4t
    @user-vs9ey3bd4t หลายเดือนก่อน

    Best youtuber ive found, literally you cant set up complete kotlin kmp app, with most simple as posible

  • @olwethupissot
    @olwethupissot 2 หลายเดือนก่อน +6

    Hey Philipp, I just wanted to express the Kotzilla team's gratitude for this incredible video! Your tutorial is super informative. Thanks so much also for sharing your experience with Koin😊.

  • @skarloti
    @skarloti 2 หลายเดือนก่อน +3

    You are like the great Arnold!
    Impressed by the short presentation, but the top of the future of programming. 🎉

    • @skarloti
      @skarloti 2 หลายเดือนก่อน +1

      Your smile conveyed satisfaction at Kotlin's progress. It was an important part of the presentation.

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

    Damm, and here I was wasting time getting Voyager to work when this is already officially supported. Saved me a headache and some dev time, as always! Keep up the good work

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

    Pretty straight forward. Thanks for your efforts!

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

    Thank you for the helpful videos.

  • @jeet5401
    @jeet5401 2 หลายเดือนก่อน

    Really Helpful

  • @mahmoudaljorban540
    @mahmoudaljorban540 2 หลายเดือนก่อน

    Thanks for a great video :)

  • @roshkaalex
    @roshkaalex 2 หลายเดือนก่อน +6

    Thank you Philipp! Still one question is. How to share one instance of viewmodel between compostable functions in nav graph? And how to pass arguments in viewmodel +koin

    • @ddaypunk
      @ddaypunk 2 หลายเดือนก่อน +1

      Given the vm is a part of the koin graph, wouldn't you just get it similarly in that nav composable with the same type? Koin would then provide the current instance if singleton.

  • @farhazulislam6101
    @farhazulislam6101 2 วันที่ผ่านมา

    Hey Lackner, you defined koin module as single { } aka singleton, so every time if you try to inject via inject() or koinInject() in composables it will always get the same reference of ViewModel. You don't require to get it from the scope. koinInject will do that for yor.

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

    Should I learn some IOS basics like we had in Android Basics before starting KMP?
    Does IOS have intents?, Broadcast?

  • @joeybasile1572
    @joeybasile1572 28 วันที่ผ่านมา

    16:36 you was diggin wontcha

  • @frank4pickerson
    @frank4pickerson 2 หลายเดือนก่อน

    Wow! It's super easy to use CMP now because of this. At the last 3 minutes before end of video, you showed how to fetch the VM: composable("screenA") {val viewModel = koinViewModel()....
    But this doesn't work in printing Hello World! unless you fetch the VM before the composable block

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

    But how to use this KMP view model in Swift (swift-ui or UIViewController) ?

  • @superhumandose
    @superhumandose 2 หลายเดือนก่อน

    Hey Phillip, can you do another updated Jetpack Crash course. There is literally very limited content on TH-cam on that

  • @tch.777
    @tch.777 2 หลายเดือนก่อน

    Thank you, amazing like always ❤
    Is there a way to create a shared koin vm that is attached to the current navigation graph like you showed it with hilt?

  • @bitwisedevs469
    @bitwisedevs469 26 วันที่ผ่านมา +1

    How about with SavedStateHandle? Is it straight forward SavedStateHandle() just like when we create ViewModel with SavedStateHandle in unit test? Thanks

  • @vladalexeev8529
    @vladalexeev8529 2 หลายเดือนก่อน +3

    This video is right what I needed as I'm making KMP project with viewmodels and jetpack nav. For whatever reason my latest Android Studio doesn't show me preview in composable functions of KMP project, although ui tooling is in gradle.

    • @PhilippLackner
      @PhilippLackner  2 หลายเดือนก่อน +1

      Preview for CMP is not supported in Android studio yet

    • @Kmg403
      @Kmg403 2 หลายเดือนก่อน

      What I've done is make a preview file in the Android package and it holds all of the previews for all of my screens. They could be split to separate files, but I decided to do it this way and use the gallery view to look at specific ones.

  • @galaxiegalaxie5003
    @galaxiegalaxie5003 2 หลายเดือนก่อน

    Great content. Awesome. Now, how could we share the same viewmodel to different screen. Could you show us some insight.Thank you.

  • @DenisGL92
    @DenisGL92 2 หลายเดือนก่อน

    Like all your videos, this one is very interesting: thanks! But if we want to keep UI in Swift for iOS, how can we use these KMP ViewModels from Swift?

  • @jasmeetsinghchawla3755
    @jasmeetsinghchawla3755 2 หลายเดือนก่อน

    Hey Philip can u make a video regarding the new changes in the google signin using firebase ?

  • @georgp.2990
    @georgp.2990 2 หลายเดือนก่อน +1

    is this compatible with use of the decompose library, or would you not mix these two?

  • @user-pj4kb1dl3w
    @user-pj4kb1dl3w 2 หลายเดือนก่อน +1

    That is why I prefer flutterr. I mean I love kotlin and Jetpack Compose, but the KMP is pretty new, so the ecosystem is not ready for real production and you need to solve everything by youself.

    • @PhilippLackner
      @PhilippLackner  2 หลายเดือนก่อน +1

      KMP is pretty ready, just Compose MP not yet

  • @Chirag-Redij38
    @Chirag-Redij38 2 หลายเดือนก่อน +1

    How to use viewmodel & koin injection with native SwiftUI.

  • @hafizmohd013
    @hafizmohd013 2 หลายเดือนก่อน

    @Philipp can you share on how use pagination in composeApp Multiplatform. So far, cash app library however this library soon not will maintain.

  • @pablovaldes6022
    @pablovaldes6022 2 หลายเดือนก่อน

    Looking great already
    I wonder if disabling Activity re-creation when configuration changes(in the manifest), will make this easier and we wouldn't need to expect/actual ViewModel modules and classes.

    • @PhilippLackner
      @PhilippLackner  2 หลายเดือนก่อน

      Then you'll break themes, language switching, landscape ui etc.

    • @pablovaldes6022
      @pablovaldes6022 2 หลายเดือนก่อน

      Doesn't that get recomposed when the respective staticCompositionLocal gets updated, regardless of Activity recreation?
      The whole point of compose is being reactive

    • @ibrahimanimasahun9431
      @ibrahimanimasahun9431 2 หลายเดือนก่อน

      @@pablovaldes6022 you're right

  • @malschauen6267
    @malschauen6267 2 หลายเดือนก่อน

    Helpful info. This would be a nice 🌞addition to incorporate into your premium course "Building Industry-Level Multiplatform Apps With KMM" (could also be renamed KMM ---> KMP). Are any updates planned? Some policy would be useful to know.

  • @StyX_CharLEE
    @StyX_CharLEE 27 วันที่ผ่านมา

    if you added dependency to commonMain you don't need to make it API to be available in other sourceSets

  • @manojshivhare937
    @manojshivhare937 2 หลายเดือนก่อน

    can you please suggest me any tutorial or post where i can find that how we can write business logic in shared file then we can generate .Framework file / .arr file to distribute into Android / iOS project to use that business logic separately.

  • @Revitalish
    @Revitalish 2 หลายเดือนก่อน

    where is the vid you made on shared ViewModel in compose navigation in native android? i cant find it anywhere :(

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

    Hey Phillip, is your paid course on KMM updated with these new libraries and developments in KMM?

    • @PhilippLackner
      @PhilippLackner  2 หลายเดือนก่อน +1

      I just recorded some update videos this week, they'll be published in the next 7-10 days after they're edited :) There's a video that shows how to update the project to run on the latest Gradle with all the latest dependencies and there are videos about whether you should use CMP and Jetpack ViewModels. Those are not used in the course for good reason at this point which is all explained though.

    • @anudeepananth
      @anudeepananth 2 หลายเดือนก่อน

      @@PhilippLackner Just purchased your KMP course :)

  • @events4089
    @events4089 2 หลายเดือนก่อน

    Bro please do a video on Room database for kmp

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

    Can't we use KMP in windows?

    • @PhilippLackner
      @PhilippLackner  2 หลายเดือนก่อน +1

      Yes

    • @ddaypunk
      @ddaypunk 2 หลายเดือนก่อน

      In the KMP wizard he mentions at the start of the video, you can add the desktop flavor. That would run on Windows since JVM runs on Windows.

  • @frank4pickerson
    @frank4pickerson 2 หลายเดือนก่อน

    Running it on iOS after cloning, gives me the following:
    Duplicate Library Names:
    library_commonMain
    lifecycle-viewmodel_commonMain
    annotation_commonMain

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

    Great video as usual, but after this video, I think it will be a long time before KMM becomes better than Flutter.
    Too much code, in my opinion, to do easy things like initialize a vm.

  • @danineira2
    @danineira2 2 หลายเดือนก่อน

    One question, do you recommend a MacBook air m3 16Gb to develop Apps with KMP? Is it enough to use Android Studio XCode with different emulators? Or it is worth jumping to the 18Gb macbook pro m3 pro. Thank you very much for your videos.

    • @MultiDeivas
      @MultiDeivas 2 หลายเดือนก่อน

      Get a m2 with 32gb ram better. These development applications are ram hogs, especially with emulators.

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

    Koin is not dependency injection! Koin is service locator!

    • @PhilippLackner
      @PhilippLackner  2 หลายเดือนก่อน +1

      Dependency injection = passing objects to objects
      Exactly what's happening here in the VM's constructor ;)

    • @MonichGPT
      @MonichGPT 2 หลายเดือนก่อน +1

      @@PhilippLackner DI and SL it different patterns. Please read about it. You miss the point: DI is not passing objects, DI it is about getting objects and SL it is about taking objects.

    • @ddaypunk
      @ddaypunk 2 หลายเดือนก่อน

      @@MonichGPT You are arguing semantics here. Koin is listed on their site as a "DI Framework". It is an abstraction over the concept, telling you WHAT, but not HOW it is done.

    • @MonichGPT
      @MonichGPT 2 หลายเดือนก่อน

      @@ddaypunk Koin does not provide DI

    • @deviantstudio
      @deviantstudio 2 หลายเดือนก่อน

      who told you that lol? so injecting into the constructor is SL? something new here

  • @maksym1266
    @maksym1266 2 หลายเดือนก่อน +1

    Why do you use a 'single' for ios but not a 'factory'?

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

      Factory will generate a new instance for every call. Single will generate only one instance (Singleton)

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

    Would this approach work when the UI is not shared?
    I mean:
- XML/Compose on Android
    - swiftUI on iOS
    - bussines logic to be shared across both platform (including the view models)
    I’m trying to understand if a shared view model will work in this case.

  • @frank4pickerson
    @frank4pickerson 2 หลายเดือนก่อน

    It's neat that live templates fills the funs. But why live template don't show for Android Studio on latest Macbook Pro. I am using experimental Android Studio- Canary?

    • @FahadAyaz
      @FahadAyaz 2 หลายเดือนก่อน +1

      I think a lot of that was Gemeni and not live templates

  • @sameershelar2000
    @sameershelar2000 2 หลายเดือนก่อน

    Can I use this ViewModel without Compose Multiplatform?
    I want to do a similar implementation you did in your KMM course. Shared View Model with nativeUI.

  • @andreaslinis1586
    @andreaslinis1586 2 หลายเดือนก่อน

    Hello Philip, is it safe to use compose multiplatform for migration òf an already existing production android project to ios, or do you think it is not stable enough

    • @juandurini309
      @juandurini309 2 หลายเดือนก่อน +1

      Compose Multiplatform is still in alpha, so thing might change in the future and that could lead to deprecations and big refactors in the future

    • @FahadAyaz
      @FahadAyaz 2 หลายเดือนก่อน +1

      On the other hand Compose Multiplatform will generally align well with Jetpack Compose, so if you're using that, you're probably going to be fine (although might have to wait a bit for newer stuff to get merged in)

    • @andreaslinis1586
      @andreaslinis1586 2 หลายเดือนก่อน +1

      @FahadAyaz compose multiplatform just went beta for ios !! That's great news

  • @rajvirsingh2741
    @rajvirsingh2741 2 หลายเดือนก่อน

    Can anyone help me out for my DSA preparation as I currently use kotlin for my Android dev and I am fluent in using kotlin, so should I use the same for my DSA preparation or should I go for Java or c++

    • @malschauen6267
      @malschauen6267 2 หลายเดือนก่อน

      Why not haskell? I would try something new. But I am not ready to help you. Good luck. 🍧🌟

  • @safionweb
    @safionweb 2 หลายเดือนก่อน

    Have they merged KMP and CMP? I remember KMP was different where you had to use Swift for iOS-specific code.
    This tutorial is Compose Multiplatform but Phillip is saying KMP. So, maybe I missed something, is KMP and CMP the same thing now?

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

      Will CMP work without KMP? CMP only means sharing your UI (Compose code in this case) among all platforms, the rest is KMP.

    • @safionweb
      @safionweb 2 หลายเดือนก่อน

      @@osisuper98 I thought KMP generates purely native apps for both iOS and Android as if they were using Android Studio and XCode respectively. And CMP generates non-native apps just like Flutter and ReactNative.
      If that is true, I don't think we can say that CMP = KMP + Shared UI.

    • @FahadAyaz
      @FahadAyaz 2 หลายเดือนก่อน +1

      Compose Multiplatform is built on top of Kotlin Multiplatform. You can choose to share UI with CMP or not share UI and use each platform's native implementations. It's up to you to decide what works best for your project.

    • @safionweb
      @safionweb 2 หลายเดือนก่อน

      @@FahadAyaz Thanks.

  • @arozendojr
    @arozendojr 2 หลายเดือนก่อน

    Suggestion, show how compose-ios works, compare with how React-native or Flutter works, write once and run on both emulators

  • @devgabriel6898
    @devgabriel6898 2 หลายเดือนก่อน

    Going nicely! But still too godamn complex. I will wait until they have something that does everything in this nice video automatically

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

    is compose multiplatform still alpha in ios side ?

    • @malschauen6267
      @malschauen6267 2 หลายเดือนก่อน

      Hm, really quite difficult to find this information on the wide wide web. So might be easier to ask in a YT comment. Let me help you: I would say maybe. 🐣

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

      ​@@malschauen6267 I guess sarcasm and dismissiveness are easier than actually trying to be helpful

    • @meidy3795
      @meidy3795 2 หลายเดือนก่อน

      Now it's beta

  • @MonichGPT
    @MonichGPT 2 หลายเดือนก่อน +1

    You ever getting ViewModel by hands! This is not DI!!! It is not even verify the dependency graph!!!

  • @zachbearse2639
    @zachbearse2639 2 หลายเดือนก่อน

    Doesn't the singleton iOS ViewModel defeat the point? You're throwing away all the scoping apis that make this cross platform view model so useful in the first place.

  • @DarrenHinderer
    @DarrenHinderer 2 หลายเดือนก่อน

    ko-in? lol why not pronounce like "coin"

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

      Koin = kotlin inject

    • @barfyman3Six2
      @barfyman3Six2 2 หลายเดือนก่อน +1

      @@PhilippLackner It is pronounced "coin".

  • @superkobke
    @superkobke 2 หลายเดือนก่อน +1

    Do you really need to have cringe thumbnails to capture your audience? That's so clickbait. I really like your videos man, but jesus, go one step up!!

  • @mohegyux4072
    @mohegyux4072 2 หลายเดือนก่อน

    I reaaly hate viewmodels and these design patterns, I just use Object classes, lives forever and access it anywhere, I know it's "wrong" but it's easier for my tiny brain, I seperate concerns on my own and make single sources of truth on my own, all in singleton objects

    • @JeffreyLiu8
      @JeffreyLiu8 2 หลายเดือนก่อน

      Lives forever may have potential memory issue, and you would have to write specific code to cancel work for some screens based on requirements.

    • @mohegyux4072
      @mohegyux4072 2 หลายเดือนก่อน

      ​@@JeffreyLiu8 True, I do handle cancelling work , and I always use try catch just in case and log the errors to my backend. for me this makes sense esp. for simple apps and for one-dev apps

    • @PhilippLackner
      @PhilippLackner  2 หลายเดือนก่อน +1

      One process death and your app will misbehave. Don't do that :D

    • @mohegyux4072
      @mohegyux4072 2 หลายเดือนก่อน

      @@PhilippLackner ​ may be make a video "why we need viewmodels?"

    • @mohegyux4072
      @mohegyux4072 2 หลายเดือนก่อน

      @@PhilippLackner for example I have an object named 'ui' that contains some mutable state flows like "button1_visible" , "current_text" etc, why should I put them in a viewmodel and not in a simple singleton ?