How to reuse view and reducer with more than one model? #1010
Replies: 3 comments 4 replies
-
Can I assume the lack of replies means that it's just not possible to reuse views with different models like this? Really would love some advice or help on this as I really want to give TCA ago, but I'm now considering rolling my own lightweight unidirectional redux style framework. |
Beta Was this translation helpful? Give feedback.
-
The lack of replies may be due to there being a lot of code in your post to understand. Is there a simpler way to explain what you are trying to achieve, and a smaller code snippet that shows the problem you are having? From briefly looking at the point you highlight in the code I'm not sure I understand how the domain is modeled. You have an itemStore: Store<GroupModel, GroupDetailsAction> And you somehow want to transform that into a: Store<GroupModel, EditableItemAction> However, struct GroupModel: EditableItemConformable {
...
var items: IdentifiedArrayOf<ChildModel> = []
} And enum GroupDetailsAction: Equatable {
...
case item(id: ChildModel.ID, action: EditableItemAction)
} Looking at this domain modeling makes it seem impossible to speak about My gut reaction tells me that the domain modeling is just not quite right. It seems the
Curious what features you would include in your framework that are not currently present in TCA. Maybe that would help me and others see the problems you are having. FWIW, the repo has a few examples of reusable components, in case you haven't seen it:
|
Beta Was this translation helpful? Give feedback.
-
I didn't mean I was trying to create an alternative to TCA it was more than some of the complexity I'm struggling with seems to be related to the fact that it's composable but for a simple app like I'm trying to build feels a bit daunting so it might be easier for me to build my own lightweight one-off reducer, store for my specific needs etc as currently using TCA feels like I'm using a sledgehammer to crack a nut but I agree TCA seems pretty feature complete. |
Beta Was this translation helpful? Give feedback.
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'm just starting out with TCA but I am unsure how to create reusable views and reducers that can be used with different models.
As an example, I've created a reusable View that has a reducer factory that allows a different concrete type to be used for the view state as long as it conforms to a protocol:
EditableItemView.swift
Given I have the following models:
Models.swift
Both of those can be used as the state for the
EditableItemView
.Now putting this all together I have two screens that list items, the first screen is a list of "groups" that should have editable titles, the second screen is a list of "child" items that belong to the select group.
GroupsView.swift
The detail screen looks like this:
GroupDetailsView.swift
If you want to try building this you'll need an entry point:
main.swift
The issue I'm having is this code in the GroupsView.swift:
It's unclear to me how to scope the already scoped
itemStore
so that it uses theGroupsViewAction.item2(id:action:)
which is needed because the action isEditableItemAction
which is needed to perform the editing action on theEditableItemView
's reducer.Can anyone advise how I can work with a scoped array using ForEach with two different actions?
I'm wondering if I'm approaching this all wrong but it feels like I should be able to have shared views that have their own reducers that can work with different concrete types as long as they conform to a protocol.
Here is a video of how it looks currently:
Simulator.Screen.Recording.-.iPhone.13.-.2022-02-25.at.18.58.44.mp4
Gist: https://gist.github.com/cameroncooke/fea9126060ec6ad3661ff711e66926ce
Cross-posting from: https://forums.swift.org/t/how-to-reuse-view-and-reducer-with-more-than-one-model/55568
Beta Was this translation helpful? Give feedback.
All reactions