-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContentView.swift
78 lines (63 loc) · 2.38 KB
/
ContentView.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
//
// ContentView.swift
// P12F CoreDataProject
//
// Created by Julian Moorhouse on 03/04/2021.
//
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) var moc
@State var lastNameFilter = "A"
@State var sort: [NSSortDescriptor] = []
@State var predicate = Predicate.beginsWith
var body: some View {
VStack {
FilteredList(filterKey: "lastName", filterValue: lastNameFilter, sort: sort, predicate: predicate) { (singer: Singer) in
Text("\(singer.wrappedFirstName) \(singer.wrappedFirstName)")
}
Button("Add Examples") {
let taylor = Singer(context: self.moc)
taylor.firstName = "Taylor"
taylor.lastName = "Swift"
let ed = Singer(context: self.moc)
ed.firstName = "Ed"
ed.lastName = "Sheeran"
let adele = Singer(context: self.moc)
adele.firstName = "Adele"
adele.lastName = "Adkins"
try? self.moc.save()
}
Button("Show A") {
self.lastNameFilter = "A"
}
Button("Show S") {
self.lastNameFilter = "S"
}
Button("First name sort asc") {
self.sort = [
NSSortDescriptor(keyPath: \Singer.firstName, ascending: true),
NSSortDescriptor(keyPath: \Singer.lastName, ascending: true)
]
}
Button("last name sort desc") {
self.sort = [
NSSortDescriptor(keyPath: \Singer.lastName, ascending: false),
NSSortDescriptor(keyPath: \Singer.firstName, ascending: true)
]
}
Button("Predicate - Begins With") {
self.predicate = .beginsWith
}
Button("Predicate - Less than") {
self.predicate = .lessThan
}
Spacer(minLength: 10)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}