> loading_
# Fetch the latest peak hours policy from our new dynamic config endpoint.
# This ensures your users always have the most up-to-date schedule.
import Foundation
// 1. Define a struct that matches the JSON structure of our remote policy.
struct PeakHoursPolicy: Codable {
let version: String
let timezone: String
let schedule: [String: [String]] // e.g., ["Monday": ["14:00", "17:00"]]
}
// 2. Create a function to fetch and decode the policy.
func fetchLatestPolicy(completion: @escaping (Result<PeakHoursPolicy, Error>) -> Void) {
// The endpoint hosting the AI-updated JSON configuration.
guard let url = URL(string: "https://config.claude-notifier.com/peak-hours.json") else { return }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
completion(.failure(error))
return
}
guard let data = data else { return }
// 3. Decode the JSON data into our Swift struct.
do {
let policy = try JSONDecoder().decode(PeakHoursPolicy.self, from: data)
// On success, pass back the new policy.
completion(.success(policy))
} catch {
// Handle any JSON parsing errors.
completion(.failure(error))
}
}
task.resume()
}
// 4. Call this function on app launch or when a user pulls to refresh.
// Remember to update your UI on the main thread.
fetchLatestPolicy { result in
DispatchQueue.main.async {
switch result {
case .success(let policy):
print("Successfully fetched policy version: \(policy.version)")
// updateYourUI(with: policy)
case .failure(let error):
print("Failed to fetch policy: \(error.localizedDescription)")
// showAlerToUser()
}
}
}