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

Viktor Petrovski

04/22/2021, 11:33 AM
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

streetsofboston

04/22/2021, 12:42 PM
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

Viktor Petrovski

04/22/2021, 1:12 PM
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

streetsofboston

04/22/2021, 2:17 PM
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

Viktor Petrovski

04/22/2021, 2:28 PM
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

streetsofboston

04/22/2021, 5:14 PM
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

Viktor Petrovski

04/22/2021, 5:15 PM
Thanks for sharing your thoughts 🙂
👍 1
s

Saran Akkaraviwat

04/26/2021, 4:16 AM
@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

Viktor Petrovski

04/26/2021, 7:33 AM
Thanks for the reply @Saran Akkaraviwat what about the ViewModels and your custom view how would they communicate
s

Saran Akkaraviwat

04/26/2021, 7:34 AM
through only the uimodel and listener interface
v

Viktor Petrovski

04/26/2021, 7:35 AM
Interesting, do you have any code example for this I can take a look?
s

Saran Akkaraviwat

04/26/2021, 7:40 AM
v

Viktor Petrovski

04/26/2021, 7:43 AM
Thanks i will take a look 🙂
Copy code
fun setupView(
        uiModel: OrderOngoingCardUiModel,
        motionLayoutOnGoingCard: MotionLayout,
        isAnimateOrderOngoingCard: Boolean
    )
Is called from a fragment?
s

Saran Akkaraviwat

04/26/2021, 7:59 AM
yep
fragment / activity
v

Viktor Petrovski

04/26/2021, 8:00 AM
Cool, thanks 🙂
2 Views