Commit 1af5afe0 authored by Eyad Hasan's avatar Eyad Hasan
Browse files

The model needs some more abstraction to work for certain functions. Need to...

The model needs some more abstraction to work for certain functions.  Need to figure out a generic schedule to use for grabbing the current schedule.
parent 612ef55d
import Foundation
// So many dictionaries
struct Alerts : Codable {
let id: Int
let created: String
public struct Facilities : Codable {
struct facility_category : Codable {
let created: String
let id: Int
let modified: String
let name: String
}
let facility_category: facility_category
let facility_classifier: String
struct facility_location : Codable {
let address: String
let building: String
let campus_region: String
struct coordinate_location : Codable {
let coordinates: [Double]
let type: String
}
let coordinate_location : coordinate_location
let created: String
let id: Int
let modified: String
let on_campus: Bool
}
let facility_location : facility_location
let facility_name: String
let facility_product_tags: [String]
let logo: String
let modified: String
let urgency_tag: String
let subject: String
let body: String
let url: String
let start_datetime: String
let end_datetime: Date?
let note: String
let phone_number: String
let slug: String
struct open_times : Codable {
let end_day: Int
let end_time: String
let modified: String
let schedule: Int
let start_day: Int
let start_time: String
}
struct special_schedules : Codable {
let id: Int
let modified: String
let name: String
let twenty_four_hours: Bool
let valid_end: String
let valid_start: String
let open_times: [open_times]
}
struct main_schedule : Codable {
let id: Int
let modified: String
let name: String
let twenty_four_hours: Bool
let valid_end: String
let valid_start: String
let open_times: [open_times]
}
let main_schedule: main_schedule
let special_schedule: special_schedules
let open_times: open_times
let tapingo_url: String
}
guard let url = URL(string: "https://api.srct.gmu.edu/whatsopen/v2/alerts/?format=json") else {throw NSError()}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let response = try decoder.decode([Alerts].self, from: dataResponse)
for i in response{
print(i.start_datetime)
print(i.modified)
guard let url = URL(string: "https://api.srct.gmu.edu/whatsopen/v2/facilities/?format=json") else {throw NSError()}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
let decoder = JSONDecoder()
let response = try decoder.decode([Facilities].self, from: dataResponse)
for i in response{
print(i.schedules.main_schedule.open_times)
}
//print(response[0].facility_location.id)
} catch let parsingError {
print("Error", parsingError)
}
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
task.resume()
......@@ -245,5 +245,5 @@ public extension List {
}
func filterByLocation(_ facilities: [Facilities], filters: WOPFilters) -> [Facilities] {
return facilities.filter { filters.onlyFromLocations[($0.facility_location.building)!.lowercased()] ?? false }
return facilities.filter { filters.onlyFromLocations[($0.facility_location.building).lowercased()] ?? false }
}
......@@ -50,14 +50,14 @@ public class FacilitiesDetailViewController: UIViewController, UITableViewDelega
modalPresentationCapturesStatusBarAppearance = true
var name = facility.facilityName
var name = facility.facility_name
let separator = name.index(of: "[")
if separator != nil {
name = String(name[..<separator!]).replacingOccurrences(of: "\\s+$",with: "", options: .regularExpression)
}
NameLabel.text = name
PlaceLabel.text = facility.facilityLocation!.building
CategoryLabel.text = facility.category?.categoryName.uppercased()
PlaceLabel.text = facility.facility_location.building
CategoryLabel.text = facility.facility_catagory.name
let open = WOPUtilities.isOpen(facility: facility)
......
......@@ -24,11 +24,12 @@ public struct networkCheck {
public struct Facilities : Codable {
struct facility_category : Codable {
let created: Date
let created: String
let id: Int
let modified: String
let name: String
}
let facility_category: facility_category
let facility_classifier: String
struct facility_location : Codable {
......@@ -40,7 +41,7 @@ public struct Facilities : Codable {
let type: String
}
let coordinate_location : coordinate_location
let created: Date
let created: String
let id: Int
let modified: String
let on_campus: Bool
......@@ -51,60 +52,73 @@ public struct Facilities : Codable {
let facility_product_tags: [String]
let logo: String
struct main_schedule : Codable {
let modified: String
let note: String
let phone_number: String
let slug: String
struct open_times : Codable {
let end_day: Int
let end_time: String
let modified: String
let schedule: Int
let start_day: Int
let start_time: String
}
struct special_schedules : Codable {
let id: Int
let modified: String
let name: String
struct open_times : Codable {
let end_day: Int
let end_time: String
let modified: String
let schedule: Int
let start_day: Int
let start_time: String
let twenty_four_hours: Bool
let valid_end: String
let valid_start: String
let open_times: [open_times]
}
struct main_schedule : Codable {
let id: Int
let modified: String
let name: String
let twenty_four_hours: Bool
let valid_end: Date?
let valid_start: Date?
let valid_end: String
let valid_start: String
let open_times: [open_times]
}
let main_schedule: main_schedule
let main_schedule: main_schedule
let special_schedule: [special_schedules]
let modified: String
let note: String
let phone_number: String
let slug: String
struct special_schedules : Codable {
struct schedules : Codable {
let id: Int
let modified: Date
let modified: String
let name: String
struct open_times : Codable {
let end_day: Int
let end_time: String
let modified: String
let schedule: Int
let start_day: Int
let start_time: String
}
let twenty_four_hours: Bool
let valid_end: Date
let valid_start: Date
let valid_end: String
let valid_start: String
let open_times: [open_times]
}
let schedules: schedules
let open_times: open_times
let tapingo_url: String
let special_schedules: [special_schedules]
}
public struct Alerts : Codable {
let id: Int
let created: Date
let created: String
let modified: String
let urgency_tag: String
let subject: String
let body: String
let url: String
let start_datetime: Date
let end_datetime: Date
let start_datetime: String
let end_datetime: String
}
......
......@@ -91,7 +91,7 @@ public class WOPUtilities: NSObject {
//Gets the current day of the week.
// This is also broken and will have to be updated to account for not having a schedules type.
public static func today(facility: Facilities, special: Bool = false) -> (Facilities.main_schedule.open_times?, Facilities.special_schedules.open_times?) {
public static func today(facility: Facilities, special: Bool = false) -> Facilities.open_times{
let scheduleOpenTimes = getCurrentSchedule(facility)?.open_times
let currentDay = getCurrentDayOfWeek()
......@@ -100,7 +100,7 @@ public class WOPUtilities: NSObject {
return openTime
}
}
return nil
return openTime
}
public static func getStartEndDates(_ facility: Facilities) -> (startTime: Date, endTime: Date)? {
......@@ -226,11 +226,11 @@ public class WOPUtilities: NSObject {
// This needs to be updated with the correct return type. Should also be modified in order to account for semantic types.
public static func getCurrentSchedule(_ facility: Facilities) -> Facilities.special_schedules? {
public static func getCurrentSchedule(_ facility: Facilities) -> Facilities.schedules {
let formatter = ISO8601DateFormatter()
let now = Date()
if(facility.special_schedules != nil) {
for schedule in facility.special_schedules {
if(facility.special_schedule != nil) {
for schedule in facility.special_schedule {
if schedule.valid_start == "" {
dump(schedule)
}
......@@ -242,7 +242,7 @@ public class WOPUtilities: NSObject {
}
}
}
return facility.main_schedule
return schedule
}
public static func isMainSchedule(facility: Facilities) -> Bool {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment