2023-03-11 18:30:33 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
2023-03-28 10:35:38 +02:00
|
|
|
// Licensed under the Apache License 2.0.
|
2023-03-11 18:30:33 +01:00
|
|
|
//
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-11 18:30:33 +01:00
|
|
|
import WidgetKit
|
|
|
|
import SwiftUI
|
|
|
|
import Intents
|
|
|
|
|
2023-05-01 07:50:24 +02:00
|
|
|
struct PhotoProvider: TimelineProvider {
|
|
|
|
typealias Entry = PhotoWidgetEntry
|
2023-03-11 18:30:33 +01:00
|
|
|
|
2023-05-01 07:50:24 +02:00
|
|
|
func placeholder(in context: Context) -> PhotoWidgetEntry {
|
|
|
|
StatusFetcher.shared.placeholder()
|
2023-03-11 18:30:33 +01:00
|
|
|
}
|
|
|
|
|
2023-05-01 07:50:24 +02:00
|
|
|
func getSnapshot(in context: Context, completion: @escaping (PhotoWidgetEntry) -> Void) {
|
2023-03-11 18:30:33 +01:00
|
|
|
Task {
|
|
|
|
if let widgetEntry = await self.getWidgetEntries(length: 1).first {
|
|
|
|
completion(widgetEntry)
|
|
|
|
} else {
|
2023-05-01 07:50:24 +02:00
|
|
|
let entry = StatusFetcher.shared.placeholder()
|
2023-03-11 18:30:33 +01:00
|
|
|
completion(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:10:59 +02:00
|
|
|
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
|
2023-03-11 18:30:33 +01:00
|
|
|
Task {
|
|
|
|
let currentDate = Date()
|
|
|
|
let widgetEntries = await self.getWidgetEntries()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-04-10 12:35:49 +02:00
|
|
|
let nextUpdateDate = Calendar.current.date(byAdding: .hour, value: 1, to: currentDate)!
|
2023-03-11 18:30:33 +01:00
|
|
|
let timeline = Timeline(entries: widgetEntries, policy: .after(nextUpdateDate))
|
|
|
|
completion(timeline)
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-05-01 07:50:24 +02:00
|
|
|
func getWidgetEntries(length: Int = 3) async -> [PhotoWidgetEntry] {
|
2023-03-11 18:30:33 +01:00
|
|
|
do {
|
2023-05-01 07:50:24 +02:00
|
|
|
return try await StatusFetcher.shared.fetchWidgetEntries(length: length)
|
2023-03-11 18:30:33 +01:00
|
|
|
} catch {
|
2023-05-01 07:50:24 +02:00
|
|
|
return [StatusFetcher.shared.placeholder()]
|
2023-03-11 18:30:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|