RIT-Dining/TigerDine/Data/PushesModel.swift
NinjaCheetah d3d060b5e2
Started work on refactors
- The favorites model now lives inside of the base dining model, since it was only ever used in places where the main dining model was also available and is only relevant when the dining model is available.
- Removed unnecessary instances of models that were going unused.
- Moved the favorite/map/menu buttons in the top right of the DetailView into the right side toolbar.
  - This frees up a good bit of space at the top of the DetailView and looks cleaner, especially with iOS 26's new toolbar style.
- Actually added a copyright string to the about screen.
More refactors, both internally and for the UI, will be coming soon.
2026-01-07 19:29:30 -05:00

90 lines
2.8 KiB
Swift

//
// PushesModel.swift
// TigerDine
//
// Created by Campbell on 11/20/25.
//
import SwiftUI
@Observable
class VisitingChefPushesModel {
var pushes: [ScheduledVistingChefPush] = [] {
didSet {
save()
}
}
private let key = "ScheduledVisitingChefPushes"
init() {
load()
}
/// Schedule a new push notification with the notification center and save its information to UserDefaults if it succeeded.
func scheduleNewPush(name: String, location: String, startTime: Date, endTime: Date) async {
guard !pushAlreadyRegisered(name: name, location: location, startTime: startTime, endTime: endTime) else { return }
let uuid_string = await scheduleVisitingChefNotif(
name: name,
location: location,
startTime: startTime,
endTime: endTime
)
// An empty UUID means that the notification wasn't scheduled for one reason or another. This is ignored for now.
if uuid_string != "" {
pushes.append(
ScheduledVistingChefPush(
uuid: uuid_string,
name: name,
location: location,
startTime: startTime,
endTime: endTime
)
)
save()
}
}
/// Cancels all reigstered push notifications for a specified visiting chef.
func cancelPushesForChef(name: String) {
var uuids: [String] = []
for push in pushes {
if push.name == name {
uuids.append(push.uuid)
}
}
Task {
await cancelVisitingChefNotifs(uuids: uuids)
}
// Once they're canceled, we can drop them from the list.
pushes.removeAll { $0.name == name }
save()
}
/// Checks if a push notification meeting the specified criteria is already scheduled.
func pushAlreadyRegisered(name: String, location: String, startTime: Date, endTime: Date) -> Bool {
for push in pushes {
if push.name == name && push.location == location && push.startTime == startTime && push.endTime == endTime {
return true
}
}
return false
}
/// Write out the registered push notifications.
private func save() {
let encoder = JSONEncoder()
if let data = try? encoder.encode(pushes) {
UserDefaults.standard.set(data, forKey: key)
}
}
/// Load registered push notifications.
private func load() {
let decoder = JSONDecoder()
if let data = UserDefaults.standard.data(forKey: key),
let decoded = try? decoder.decode([ScheduledVistingChefPush].self, from: data) {
pushes = decoded
}
}
}