RIT-Dining/RIT Dining/Types.swift
NinjaCheetah 30c025e113
Lots of code cleanup
Separated types out into their own file to make them easier to keep track of, and described what they're all for better. Also removed unnecessary "Location" type used in ContentView to display data, because it was almost an exact duplicate of the type that it was being created from. Removing that removed a lot of pointless extra logic, and should make the processs of how it pulls and parses the data easier to understand.
Multiple open periods for one location are also now sorted, so that the earliest open time will be shown first. Some locations have them flipped in the response data, so they were backwards before.
2025-09-02 15:03:43 -04:00

67 lines
1.8 KiB
Swift

//
// Types.swift
// RIT Dining
//
// Created by Campbell on 9/2/25.
//
import Foundation
// I'll be honest, I am NOT good at representing other people's JSON in my code. This kinda sucks but it gets the job done and can
// be improved later when I feel like it.
struct DiningLocationParser: Decodable {
// An individual "event", which is just an open period for the location.
struct Events: Decodable {
// Hour exceptions for the given event.
struct HoursException: Decodable {
let id: Int
let name: String
let startTime: String
let endTime: String
let startDate: String
let endDate: String
let open: Bool
}
let startTime: String
let endTime: String
let exceptions: [HoursException]?
}
// Other basic information to read from a location's JSON that we'll need later.
let id: Int
let name: String
let summary: String
let description: String
let mapsUrl: String
let events: [Events]
}
// Struct that probably doesn't need to exist but this made parsing the list of location responses easy.
struct DiningLocationsParser: Decodable {
let locations: [DiningLocationParser]
}
// Enum to represent the four possible states a given location can be in.
enum OpenStatus {
case open
case closed
case openingSoon
case closingSoon
}
// An individual open period for a location.
struct DiningTimes: Equatable, Hashable {
var openTime: Date
var closeTime: Date
}
// The basic information about a dining location needed to display it in the app after parsing is finished.
struct DiningLocation: Identifiable, Hashable {
let id: Int
let name: String
let summary: String
let desc: String
let mapsUrl: String
let diningTimes: [DiningTimes]?
let open: OpenStatus
}