|
| 1 | +import SwiftUI |
| 2 | +import WidgetKit |
| 3 | + |
| 4 | +public extension UIImage { |
| 5 | + static func loadFrom(url: URL, completion: @escaping (_ image: UIImage?) -> Void) { |
| 6 | + DispatchQueue.global().async { |
| 7 | + if let data = try? Data(contentsOf: url) { |
| 8 | + DispatchQueue.main.async { |
| 9 | + completion(UIImage(data: data)) |
| 10 | + } |
| 11 | + } else { |
| 12 | + DispatchQueue.main.async { |
| 13 | + completion(nil) |
| 14 | + } |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +private struct Provider: TimelineProvider { |
| 21 | + let icon = UIImage(named: "ic_chart") ?? UIImage() |
| 22 | + func placeholder(in _: Context) -> SimpleEntry { |
| 23 | + SimpleEntry(date: Date(), image: Image(uiImage: icon)) |
| 24 | + } |
| 25 | + |
| 26 | + func getSnapshot(in _: Context, completion: @escaping (SimpleEntry) -> Void) { |
| 27 | + let entry = SimpleEntry(date: Date(), image: Image(uiImage: icon)) |
| 28 | + completion(entry) |
| 29 | + } |
| 30 | + |
| 31 | + func getTimeline(in _: Context, completion: @escaping (Timeline<Entry>) -> Void) { |
| 32 | + let currentDate = Date() |
| 33 | + let nextDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate) |
| 34 | + |
| 35 | + guard let nextDate = nextDate else { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.kappa.expense")?.appendingPathComponent("chart.png") else { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + UIImage.loadFrom(url: url) { image in |
| 44 | + if let image = image { |
| 45 | + let date = Date() |
| 46 | + let img = Image(uiImage: image) |
| 47 | + |
| 48 | + let entry: SimpleEntry |
| 49 | + |
| 50 | + entry = SimpleEntry( |
| 51 | + date: date, |
| 52 | + image: img |
| 53 | + ) |
| 54 | + |
| 55 | + let timeline = Timeline( |
| 56 | + entries: [entry], |
| 57 | + policy: .after(nextDate) |
| 58 | + ) |
| 59 | + |
| 60 | + completion(timeline) |
| 61 | + |
| 62 | + } else { |
| 63 | + let entry = SimpleEntry(date: currentDate, image: Image(uiImage: icon)) |
| 64 | + let timeline = Timeline(entries: [entry], policy: .atEnd) |
| 65 | + completion(timeline) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +private struct SimpleEntry: TimelineEntry { |
| 72 | + let date: Date |
| 73 | + let image: Image |
| 74 | +} |
| 75 | + |
| 76 | +private struct ChartWidgetEntryView: View { |
| 77 | + var entry: Provider.Entry |
| 78 | + |
| 79 | + var body: some View { |
| 80 | + ZStack { |
| 81 | + entry.image |
| 82 | + .resizable() |
| 83 | + .aspectRatio(contentMode: .fill) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +struct ChartWidget: Widget { |
| 89 | + private let kind: String = WidgetKind.chart |
| 90 | + |
| 91 | + var body: some WidgetConfiguration { |
| 92 | + StaticConfiguration(kind: kind, provider: Provider()) { entry in |
| 93 | + ChartWidgetEntryView(entry: entry) |
| 94 | + } |
| 95 | + .configurationDisplayName("Chart Widget") |
| 96 | + .description("A pie chart show your expenses' percentages") |
| 97 | + .supportedFamilies([.systemSmall]) |
| 98 | + } |
| 99 | +} |
0 commit comments