Skip to content

Commit 4a50762

Browse files
authored
Merge pull request #21 from codeRIT/staging
Merge staging v1.0.1
2 parents ab86fa9 + 23769c8 commit 4a50762

3 files changed

Lines changed: 84 additions & 36 deletions

File tree

BrickHack-Mobile/AlertMessage.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ class MessageHandler {
2323
static func showAlertMessage(withTitle title: String, body: String, type: MessageType ) {
2424

2525
let view = MessageView.viewFromNib(layout: .tabView)
26-
view.configureTheme(.error)
26+
27+
// Map our fake type to their type
28+
switch type {
29+
case .error: view.configureTheme(.error)
30+
case .warning: view.configureTheme(.warning)
31+
case .info: view.configureTheme(.info)
32+
}
33+
2734
view.button?.isHidden = true
2835
view.configureContent(title: title,
2936
body: body,
@@ -103,4 +110,18 @@ class MessageHandler {
103110
body: "Please try again later.",
104111
type: .error)
105112
}
113+
114+
static func showUnknownUserDataError() {
115+
print("ERROR: Could not find questionnaire for user.")
116+
showAlertMessage(withTitle: "User name unknown!",
117+
body: "Setting a placeholder name in the meantime...",
118+
type: .info)
119+
}
120+
121+
static func showScheduleParsingError() {
122+
print("ERROR: Unable to get latest schedule")
123+
showAlertMessage(withTitle: "Unable to get latest schedule!",
124+
body: "Please try again later, by restarting the app.",
125+
type: .error)
126+
}
106127
}

BrickHack-Mobile/Controllers/LoginViewController.swift

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,6 @@ class LoginViewController: UIViewController {
136136
print("passed user object")
137137
eventsVC.currentUser = self.currentUser
138138
}
139-
140-
141-
// Check for MainTabBarController (skip through nav controller)
142-
// Note: not used!
143-
/*
144-
if let tabVC = segue.destination.children.first as? MainTabBarController {
145-
146-
// Check if valid user (on error, user will reauth)
147-
guard userID != 0 else {
148-
MessageHandler.showInvalidUserError()
149-
return
150-
}
151-
152-
// Pass data to the tab bar controller, which will handle passing its own children
153-
tabVC.userID = userID
154-
tabVC.oauthGrant = oauthGrant
155-
} */
156139
}
157140
}
158141

@@ -176,13 +159,15 @@ class LoginViewController: UIViewController {
176159
SVProgressHUD.show()
177160
}
178161

