I'm really getting in a muddle trying to work out how to make this work. I'm creating an app to show a list of people and their birthdays.
I've got my AppState with an var people: IdentifiedArrayOf<Person> = []
.
Where Person
just contains a name
and dob
.
I've got the app showing a list of the people. And I can even tap to open a view that just takes a Person
.
But... I'd like to extend this view to have other computed vars and even be editable etc...
My first attempt was to put extra parameters into Person
like func age() -> String
etc... And then to add a PersonAction
and treat the Person
as a child state of AppState
. But it didn't feel right. Making the Person codable now stores extra stuff to disc like isEditing
etc... which doesn't feel right.
My current idea is to have something like PersonViewState
which takes a Person
and then contains all the other properties.
struct PersonViewState {
var person: Person
var ageString: String
var isEditing: Bool
etc...
}
But... with this I don't really want to change the AppState
to store IdentifiedArrayOf<PersonViewState>
as I'll only be showing one of these at a time (when the user taps the row with the person on).
So I'm just wondering how I can use the IdentifiedArrayOf<Person>
and then have a NavigationLink
which will load a view that takes PersonViewState
.
So...
IdentifiedArrayOf<Person>
-> ForEach(viewStore.people) { person in
-> NavigationLink(destination: { PersonView(store: ...?) }
I'm not sure how to scope this store to the AppState and create the action so that I can edit the Person from this view.
I'd like to create the PersonViewState
in the reducer too so that I can pass in dependencies like Date()
and Calendar.current
and use them to calculate the age at the point that the user taps the row. I'm thihnking from this that I actually don't just need a NavigationLink but a Button that would send an action into the reducer to create the PersonViewState
at the point of the user tapping the button and use an IfLetStore to scope the viewstore?
Sorry if this is all a bit confused but that probably reflects what is in my brain right now.