Michael Langford

01/10/2022, 5:32 PM
I have a list of Packages. When a user taps one, they can edit it. We do this in a dialog. I'm having a bit of an issue handling the indirection. How do I pass the correct package to the viewModel that's associated with what's on the dialog when showing it? The naive way to do so seems like it would literally create a dialog per list item. Creating a view model and creating one dialog, does not seem to later let me change fields on the view model to specify the user selection.
(Apologies about the name, it's a domain term, and has nothing to do with java packages. This is a port from iOS)

Colton Idle

01/10/2022, 6:36 PM
I think it depends on whether or not your dialog is just a dialog in compose or if you are using dialogs as a navigation destination in compose-nav. If it's just a regular dialog, then I would represent the user hitting the edit button as state and as a quick solution I would have a isEditingItem :Int = -1 in my state. Then in my composable screen I would do if (isEditingItem > -1) { MyDialog()... } and then your dialog can access your VM and talk to your domain/package objects.
If I were using compose navigation and the dialog was a destination, then I would put the arg of the package id as a arg in the navigation route, and then the dialog and it's VM would retrieve that id, load it from the database or whatever, and edit it from there. I think out of simplicity I would probably just go the first route where everything is just contained in a single screen and VM. You could argue that on a larger device you could put the editing logic side by side of the list of packages, so keeping the editing logic in the same vm as viewing the packages makes sense to me.