Replies: 1 comment
-
I guess I will have to use I can see how this will work for the direct parent of the state.path[id: subItemStackId, case: /Path.State.detailSubItem]?.subItem = subItem But I cannot do something like this to mutate the if let index = state.path[id: subItemStackId - 1, case: /Path.State.detailItem]?.item.subItems.firstIndex(where: { $0.id == subItem.id}) {
state.path[id: subItemStackId - 1, case: /Path.State.detailItem]?.item.subItems[index] = subItem
} … cause it result in an error:
I think I am close, but how can I get access to the previous |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am playing around with stack-based navigation. I have a sample project where I have the following structure:
What I have
The
RootView
shows a list of items. I drill down to the detail view of an item with aNavigationLink
.The
itemView
has a titleTextField
and a list of subitems. Tapping on one of the subitems further drills down in the navigation stack to the detail view of this subitem.The
SubItemView
also has a titleTextField
.What I want to accomplish
When drilling down to an item and a subitem, I obviously want changes in the item and/or subitem to get registered by its parent and potentially grand-parent, so that when I pop to root and then drill back down, my mutations are retained.
What I am struggling with
I have it working right now, but I guess there must be a much more elegant way … Right now, when a mutation is done in one of the children, I fire up a
.delegate(.onChange)
action that is observed by root.In root, I then loop over the path to try and see whether the item or subitem with that
ID
is part of the stack element and assign the new value.That means that from the
SubItemView
, when I fire.delegate(.onChange)
, I have to loop over all items in root first, trying to find the one item with this exactSubItem
that was changed, then assign the mutatedSubItem
to its parentItem
. This ensures that the change is reflected inItemView
. After that, I will need to also mutate the array ofItem
s in theItemView
.Here's the code where I do exactly that:
This feels to be more complicated than it should be, so I am wondering whether I am "holding it wrong"
TM Apple
…So I wonder …
… whether there is a better approach for doing what I want to do …
You can find the whole code as a gist here.
I am looking forward to your suggestions, thanks for considering helping me out here … :-)
Beta Was this translation helpful? Give feedback.
All reactions