Added caching, background refresh, and first widget

- The main dining location information is now cached on download.
   - The freshness of the cache is checked whenever it's loaded, and if the last refreshed date is not today's date then it's dropped and the app refreshes from the API normally.
   - This reduces load times if you open the app multiple times a day. The data won't change during the day, so you can load it the first time and then use the cache the rest of the time.
   - Refreshing via pull to refresh or the refresh button still refreshes the cache from the server.
- Added a background refresh task.
   - TigerDine now registered a background fetch task with the device that will update the location information up to a maximum of 4 times per day. The cache is checked first, so a new request will only be made if the cache is stale.
   - This allows for automatic notification scheduling at times other than when the app is launched. As long as background tasks can run, notifications will be automatically scheduled when necessary.
   - Depending on the timing, this may allow you to never see any load times in TigerDine, since the cache might already be up to date before you use the app for the first time in a day.
- Started adding widgets!
   - TigerDine now offers an hours widget that lets you put the hours for a specified location on your home screen, along with a visual indicator of when that location is open today.
   - The widget automatically feeds off of TigerDine's cache, so hey, no extra network requests required!
   - This widget currently DOES NOT support multi-opening locations like Brick City Cafe or Gracie's. This is still a work in progress.
This commit is contained in:
2026-01-09 19:19:04 -05:00
parent d3d060b5e2
commit 26e419a41b
22 changed files with 1038 additions and 32 deletions

View File

@@ -0,0 +1,26 @@
//
// BackgroundRefresh.swift
// TigerDine
//
// Created by Campbell on 1/9/26.
//
import SwiftUI
import BackgroundTasks
/// This is the global function used to tell iOS that we want to schedule a new instance of the background refresh task. It's used both in the main app to automatically reschedule a task when one completes, and also within the dining model to schedule a task when a refresh finishes.
func scheduleNextRefresh() {
let request = BGAppRefreshTaskRequest(
identifier: "dev.ninjacheetah.RIT-Dining.refresh"
)
// Refresh NO SOONER than 6 hours from now. That's not super important since the task will exit pretty much immediately
// if the cache is still fresh, but we really don't need to try more frequently than this so don't bother.
request.earliestBeginDate = Date(timeIntervalSinceNow: 6 * 60 * 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("failed to schedule background refresh: ", error)
}
}

View File

