hi,
you'd first want to group the items so that items with timestamps on the same day are listed together. having a computed variable of this form would help:
var groups: [[Item]] {
// do the grouping here ... your example of five items (three on 20/02/2023
// and two on 19/02/2023) would be returned as an array of arrays:
// [ [Item, Item, Item], [Item, Item] ]
}
if you get this done, then your SwiftUI view would become
ForEach(groups, id: \.self) { group in
Section(header: Text(title(for: group))) {
ForEach(group, id: \.self){ item in
Text("\(item.title!)")
}
}
}
to figure out the title for the group is easy: all items in the group occurred on the same day, so just look at the first item in the group:
private func title(for group: [Item]) -> String {
String(group[0].timestamp!.formatted(.dateTime.day().month().year()))
}
to do the actual grouping above, you can start with a dicitionary function to group the items by the start of the day of the timestamp. each key will be a start of day time, and each value will be an array of Items whose timestamp has that start of day time. use the result to then combine the dictionary values into an array where the elements are of type [Item]
var groups: [[Item]] {
let dictionary: [ Date : [Item] ] = Dictionary(grouping: items, by: { Calendar.current.startOfDay(for: $0.timestamp!) })
var arrayOfArrays = [[Item]]()
for key in dictionary.keys.sorted() {
arrayOfArrays.append(dictionary[key]!)
}
return arrayOfArrays
}
i think that will do it ... UPDATE: code above has been checked; my original code was certainly aspirational but a little sloppy, but hey, it's Friday (!)
hope that helps,
DMG