-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathContents.swift
120 lines (101 loc) · 3.4 KB
/
Contents.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
//: [Previous](@previous)
//: If you are using Xcode 13+, ignore the comments below:
//: For this page, make sure your build target is set to ParseSwift (iOS) and targeting
//: an iPhone, iPod, or iPad. Also be sure your `Playground Settings`
//: in the `File Inspector` is `Platform = iOS`. This is because
//: SwiftUI in macOS Playgrounds doesn't seem to build correctly
//: Be sure to switch your target and `Playground Settings` back to
//: macOS after leaving this page.
import PlaygroundSupport
import Foundation
import ParseSwift
import SwiftUI
import Combine
PlaygroundPage.current.needsIndefiniteExecution = true
initializeParse()
//: Create your own value typed ParseObject.
struct GameScore: ParseObject, Identifiable {
//: Conform to Identifiable for iOS13+
var id: String { // swiftlint:disable:this identifier_name
guard let objectId = self.objectId else {
return UUID().uuidString
}
return objectId
}
//: These are required for any Object.
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: Your own properties.
var score: Int = 0
var location: ParseGeoPoint?
var name: String?
}
//: It's recommended to place custom initializers in an extension
//: to preserve the convenience initializer.
extension GameScore {
//: Custom initializer.
init(name: String, score: Int) {
self.name = name
self.score = score
}
}
//: To use queries with SwiftUI
//: To create a custom view model that queries GameScore's.
class ViewModel: ObservableObject {
@Published var objects = [GameScore]()
@Published var error: ParseError?
private var subscriptions = Set<AnyCancellable>()
init() {
fetchScores()
}
func fetchScores() {
let query = GameScore.query("score" > 2)
.order([.descending("score")])
let publisher = query
.findPublisher()
.sink(receiveCompletion: { result in
switch result {
case .failure(let error):
//: Publish error.
self.error = error
case .finished:
print("Successfully queried data")
}
},
receiveValue: {
//: Publish found objects
self.objects = $0
print("Found \(self.objects.count), objects: \(self.objects)")
})
publisher.store(in: &subscriptions)
}
}
//: Create a SwiftUI view.
struct ContentView: View {
//: A view model in SwiftUI
@ObservedObject var viewModel = ViewModel()
var body: some View {
NavigationView {
if let error = viewModel.error {
Text(error.description)
} else {
//: Warning - List seems to only work in Playgrounds Xcode 13+.
List(viewModel.objects, id: \.id) { object in
VStack(alignment: .leading) {
Text("Score: \(object.score)")
.font(.headline)
if let createdAt = object.createdAt {
Text("\(createdAt.description)")
}
}
}
}
Spacer()
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
PlaygroundPage.current.finishExecution()
//: [Next](@next)