mirror of
https://github.com/NinjaCheetah/RIT-Dining.git
synced 2025-10-19 06:36:18 -04:00
A model is now used to store all of the dining location information. This means that the data is now shared between all views, making it much easier to refresh and removing lots of excess API calls. dining-all is now called for the current day and the following 6 days on app launch and on refresh, but beyond that no additional API calls need to be made (excluding occupancy info). This means no more loading screens between views! The window for hours in DetailView has been shifted to now show the current day and the next 6 days rather than the hours for each day in the current calendar week. This makes a lot more sense, because who cares what last Tuesday's hours were on Saturday, you'd rather know what's coming up in the next week. The visiting chef screen now supports scrolling through 7 days of visiting chefs instead of just today and tomorrow. Some basic frameworks laid for the visiting chef notification feature, however it does not work yet and is not exposed in the app. Also fixed sorting and searching bugs introduced by changes in the previous commit.
60 lines
2.2 KiB
Swift
60 lines
2.2 KiB
Swift
//
|
|
// Model.swift
|
|
// RIT Dining
|
|
//
|
|
// Created by Campbell on 10/1/25.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
@Observable
|
|
class DiningModel {
|
|
var locationsByDay = [[DiningLocation]]()
|
|
var daysRepresented = [Date]()
|
|
var lastRefreshed: Date?
|
|
|
|
// This is the actual method responsible for making requests to the API for the current day and next 6 days to collect all
|
|
// of the information that the app needs for the various view. Making it part of the model allows it to be updated from
|
|
// any view at any time, and prevents excess API requests (if you never refresh, the app will never need to make more than 7
|
|
// calls per launch).
|
|
func getHoursByDay() async throws {
|
|
let calendar = Calendar.current
|
|
let today = calendar.startOfDay(for: Date())
|
|
let week: [Date] = (0..<7).compactMap { offset in
|
|
calendar.date(byAdding: .day, value: offset, to: today)
|
|
}
|
|
daysRepresented = week
|
|
var newLocationsByDay = [[DiningLocation]]()
|
|
for day in week {
|
|
let dateString = day.formatted(.iso8601
|
|
.year().month().day()
|
|
.dateSeparator(.dash))
|
|
switch await getAllDiningInfo(date: dateString) {
|
|
case .success(let locations):
|
|
var newDiningLocations = [DiningLocation]()
|
|
for i in 0..<locations.locations.count {
|
|
let diningInfo = parseLocationInfo(location: locations.locations[i], forDate: day)
|
|
newDiningLocations.append(diningInfo)
|
|
}
|
|
newLocationsByDay.append(newDiningLocations)
|
|
case .failure(let error):
|
|
throw(error)
|
|
}
|
|
}
|
|
locationsByDay = newLocationsByDay
|
|
lastRefreshed = Date()
|
|
}
|
|
|
|
// Iterates through all of the locations and updates their open status indicator based on the current time. Does a replace
|
|
// to make sure that it updates any views observing this model.
|
|
func updateOpenStatuses() {
|
|
locationsByDay = locationsByDay.map { day in
|
|
day.map { location in
|
|
var location = location
|
|
location.updateOpenStatus()
|
|
return location
|
|
}
|
|
}
|
|
}
|
|
}
|