Skip to content

Commit cfce9a1

Browse files
authored
Merge pull request #20 from codeRIT/peter-scheduleparse
[WIP] Schedule parsing fixes
2 parents 618e236 + 189ac57 commit cfce9a1

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

BrickHack-Mobile/AlertMessage.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ class MessageHandler {
117117
body: "Setting a placeholder name in the meantime...",
118118
type: .info)
119119
}
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+
}
120127
}

BrickHack-Mobile/Models/ScheduleParser.swift

Lines changed: 27 additions & 6 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
}
@@ -270,7 +276,7 @@ class ScheduleParser {
270276
case 1: currentEvent.time = stringToDate(cellText) ?? Date()
271277
case 2: currentEvent.title = cellText
272278
case 3: currentEvent.location = cellText
273-
case 4: currentEvent.description = cellText
279+
case 4: currentEvent.description = filterDescription(cellText)
274280
case 5: currentEvent.uuid = cellText
275281
default: break
276282
}
@@ -280,6 +286,21 @@ class ScheduleParser {
280286

281287
}
282288

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+
}
283304

284305
private static func stringToDate(_ text: String) -> Date? {
285306

@@ -296,7 +317,7 @@ class ScheduleParser {
296317

297318
// Build the final date
298319
var dateComponents = DateComponents()
299-
dateComponents.timeZone = TimeZone(identifier: "America/New_York") // Might not be req'd but works
320+
dateComponents.timeZone = TimeZone(identifier: "America/New_York")
300321
dateComponents.year = 2020
301322
dateComponents.month = 2
302323
dateComponents.hour = Calendar.current.component(.hour, from: convertedTime!)

0 commit comments

Comments
 (0)