forked from Whiffer/SwiftUI-Core-Data-Test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeExamples.swift
195 lines (177 loc) · 7.27 KB
/
CodeExamples.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// CodeExamples.swift
// Shared
//
// Created by Chuck Hartman on 7/4/20.
//
import SwiftUI
import CoreData
// MARK: CoreDataDataSource Examples of Common Usage Patterns
// MARK: Examples that use an NSFetchedResultsController
// Using State to control sort order
struct ItemsView : View {
// Data source with a default fetch request for all Item's
@ObservedObject private var dataSource = CoreDataDataSource<Item>()
// State var to control the sort order of the fetch request
@State private var sortAscending: Bool = true
var body: some View {
List {
// List shows all Item objects using current sort state
ForEach(self.dataSource
.sortAscending1(self.sortAscending)
.objects) { item in
Text("\(item.name)")
}
}
}
}
struct ItemAttributesView : View {
// Object passed in to be used as predicateObject
var item: Item
// Data source to fetch all Attribute's related to the Item passed in
@ObservedObject private var dataSource = CoreDataDataSource<Attribute>(predicateKey: "item")
var body: some View {
Form {
Text("\(item.name)")
Section(header: Text("Attributes")) {
// List shows all Attribute objects related to Item
ForEach(self.dataSource
.predicateObject(item)
.objects) { attribute in
Text("\(attribute.name)")
}
}
}
}
}
struct AllAttributesByItemView: View {
// Data source to fetch All Attribute's and group them by Item
@ObservedObject private var dataSource = CoreDataDataSource<Attribute>(sortKey1: "item.order",
sortKey2: "order",
sectionNameKeyPath: "item.name")
var body: some View {
List {
// The sections outer ForEach loop provides the section (i.e. Item) names
ForEach(self.dataSource.sections, id: \.name) { section in
Section(header: Text("Attributes for: \(section.name)")) {
// The objects inner ForEach loop provides all of the Attribute objects for the section's Item
ForEach(self.dataSource.objects(inSection: section)) { attribute in
Text("\(attribute.name)")
}
}
}
}
.listStyle(GroupedListStyle())
}
}
struct DynamicPredicate: View {
// Data source with a default fetch request for all Attribute's
@ObservedObject private var dataSource = CoreDataDataSource<Attribute>()
// State var for TextField input
@State private var searchText = ""
var body: some View {
VStack {
TextField("Enter Search Text", text: self.$searchText)
List {
Section(header: Text("Search Results for: '\(self.searchText)'"))
{
// Rebuilds the NSPredicate each time self.searchText changes
ForEach(self.dataSource
.predicate(NSPredicate(format: "name contains[c] %@", self.searchText))
.objects) { attribute in
Text("\(attribute.name)")
}
}
}
}
}
}
struct ChangingSortKey: View {
// Data source with a default fetch request for all Attribute's
@ObservedObject private var dataSource = CoreDataDataSource<Attribute>()
// State var to specify searching by name or by order
@State private var sortByOrder = true
var body: some View {
List {
Section(header: Text("Search Results by: '\(self.sortByOrder ? "order" : "name")'"))
{
// List shows all Attribute objects related to Item sorted by order or name
ForEach(self.dataSource
.sortKey1(self.sortByOrder ? "order" : "name")
.objects) { attribute in
Text("\(attribute.name)")
}
}
}
}
}
struct CoreDataViewer: View {
// Object passed in to be used as predicateObject
var entityDescription: NSEntityDescription
// Data source with a default fetch request for all Attribute's
@ObservedObject private var dataSource = CoreDataDataSource<NSManagedObject>()
var body: some View {
List {
Section(header: Text("All of: \(self.entityDescription.name!)")) {
// List shows all objects related to the entity description that was passed in
ForEach(self.dataSource
.entity(self.entityDescription)
.objects, id: \.self) { managedObject in
Text("\(managedObject.objectID)")
}
}
}
}
}
// MARK: Examples that do not use an NSFetchedResultsController
extension Item {
// Return a simple count of all Item objects
class func countItems() -> Int {
return CoreDataDataSource<Item>().count
}
// Return an array of all Item objects
class func allItemsInOrder() -> [Item] {
return CoreDataDataSource<Item>().fetch()
}
// Return an array of all Item objects where 'selected' is true
class func selectedItems() -> [Item] {
return CoreDataDataSource<Item>(predicate: NSPredicate(format:"selected = true")).fetch()
}
}
// MARK: Example that uses both NSFetchedResultsController methods and non-NSFetchedResultsController methods
struct ItemAttributesListView: View {
// Item passed in to be used as predicateObject
var target: Item
// Data source to fetch all Attribute's related to the Item passed in
// Group Attribute's for the Item by attributeGroupType
@ObservedObject private var dataSource =
CoreDataDataSource<Attribute>(sortKey1: "attributeType.attributeGroupType.order",
sortKey2: "attributeType.order",
sectionNameKeyPath: "attributeGroupTypeName",
predicateKey: "item")
var body: some View {
List {
// count will provide the number of objects that would be returned to the dataSource
if self.dataSource
.predicateObject(self.target)
.count > 0 {
// Should use the same datasource modifiers as used by the count
ForEach(self.dataSource
.predicateObject(self.target)
.sections, id: \.name) { section in
Section(header: Text(section.name)) {
// The inner ForEach loop provides the Attribute objects for the given AtteributeGroup
ForEach(self.dataSource.objects(inSection: section)) { attribute in
Text("\(attribute.name)")
}
}
}
} else {
Section(header: Text("There are no Attributes.")) {
EmptyView()
}
}
}
.listStyle(GroupedListStyle())
}
}