|
| 1 | +// |
| 2 | +// HomePlayerWidget.swift |
| 3 | +// HomePlayerWidget |
| 4 | +// |
| 5 | +// Created by Kingkor Roy Tirtho on 15/12/24. |
| 6 | +// |
| 7 | + |
| 8 | +import WidgetKit |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +private let widgetGroupId = "group.spotube_home_player_widget" |
| 12 | + |
| 13 | +struct Provider: TimelineProvider { |
| 14 | + func placeholder(in context: Context) -> SimpleEntry { |
| 15 | + SimpleEntry(date: Date(), emoji: "😀") |
| 16 | + } |
| 17 | + |
| 18 | + func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) { |
| 19 | + let entry = SimpleEntry(date: Date(), emoji: "😀") |
| 20 | + completion(entry) |
| 21 | + } |
| 22 | + |
| 23 | + func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { |
| 24 | + var entries: [SimpleEntry] = [] |
| 25 | + |
| 26 | + // Generate a timeline consisting of five entries an hour apart, starting from the current date. |
| 27 | + let currentDate = Date() |
| 28 | + for hourOffset in 0 ..< 5 { |
| 29 | + let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! |
| 30 | + let entry = SimpleEntry(date: entryDate, emoji: "😀") |
| 31 | + entries.append(entry) |
| 32 | + } |
| 33 | + |
| 34 | + let timeline = Timeline(entries: entries, policy: .atEnd) |
| 35 | + completion(timeline) |
| 36 | + } |
| 37 | + |
| 38 | +// func relevances() async -> WidgetRelevances<Void> { |
| 39 | +// // Generate a list containing the contexts this widget is relevant in. |
| 40 | +// } |
| 41 | +} |
| 42 | + |
| 43 | +struct SimpleEntry: TimelineEntry { |
| 44 | + let date: Date |
| 45 | + let emoji: String |
| 46 | +} |
| 47 | + |
| 48 | +struct HomePlayerWidgetEntryView : View { |
| 49 | + var entry: Provider.Entry |
| 50 | + |
| 51 | + var body: some View { |
| 52 | + VStack { |
| 53 | + Text("Time:") |
| 54 | + Text(entry.date, style: .time) |
| 55 | + |
| 56 | + Text("Emoji:") |
| 57 | + Text(entry.emoji) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +struct HomePlayerWidget: Widget { |
| 63 | + let kind: String = "HomePlayerWidget" |
| 64 | + |
| 65 | + var body: some WidgetConfiguration { |
| 66 | + StaticConfiguration(kind: kind, provider: Provider()) { entry in |
| 67 | + if #available(iOS 17.0, *) { |
| 68 | + HomePlayerWidgetEntryView(entry: entry) |
| 69 | + .containerBackground(.fill.tertiary, for: .widget) |
| 70 | + } else { |
| 71 | + HomePlayerWidgetEntryView(entry: entry) |
| 72 | + .padding() |
| 73 | + .background() |
| 74 | + } |
| 75 | + } |
| 76 | + .configurationDisplayName("My Widget") |
| 77 | + .description("This is an example widget.") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#Preview(as: .systemSmall) { |
| 82 | + HomePlayerWidget() |
| 83 | +} timeline: { |
| 84 | + SimpleEntry(date: .now, emoji: "😀") |
| 85 | + SimpleEntry(date: .now, emoji: "🤩") |
| 86 | +} |
0 commit comments