Skip to content

Instantly share code, notes, and snippets.

@beechtom
Created October 5, 2023 20:03
Show Gist options
  • Save beechtom/3f9df8fae509722ce93c74bed48df8ca to your computer and use it in GitHub Desktop.
Save beechtom/3f9df8fae509722ce93c74bed48df8ca to your computer and use it in GitHub Desktop.
SectionedQuery-05
import SwiftData
import SwiftUI
@propertyWrapper
struct SectionedQuery<SectionIdentifier, Result>: DynamicProperty
where SectionIdentifier: Hashable, Result: PersistentModel {
private let sectionIdentifier: KeyPath<Result, SectionIdentifier>
@Query private var results: [Result]
var wrappedValue: SectionedResults<SectionIdentifier, Result> {
SectionedResults(sectionIdentifier: sectionIdentifier,
results: results)
}
init<Value>(_ sectionIdentifier: KeyPath<Result, SectionIdentifier>,
filter: Predicate<Result>? = nil,
sort keyPath: KeyPath<Result, Value>,
order: SortOrder = .forward,
animation: Animation = .default) where Value: Comparable {
self.sectionIdentifier = sectionIdentifier
_results = Query(filter: filter,
sort: keyPath,
order: order,
animation: animation)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment