import SwiftUI import WidgetKit struct FollowerEntry: TimelineEntry { let date: Date let snapshot: Snapshot } struct Provider: TimelineProvider { func placeholder(in context: Context) -> FollowerEntry { FollowerEntry(date: Date(), snapshot: .sample) } func getSnapshot(in context: Context, completion: @escaping (FollowerEntry) -> Void) { let current = Snapshot.current() let useSample = context.isPreview && current.count == nil completion(FollowerEntry(date: Date(), snapshot: useSample ? .sample : current)) } func getTimeline(in context: Context, completion: @escaping (Timeline) -> Void) { Task { let snapshot = await Updater.update() // Il sistema decide quando ricaricare davvero: chiediamo ogni 15 minuti, su Mac e iPhone. let interval: TimeInterval = 15 * 60 let now = Date() let next = now.addingTimeInterval(interval) var entries = [FollowerEntry(date: now, snapshot: snapshot)] // Quando il giallo del traguardo finisce prepariamo giĆ  la versione normale. if snapshot.milestone != nil, let end = FollowerStore.shared.milestoneEnd, end < next { var normal = snapshot normal.milestone = nil if !Calendar.current.isDate(end, inSameDayAs: now) { normal.today = 0 } entries.append(FollowerEntry(date: end, snapshot: normal)) } completion(Timeline(entries: entries, policy: .after(next))) } } } struct FollowerWidgetView: View { let entry: FollowerEntry @Environment(\.widgetFamily) private var family var body: some View { switch family { case .systemMedium: MediumStazione(snapshot: entry.snapshot) default: SmallStazione(snapshot: entry.snapshot) } } } struct FollowerWidget: Widget { var body: some WidgetConfiguration { StaticConfiguration(kind: "FollowerWidget", provider: Provider()) { entry in // Sulla scrivania macOS schiarisce il containerBackground: lo sfondo scuro lo disegniamo noi. FollowerWidgetView(entry: entry) .padding(16) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Stazione.background) .containerBackground(Stazione.background, for: .widget) } .contentMarginsDisabled() .configurationDisplayName("Follower Instagram") .description("I tuoi follower su un tabellone da stazione.") .supportedFamilies([.systemSmall, .systemMedium]) } } @main struct TabelloneWidgets: WidgetBundle { var body: some Widget { FollowerWidget() } }