Skip to content

Instantly share code, notes, and snippets.

@rock3m
Last active July 18, 2022 22:50
Show Gist options
  • Save rock3m/ee925b14ece3eca01b81868271a0c7df to your computer and use it in GitHub Desktop.
Save rock3m/ee925b14ece3eca01b81868271a0c7df to your computer and use it in GitHub Desktop.
Example of using SectionedFetchQuery
struct WorkHistoryView: View {
@SectionedFetchRequest<String, WorkHistoryEntry>(
sectionIdentifier: \.createdAtDate,
sortDescriptors: [SortDescriptor(\.createdAt, order: .reverse)]
)
private var historyEntrySections: SectionedFetchResults<String, WorkHistoryEntry>
var body: some View {
List {
ForEach(historyEntrySections) { historyEntrySection in
Section(header: Text(historyEntrySection.id)) {
ForEach(historyEntrySection) { historyEntry in
WorkHistoryEntryView(historyEntry: historyEntry)
}
}.headerProminence(.increased)
}
}.navigationTitle("history_title")
}
}
extension WorkHistoryEntry {
@objc
var createdAtDate: String {
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.doesRelativeDateFormatting = true
return formatter.string(from: self.createdAt!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment