-
Notifications
You must be signed in to change notification settings - Fork 117
[Woo POS] Avoid initial view model unwanted recreation #12799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9649cc0
DI PointOfSaleDashboardViewModel from HubMenu
iamgabrielma 90d5619
Present filter modal view on tap
iamgabrielma 4a5ded6
Make pos vm StateObject
iamgabrielma 4275f7d
Adjust init access to internal
iamgabrielma df0879b
Merge branch 'trunk' into task/pos-compose-viewmodel-outside-view
iamgabrielma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering, would
@StateObject
here fix the issue? Personally, I feel like owning the view model within the POS boundary (as if an invisible framework) would be preferable, and this seems to be the goal ofStateObject
in the doc. The dependencies can be DI'ed through the view's initializer, and the view just does a simple initialization of the view model with the depenencies.@ObservedObject
view model works in the HubMenu because it's being held by the UIKit hosting controller which doesn't get recreated like in a SwiftUI view.If
@StateObject
doesn't work out and the POS view model needs to be DI'ed to the POS view, maybe havingHubMenuViewModel
own it would be more consistent with the existing setup likeInPersonPaymentsMenuViewModel
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean the View declaring its own dependencies via a
@StateObject
rather than DI it from outside? So:It does seem to fix the problem as well, I'm guessing because of:
I like the idea of setting a POS boundary via using StateObject, and the benefits of dependency injection for testing purposes are not lost in this case since is just the initial POS state. I don't really know if there's any other trade-offs we should be aware of?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup could be just
@StateObject private var viewModel: PointOfSaleDashboardViewModel
and setting the view model in the view init given the dependencies.Good point, I can look more into this tomorrow. We've been using
@StateObject
in the code base in views that own a view model throughout the UI's lifetime.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some more reading on the official doc and other posts,
StateObject
feels to me like the most appropriate place for the view to own the view model that's only created once. With a few notes:If we do want the view model to be recreated again when some dependency changes, it looks like we can achieve this by binding the identity of the view that owns the StateObject view model to one or more parameters.
Lemme know what you think!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me! Updated on 4a5ded6 . Now the Hub doesn't know anything about the
pos
viewmodel, but is entirely driven internally by the point of entry.