RIT-Dining/Shared/SharedComponents.swift
NinjaCheetah 26e419a41b
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.
2026-01-09 19:19:04 -05:00

70 lines
2.2 KiB
Swift

//
// SharedComponents.swift
// TigerDine
//
// Created by Campbell on 9/8/25.
//
import Foundation
import SafariServices
import SwiftUI
// Gross disgusting UIKit code :(
// There isn't a direct way to use integrated Safari from SwiftUI, except maybe in iOS 26? I'm not targeting that though so I must fall
// back on UIKit stuff.
struct SafariView: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: Context) -> SFSafariViewController {
SFSafariViewController(url: url)
}
func updateUIViewController(_ uiViewController: SFSafariViewController, context: Context) {}
}
func getTCAPIFriendlyDateString(date: Date) -> String {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone.current
formatter.dateFormat = "yyyy-MM-dd"
return formatter.string(from: date)
}
func getFDMPAPIFriendlyDateString(date: Date) -> String {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone.current
formatter.dateFormat = "yyyy/MM/dd"
return formatter.string(from: date)
}
// The common date formatter that I'm using everywhere that open periods are shown within the app.
let dateDisplay: DateFormatter = {
let display = DateFormatter()
display.timeZone = TimeZone(identifier: "America/New_York")
display.dateStyle = .none
display.timeStyle = .short
return display
}()
let visitingChefDateDisplay: DateFormatter = {
let display = DateFormatter()
display.dateFormat = "EEEE, MMM d"
display.locale = Locale(identifier: "en_US_POSIX")
return display
}()
let weekdayFromDate: DateFormatter = {
let weekdayFormatter = DateFormatter()
weekdayFormatter.dateFormat = "EEEE"
return weekdayFormatter
}()
// Custom view extension that just applies modifiers in a block to the object it's applied to. Mostly useful for splitting up conditional
// modifiers that should only be applied for certain OS versions. (A returning feature from RNGTool!)
extension View {
func apply<V: View>(@ViewBuilder _ block: (Self) -> V) -> V { block(self) }
}