> loading_
# To get the latest peak hours, your Swift app can now fetch the remote config.
# Here's how you can fetch and decode the schedule on app launch.
import Foundation
// 1. Define a struct that matches the JSON structure from our new endpoint.
struct PeakHoursConfig: Codable {
let timezone: String
let peakDays: [String]
let peakHoursUTC: [String]
}
// 2. Create a function to fetch and parse the configuration.
func fetchPeakHoursConfig(completion: @escaping (PeakHoursConfig?) -> Void) {
// The URL for our new dynamically updated JSON file.
guard let url = URL(string: "https://config.overguild.com/claude/peak-hours.json") else {
print("Invalid URL")
completion(nil)
return
}
// 3. Use URLSession to perform the network request.
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print("Fetch failed: \(error.localizedDescription)")
completion(nil)
return
}
guard let data = data else {
print("No data returned from fetch")
completion(nil)
return
}
// 4. Decode the JSON data into our Swift struct.
do {
let config = try JSONDecoder().decode(PeakHoursConfig.self, from: data)
DispatchQueue.main.async {
completion(config)
}
} catch {
print("JSON decoding failed: \(error)")
completion(nil)
}
}.resume()
}
// 5. Call the function, for example, in your app's main view or delegate.
fetchPeakHoursConfig { config in
if let config = config {
print("Successfully fetched latest peak hours for \(config.timezone)")
// Now update your UI or local notifications with the new schedule.
}
}