Replies: 1 comment
-
ViewModel works differently on iOS vs. Android due to the Android Activity lifecycle. When there is a configuration change, such as rotating the device, changing to Dark theme, changing language, or increasing the font size, the Android However, we could make the ViewModels for iOS and Android more similar by implementing a shared Kotlin interface, using a delegate in a similar way, using expect / actual in a shared ViewModel, or by just making the Android ViewModel hold an instance of the shared ViewModel. There are differing perspectives on whether ViewModels should be shared, due to peculiarities of the Android Activity lifecycle. But I'm interested in trying out shared ViewModels using one of the methods above. |
Beta Was this translation helpful? Give feedback.
-
Hi. First of all, thanks for sharing this repository, It's very useful to help people like me to learning about KMP.
I was reading your code and I have a question about the way the structure is built. In your ViewCotroller class (inside iOS module) you guys are importing the shared module and instantiating the NativeViewModel directly from there. I know that making the VM code shared will make a lot things easier here, because it should behaves exactly equals on both platforms. However, in this scenario, you guys don't need anything that is very specific from iOS frameworks, but, for example, in sometimes I'll need something like a UIColor on my ViewModel, and creating the ViewModel inside the shared code instead of in the iOS code makes it impossible. My question here is: should we actually share the ViewModels classes when working with KMP or it's better to restrict the code sharing to only our Domain layer?
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions