Hello :wave: Often, especially in complex fragmen...
# android-architecture
v
Hello 👋 Often, especially in complex fragments I really want to break down components of the screen in separate custom views that will hold the view logic instead of overloading the fragment with bunch of stuff. This makes things a bit difficult when I use ViewModels and Hilt, since ViewModels are only intended to be used from Fragment or Activity and using it in any other place requires a workaround. Does anyone had the same difficulties? For everyone that uses Hilt and ViewModel do you use by viewModels() or you’ve built something custom. Would really appreciate to hear everyone thoughts about it. Cheers
s
I'm of the opinion to not use ViewModels or Presenters in (custom) Views. Views are Views and that's it. ViewModels are already part of Activities and Fragments. Tying them to some of your custom Views increases coupling. You could create some custom delegate for a custom View, as if it is a super lightweight Fragment, and that delegate could use ViewModels.
Also, the presence of ViewModels in custom Views messes with the IDE's layout editor.
v
Hmm, I really don’t mind IDE’s layout editor. So you are okay with having a lot of logic inside your Fragment and not breaking it down? I did not want to have multiple ViewModels for each view, but one ViewModel for the Screen, but multiple views that will observe on it so I can break down the views logic
s
Very little logic in my Fragments... It'll just observe emitted UI-model data from the ViewModel and stick it in the appropriate Views.
Apart from some very simple map/translation logic, the Fragment does very little. The real logic is in the ViewModel.
With MVVM, I tend to write ViewModels that are veeery closely aligned with the UI (Fragment/Activity Views), so that the Fragments and Activities have very little code. This make the ViewModel almost like a testable Fragment/Activity.
v
Yes of course, that is the purpose of it. But if you have a screen with a lot of things happening on it and keeping everything in one fragment can be overwhelming. Let’s see an example, imagine a profile page scree where you have multiple tabs, info, feed etc….If we make Info and Feed views woudn’t be good if they can observe from the one ViewModel shared between the ProfileFragment?
s
I'd create separate Fragments to chop up my UI in manageable pieces or I create a wrapper-class/delegate, as if it is a super lightweight Fragment, for each of those pieces. But I'd never put a ViewModel inside a (custom) View. The custom View just will have a plain API to handle its in- and output.
v
Thanks for sharing your thoughts 🙂
👍 1
s
@Viktor Petrovski In this case, I would create a custom views with coupling UI-model and move all of the the ui logic to customviews instead.
v
Thanks for the reply @Saran Akkaraviwat what about the ViewModels and your custom view how would they communicate
s
through only the uimodel and listener interface
v
Interesting, do you have any code example for this I can take a look?
s
v
Thanks i will take a look 🙂
Copy code
fun setupView(
        uiModel: OrderOngoingCardUiModel,
        motionLayoutOnGoingCard: MotionLayout,
        isAnimateOrderOngoingCard: Boolean
    )
Is called from a fragment?
s
yep
fragment / activity
v
Cool, thanks 🙂