-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathStarWarsContext.swift
192 lines (169 loc) · 4.92 KB
/
StarWarsContext.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
/**
* This defines a basic set of data for our Star Wars Schema.
*
* This data is hard coded for the sake of the demo, but you could imagine
* fetching this data from a backend service rather than from hardcoded
* values in a more complex demo.
*/
public final class StarWarsContext {
private static var tatooine = Planet(
id: "10001",
name: "Tatooine",
diameter: 10465,
rotationPeriod: 23,
orbitalPeriod: 304,
residents: []
)
private static var alderaan = Planet(
id: "10002",
name: "Alderaan",
diameter: 12500,
rotationPeriod: 24,
orbitalPeriod: 364,
residents: []
)
private static var planetData: [String: Planet] = [
"10001": tatooine,
"10002": alderaan,
]
private static var luke = Human(
id: "1000",
name: "Luke Skywalker",
friends: ["1002", "1003", "2000", "2001"],
appearsIn: [.newHope, .empire, .jedi],
homePlanet: tatooine
)
private static var vader = Human(
id: "1001",
name: "Darth Vader",
friends: ["1004"],
appearsIn: [.newHope, .empire, .jedi],
homePlanet: tatooine
)
private static var han = Human(
id: "1002",
name: "Han Solo",
friends: ["1000", "1003", "2001"],
appearsIn: [.newHope, .empire, .jedi],
homePlanet: alderaan
)
private static var leia = Human(
id: "1003",
name: "Leia Organa",
friends: ["1000", "1002", "2000", "2001"],
appearsIn: [.newHope, .empire, .jedi],
homePlanet: alderaan
)
private static var tarkin = Human(
id: "1004",
name: "Wilhuff Tarkin",
friends: ["1001"],
appearsIn: [.newHope],
homePlanet: alderaan
)
private static var humanData: [String: Human] = [
"1000": luke,
"1001": vader,
"1002": han,
"1003": leia,
"1004": tarkin,
]
private static var c3po = Droid(
id: "2000",
name: "C-3PO",
friends: ["1000", "1002", "1003", "2001"],
appearsIn: [.newHope, .empire, .jedi],
primaryFunction: "Protocol"
)
private static var r2d2 = Droid(
id: "2001",
name: "R2-D2",
friends: ["1000", "1002", "1003"],
appearsIn: [.newHope, .empire, .jedi],
primaryFunction: "Astromech"
)
private static var droidData: [String: Droid] = [
"2000": c3po,
"2001": r2d2,
]
public init() {}
/**
* Helper function to get a character by ID.
*/
public func getCharacter(id: String) -> Character? {
Self.humanData[id] ?? Self.droidData[id]
}
/**
* Allows us to query for a character"s friends.
*/
public func getFriends(of character: Character) -> [Character] {
character.friends.compactMap { id in
getCharacter(id: id)
}
}
/**
* Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2.
*/
public func getHero(of episode: Episode?) -> Character {
if episode == .empire {
// Luke is the hero of Episode V.
return Self.luke
}
// R2-D2 is the hero otherwise.
return Self.r2d2
}
/**
* Allows us to query for the human with the given id.
*/
public func getHuman(id: String) -> Human? {
Self.humanData[id]
}
/**
* Allows us to query for the droid with the given id.
*/
public func getDroid(id: String) -> Droid? {
Self.droidData[id]
}
/**
* Allows us to get the secret backstory, or not.
*/
public func getSecretBackStory() throws -> String? {
struct Secret: Error, CustomStringConvertible {
let description: String
}
throw Secret(description: "secretBackstory is secret.")
}
/**
* Allows us to query for a Planet.
*/
public func getPlanets(query: String) -> [Planet] {
Self.planetData
.sorted(by: { $0.key < $1.key })
.map { $1 }
.filter { $0.name.lowercased().contains(query.lowercased()) }
}
/**
* Allows us to query for a Human.
*/
public func getHumans(query: String) -> [Human] {
Self.humanData
.sorted(by: { $0.key < $1.key })
.map { $1 }
.filter { $0.name.lowercased().contains(query.lowercased()) }
}
/**
* Allows us to query for a Droid.
*/
public func getDroids(query: String) -> [Droid] {
Self.droidData
.sorted(by: { $0.key < $1.key })
.map { $1 }
.filter { $0.name.lowercased().contains(query.lowercased()) }
}
/**
* Allows us to query for either a Human, Droid, or Planet.
*/
public func search(query: String) -> [SearchResult] {
return getPlanets(query: query) + getHumans(query: query) + getDroids(query: query)
}
}