I'm fairly new to SwiftUI and I'm currently trying to build an app that fetches event data from the calendar(s). In the app I'm fetching events from the current date/time to the last day of the current week. The app is working fine but there is one thing that I can't seem to solve.
The events are presented in a List view like this:
ForEach(eventsData.events ?? [], id: .self) { event in
EventRow(event: event)
}
The problem is that I would like to "group" each event by the corresponding
weekday, like this:
Monday
Event A
Event B
Tuesday
Event C
Event D
Wednesday
Event E
Event F
One idea I had was to use another ForEach to loop over a unique set of weekdays (id 1-7 and weekday name), present the weekday name in a Section(header:) and then use the id as some kind of filter for my events in a second ForEach (I have created an EKEvent extension which hands me the weekday number for each event).
This might be possible, but the problem is that I don't know how to construct a unique set of weekdays (1-7 with weekday name) that is sorted correctly and that also takes into account that a week can start on Sunday or Monday (depending on where you live).
So, to summarize what I'm looking for (I think):
A set of weekday ids and names (a dictionary?) that is sorted from Monday to Sunday (or Sunday to Saturday), it has to consider the first weekday.
A way to filter my events on weekday in my ForEach (is that even possible?)
I don't know if this makes any sense, I'm new to this :)
Thanks in advance!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…