adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.31k stars 109 forks source link

[Question] SwiftUI ScreenModel #267

Open thomaskioko opened 6 months ago

thomaskioko commented 6 months ago

Is it possible to access a ScreenModel in SwiftUI or does my app need to be a compose-multiplaform? I want to keep the UI side native per platform (Android: Compose & iOS SwiftUI) and only share the "ViewModel".

Syer10 commented 6 months ago

I imagine its possible, the main thing with ScreenModels is that they are stored in the ScreenModelStore, if you implement the Screen interface or create a screen with a unique key, I imagine you could add it to the ScreenModelStore manually and manage its lifecycle with whatever iOS lifecycle APIs there are.

thomaskioko commented 6 months ago

Thanks @Syer10. I was hoping it was something supported out of the box. It was pretty easy wiring things up on the Android side (thanks to Tachidesk 😉 ). Anyway, I'll give it a shot and see what happens. Thanks again.

osrl commented 5 months ago

hey @thomaskioko . I was also looking for the same thing myself. Did you make it work? I couldn't even add voyager dependency (which includes ScreenModel) to commonMain because it's dependent to jetpack compose.

thomaskioko commented 5 months ago

Hey @osrl I'm glad to know I'm not the only one who would like this. I just set it up on Android. I am yet to try to hack something out with the suggestion. Hopefully over the weekend.

osrl commented 5 months ago

I'm not sure if that's currently possible. Please let me know if you achieve something

jeancsanchez commented 5 months ago

I'm also in the same situation. Looking forward some solution.

DevSrSouza commented 5 months ago

We have here (where I work) a custom ScreenModel/ViewModel that the Lifecycle is bound on Voyager lifecycle and on iOS, the UiViewController lifecycle. But this is a complete custom implementation that the porpuse is to only call onDispose and free a CoroutineScope based on the platforms lifecycles. I don't see how things can be a fit to Voyager ScreenModel that was designed to work with Compose and the Navigator/Screen lifecycles.

The ScreenModel/ViewModel interface is pretty simple, you can build a custom easily and where you use Voyager, bound to the lifecycle of the Screen by using ScreenLifecycleStore APIs.

https://voyager.adriel.cafe/lifecycle#extending-the-lifecycle-lazily

swapnil-musale commented 4 months ago

@thomaskioko @osrl @jeancsanchez Are you got success to integrate it on iOS side ?

thomaskioko commented 4 months ago

@swapnil-musale, I didn't have much time to try to hack something with the suggestions from folks in the comments. I ended up going with a different library. (decompose)