162+
// MARK: User data request
179163
URLSession.shared.dataTask(with: signedNameRequest) { (data, response, error) in
180164

181165
// MARK: Error checking
182166
guard error == nil else {
183167
DispatchQueue.main.async {
184168
MessageHandler.showNetworkError(withText: error!.localizedDescription)
185169
SVProgressHUD.dismiss()
170+
self.logout()
186171
}
187172
return
188173
}
@@ -191,6 +176,7 @@ class LoginViewController: UIViewController {
191176
DispatchQueue.main.async {
192177
MessageHandler.showUserDataParsingError()
193178
SVProgressHUD.dismiss()
179+
self.logout()
194180
}
195181
return
196182
}
@@ -200,6 +186,7 @@ class LoginViewController: UIViewController {
200186
DispatchQueue.main.async {
201187
MessageHandler.showNetworkError(withText: "Invalid response code")
202188
SVProgressHUD.dismiss()
189+
self.logout()
203190
}
204191
return
205192
}
@@ -208,30 +195,50 @@ class LoginViewController: UIViewController {
208195
DispatchQueue.main.async {
209196
MessageHandler.showNetworkError(withText: "User account not found")
210197
SVProgressHUD.dismiss()
198+
self.logout()
211199
}
212200
return
213201
}
214202

215-
216203
// MARK: Data conversion
217204

205+
var unknownErrorOccured = false
206+
218207
// Convert server data to our User object
219208
do {
220209
self.currentUser = try JSONDecoder().decode(User.self, from: data)
221-
} catch (let error) {
210+
} catch {
211+
212+
print("Got in error")
213+
214+
// This error might be fixable!
215+
// Admin accounts don't have a questionnaire, and if the user was
216+
// able to login, then this is almost certainly an admin account.
217+
// (Or their questionnaire doesn't exist).
218+
219+
unknownErrorOccured = true
222220
DispatchQueue.main.async {
223-
print("parsing error: \(error)")
224-
MessageHandler.showUserDataParsingError(withText: "Unable to convert JSON")
225-
SVProgressHUD.dismiss()
221+
MessageHandler.showUnknownUserDataError()
226222
}
227-
return
223+
224+
// In this case, we just assign some blank data.
225+
self.currentUser = User(firstName: "Mystery", lastName: "User", major: "Bachelors of codeRIT")
228226
}
229227

230-
// Now that we have the user data, go to the main screen,
231-
// passing the data forward!
232-
DispatchQueue.main.async {
233-
SVProgressHUD.dismiss()
234-
self.performSegue(withIdentifier: "authSuccessSegue", sender: self)
228+
// Now that we have the user data (or fake admin data),
229+
// go to the main screen, and pass the data forward!
230+
231+
// If error occured, give user like three seconds to read it before moving on
232+
if unknownErrorOccured {
233+
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
234+
SVProgressHUD.dismiss()
235+
self.performSegue(withIdentifier: "authSuccessSegue", sender: self)
236+
}
237+
} else {
238+
DispatchQueue.main.async {
239+
SVProgressHUD.dismiss()
240+
self.performSegue(withIdentifier: "authSuccessSegue", sender: self)
241+
}
235242
}
236243

237244
return

BrickHack-Mobile/Models/ScheduleParser.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct Event: CustomDebugStringConvertible {
2929
var timeString: String {
3030
let dateFormatter = DateFormatter()
3131
dateFormatter.dateFormat = "E hh:mm a"
32-
dateFormatter.timeZone = TimeZone(identifier: "UTC") // Already in ET, dont convert again
32+
dateFormatter.timeZone = TimeZone(identifier: "America/New_York")
3333
return dateFormatter.string(from: time)
3434
}
3535
var title: String
@@ -152,13 +152,17 @@ class ScheduleParser {
152152
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
153153

154154
guard error == nil else {
155-
// @TODO: Error handle
155+
DispatchQueue.main.async {
156+
MessageHandler.showScheduleParsingError()
157+
}
156158
print(error!)
157159
return
158160
}
159161

160162
guard let data = data else {
161-
// @TODO: Error handle
163+
DispatchQueue.main.async {
164+
MessageHandler.showScheduleParsingError()
165+
}
162166
print("no data")
163167
return
164168
}
@@ -175,7 +179,9 @@ class ScheduleParser {
175179

176180
} catch let error {
177181
// @TODO: Error handle (JSON parse error)
178-
print("it broke")
182+
DispatchQueue.main.async {
183+
MessageHandler.showScheduleParsingError()
184+
}
179185
print(error)
180186
}
181187
}
@@ -241,7 +247,6 @@ class ScheduleParser {
241247
case 0:
242248

243249
guard rowIndex > 1 || !currentEvent.title.isEmpty else {
244-
print("broken like me right now")
245250
break
246251
}
247252

@@ -271,7 +276,7 @@ class ScheduleParser {
271276
case 1: currentEvent.time = stringToDate(cellText) ?? Date()
272277
case 2: currentEvent.title = cellText
273278
case 3: currentEvent.location = cellText
274-
case 4: currentEvent.description = cellText
279+
case 4: currentEvent.description = filterDescription(cellText)
275280
case 5: currentEvent.uuid = cellText
276281
default: break
277282
}
@@ -281,6 +286,21 @@ class ScheduleParser {
281286

282287
}
283288

289+
// Strips the description of any HTML
290+
private static func filterDescription(_ text: String) -> String {
291+
292+
var result = String(text)
293+
294+
while result.contains("<") && result.contains(">") {
295+
let htmlStart = result.firstIndex(of: "<")!
296+
let htmlEnd = result.firstIndex(of: ">")!
297+
298+
result.removeSubrange(htmlStart...htmlEnd)
299+
}
300+
301+
print("Converted \(text) to \(result)")
302+
return result
303+
}
284304

285305
private static func stringToDate(_ text: String) -> Date? {
286306

@@ -297,7 +317,7 @@ class ScheduleParser {
297317

298318
// Build the final date
299319
var dateComponents = DateComponents()
300-
dateComponents.timeZone = TimeZone(identifier: "America/New_York") // Might not be req'd but works
320+
dateComponents.timeZone = TimeZone(identifier: "America/New_York")
301321
dateComponents.year = 2020
302322
dateComponents.month = 2
303323
dateComponents.hour = Calendar.current.component(.hour, from: convertedTime!)

0 commit comments

Comments
 (0)