Skip to content

Instantly share code, notes, and snippets.

View beechtom's full-sized avatar

Tom Beech beechtom

View GitHub Profile
@beechtom
beechtom / ContentView.swift
Last active October 5, 2023 20:24
SectionedQuery-07
import SwiftData
@Model
class Item {
@Attribute(.unique) var name: String
var kind: String
}
@beechtom
beechtom / ContentView.swift
Last active October 5, 2023 20:24
SectionedQuery-06
import SwiftData
@Model
class Item {
@Attribute(.unique) var name: String
var kind: String
}
@beechtom
beechtom / SectionedQuery.swift
Created October 5, 2023 20:03
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]
@beechtom
beechtom / SectionedQuery.swift
Created October 5, 2023 19:56
SectionedQuery-04
import SwiftUI
import SwiftData
@propertyWrapper
struct SectionedQuery<SectionIdentifier, Result>
where SectionIdentifier: Hashable, Result: PersistentModel {
private let sectionIdentifier: KeyPath<Result, SectionIdentifier>
@Query private var results: [Result]
@beechtom
beechtom / SectionedQuery.swift
Last active October 5, 2023 20:02
SectionedQuery-03
import SwiftData
import SwiftUI
@propertyWrapper
struct SectionedQuery<SectionIdentifier, Result>
where SectionIdentifier: Hashable, Result: PersistentModel {
private let sectionIdentifier: KeyPath<Result, SectionIdentifier>
private var results: [Result]
@beechtom
beechtom / SectionedQuery.swift
Last active October 5, 2023 19:33
SectionedQuery-02
import SwiftData
import SwiftUI
@propertyWrapper
struct SectionedQuery<SectionIdentifier, Result>
where SectionIdentifier: Hashable, Result: PersistentModel {
private let sectionIdentifier: KeyPath<Result, SectionIdentifier>
private var results: [Result]
@beechtom
beechtom / SectionedQuery.swift
Last active October 5, 2023 19:33
SectionedQuery-01
import SwiftData
import SwiftUI
@propertyWrapper
struct SectionedQuery<SectionIdentifier, Result>
where SectionIdentifier: Hashable, Result: PersistentModel {
}
@beechtom
beechtom / SectionedResults.swift
Created October 5, 2023 07:40
SectionedResults-10
import SwiftData
struct SectionedResults<SectionIdentifier, Result>: RandomAccessCollection
where SectionIdentifier: Hashable, Result: PersistentModel {
// ...
init(sectionIdentifier: KeyPath<Result, SectionIdentifier>,
results: [Result]) {
let groupedResults = Dictionary(grouping: results) { result in
@beechtom
beechtom / Array+Ext.swift
Created October 5, 2023 07:37
SectionedResults-09
extension Array where Iterator.Element: Hashable {
func uniqued() -> [Element] {
var seen: Set<Iterator.Element> = []
return filter { seen.insert($0).inserted }
}
}
@beechtom
beechtom / SectionedResults.swift
Last active October 5, 2023 07:44
SectionedResults-08
import SwiftData
struct SectionedResults<SectionIdentifier, Result>: RandomAccessCollection
where SectionIdentifier: Hashable, Result: PersistentModel {
typealias Element = Self.Section
typealias Index = Int
typealias Iterator = IndexingIterator<Self>
var elements: [Element]