@@ -11,23 +11,38 @@ import SwiftUI
class DiningModel {
var locationsByDay = [[DiningLocation]]()
var daysRepresented = [Date]()
var lastRefreshed: Date?
var lastRefreshed: Date? {
get {
let sharedDefaults = UserDefaults(suiteName: "group.dev.ninjacheetah.RIT-Dining")
// If this fails, we should default to an interval of 0. 1970 is obviously going to register as stale cache and will
// trigger a reload.
return Date(timeIntervalSince1970: sharedDefaults?.double(forKey: "lastRefreshed") ?? 0.0)
}
set {
let sharedDefaults = UserDefaults(suiteName: "group.dev.ninjacheetah.RIT-Dining")
sharedDefaults?.set(newValue?.timeIntervalSince1970, forKey: "lastRefreshed")
}
}
// External models that should be nested under this one.
var favorites = Favorites()
var notifyingChefs = NotifyingChefs()
var visitingChefPushes = VisitingChefPushesModel()
/// 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 {
func getDaysRepresented() async {
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
}
/// 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 {
await getDaysRepresented()
var newLocationsByDay = [[DiningLocation]]()
for day in week {
for day in daysRepresented {
let dateString = day.formatted(.iso8601
.year().month().day()
.dateSeparator(.dash))
@@ -43,8 +58,48 @@ class DiningModel {
throw(error)
}
}
// Encode all the locations as JSON.
let encoder = JSONEncoder()
let encodedLocationsByDay = try encoder.encode(newLocationsByDay)
// Write the data out so it's cached for later.
let sharedDefaults = UserDefaults(suiteName: "group.dev.ninjacheetah.RIT-Dining")
sharedDefaults?.set(encodedLocationsByDay, forKey: "cachedLocationsByDay")
// Once we're done caching, update the UI.
locationsByDay = newLocationsByDay
lastRefreshed = Date()
// And then schedule push notifications.
await scheduleAllPushes()
// And finally schedule a background refresh 6 hours from now.
scheduleNextRefresh()
}
/// Wrapper function for the real getHoursByDay() that checks the last refreshed stamp and uses cached data if it's fresh or triggers a refresh if it's stale.
func getHoursByDayCached() async throws {
let now = Date()
// If we can't access the lastRefreshed key, then there is likely no cache.
if let lastRefreshed = lastRefreshed {
if Calendar.current.startOfDay(for: now) == Calendar.current.startOfDay(for: lastRefreshed) {
// Last refresh happened today, so the cache is fresh and we should load that.
await getDaysRepresented()
let decoder = JSONDecoder()
let cachedLocationsByDay = try decoder.decode([[DiningLocation]].self, from: (UserDefaults(suiteName: "group.dev.ninjacheetah.RIT-Dining")!.data(forKey: "cachedLocationsByDay")!))
print(cachedLocationsByDay)
// Load cache, update open status, do a notification cleanup, and return. We only need to clean up because loading
// cache means that there can't be any new notifications to schedule since the last real data refresh.
locationsByDay = cachedLocationsByDay
updateOpenStatuses()
await cleanupPushes()
return
}
// Otherwise, the cache is stale and we can fall out to the call to update it.
}
try await getHoursByDay()
}
/// 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.

View File

@@ -1,136 +0,0 @@
//
// TigerCenterTypes.swift
// TigerDine
//
// Created by Campbell on 9/2/25.
//
import Foundation
/// Struct to parse the response data from the TigerCenter API when getting the information for a dining location.
struct DiningLocationParser: Decodable {
/// An individual "event", which is just an open period for the location.
struct Event: 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 daysOfWeek: [String]
let exceptions: [HoursException]?
}
/// An individual "menu", which can be either a daily special item or a visitng chef. Description needs to be optional because visiting chefs have descriptions but specials do not.
struct Menu: Decodable {
let name: String
let description: String?
let category: String
}
/// Other basic information to read from a location's JSON that we'll need later.
let id: Int
let mdoId: Int
let name: String
let summary: String
let description: String
let mapsUrl: String
let events: [Event]
let menus: [Menu]
}
/// 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
}
/// Enum to represent the five possible states a visiting chef can be in.
enum VisitingChefStatus {
case hereNow
case gone
case arrivingLater
case arrivingSoon
case leavingSoon
}
/// A visiting chef present at a location.
struct VisitingChef: Equatable, Hashable {
let name: String
let description: String
var openTime: Date
var closeTime: Date
var status: VisitingChefStatus
}
/// A daily special at a location.
struct DailySpecial: Equatable, Hashable {
let name: String
let type: String
}
/// The IDs required to get the menu for a location from FD MealPlanner. Only present if the location appears in the map.
struct FDMPIds: Hashable {
let locationId: Int
let accountId: Int
}
/// 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 mdoId: Int
let fdmpIds: FDMPIds?
let name: String
let summary: String
let desc: String
let mapsUrl: String
let date: Date
let diningTimes: [DiningTimes]?
var open: OpenStatus
var visitingChefs: [VisitingChef]?
let dailySpecials: [DailySpecial]?
}
/// Parser to read the occupancy data for a location.
struct DiningOccupancyParser: Decodable {
/// Represents a per-hour occupancy rating.
struct HourlyOccupancy: Decodable {
let hour: Int
let today: Int
let today_max: Int
let one_week_ago: Int
let one_week_ago_max: Int
let average: Int
}
let count: Int
let location: String
let building: String
let mdo_id: Int
let max_occ: Int
let open_status: String
let intra_loc_hours: [HourlyOccupancy]
}
/// Struct used to represent a day and its hours as strings. Type used for the hours of today and the next 6 days used in DetailView.
struct WeeklyHours: Hashable {
let day: String
let date: Date
let timeStrings: [String]
}