https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
n

Nacho Ruiz Martin

12/18/2021, 6:47 PM
Hey! 👋 Given a classic MVI approach with Jetpack’s
ViewModel
, how would you share some logic between different
ViewModels
? I have a 1:1 relationship between screens and VM and the latest is in charge of any change to the state. So a lot of logic may be shared, for example the management of a textfield with value update, validation, error setting and such.
w

Will Shelor

12/20/2021, 12:49 AM
There are a few ways to do this. It could be moved to extension methods or helper functions, or it could be time to move to shared ViewModels. Some logic could also be moved to the repository. For your use case as described above, I'd probably recommend extension methods, helper functions, or services.
k

K Merle

12/20/2021, 8:09 AM
Usually I move shared logic layer bellow your ViewModel, if it makes sense. Sharing a ViewModel and both screens not having complete same behavior from ViewModel is against solid principles.
n

Nacho Ruiz Martin

12/20/2021, 10:43 AM
Hey, thanks for your tips! I was thinking about placing some delegate classes there that will handle the state change and each ViewModel can have from 0 to n delegates. I feel that helper/extensions would still introduce a lot of duplicated logic in the VMs. About moving a layer below, it doesn’t makes sense in my way of seeing things, because this is pure view logic, not domain.
4 Views