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

Sergio C.

12/25/2021, 11:00 PM
Using MVVM and clean code how should I create the connections to the ViewModel in the scenario where I need to have the Context to be able to show several Dialogs in a process when the user performs an action? Imagine the user clicks a button to create a document, in this process I need several inputs from the user, for that I need to pass the Context to show a dialog to select items, or choose yes, no or other options etc, creating a document has it’s business logic but the issue is it’s not a one shot operation where the user clicks and waits for the result, it’s a back and forth View to business logic. Passing the Context to the ViewModel it’s the easy way but I think is not a great idea and it should create memory leaks, creating an observer in the activity for every user interaction in the process of creating a document is going to be messy and confusing… Any other good options to solve this? Or a project sample with the same kind of logic?
m

marlonlom

12/27/2021, 1:30 AM
afaik, you dont pass the android framework context into viewmodel (and data layer), thats because of the concept topoc of the framework being isolated from the domain of the solution :S
1
m

manu71

12/27/2021, 10:57 AM
you should notify the UI (fragment, activity) from the ViewModel with an event, for example displayError, then in the observer (fragment / activity) you can use context and display the dialog
s

Sergio C.

12/27/2021, 10:58 PM
Yes. I need to do a subscribe/notify logic. It splits the document creation logic into multiple pieces, but I’ not seeing a better way.
m

Matti MK

12/28/2021, 8:06 AM
Yes. I need to do a subscribe/notify logic
This to me sounds correct. Perhaps an MVI approach for this specific scenario would be suitable?
s

Sergio C.

12/29/2021, 12:47 PM
need to take a look at MVI
can it be integrated with MVVM?
m

Matti MK

12/29/2021, 12:50 PM
Yes, you can use either one, or a mix. You can read on it, for example, here: https://proandroiddev.com/unidirectional-data-flow-on-android-the-blog-post-part-1-cadcf88c72f5
👍 1
s

Sergio C.

12/29/2021, 12:54 PM
cool thanks. I'l take a look
m

Matti MK

12/29/2021, 12:58 PM
Nonetheless, based on your description I believe what you are trying could also be achieved with MVVM, you just need to make sure to keep the state correct. As mentioned above, don’t mix your VM with Context, instead pass events from the VM to the View and let the view handle the how to present the effects of those events
👍 1
s

Sergio C.

12/29/2021, 1:04 PM
yes, passing context to the VW is something I'm trying to avoid. Looking at the pictures in looks like I could separate things by actions, might be a good idea too.
4 Views