forked from ABausG/home_widget
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHomeWidgetExample.swift
100 lines (87 loc) · 3.02 KB
/
HomeWidgetExample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// HomeWidgetExample.swift
// HomeWidgetExample
//
// Created by Anton Borries on 04.10.20.
//
import SwiftUI
import WidgetKit
private let widgetGroupId = "group.es.antonborri.exampleHomeWidget"
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> ExampleEntry {
ExampleEntry(date: Date(), title: "Placeholder Title", message: "Placeholder Message")
}
func getSnapshot(in context: Context, completion: @escaping (ExampleEntry) -> Void) {
let data = UserDefaults.init(suiteName: widgetGroupId)
let entry = ExampleEntry(
date: Date(), title: data?.string(forKey: "title") ?? "No Title Set",
message: data?.string(forKey: "message") ?? "No Message Set")
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
getSnapshot(in: context) { (entry) in
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
}
struct ExampleEntry: TimelineEntry {
let date: Date
let title: String
let message: String
}
struct HomeWidgetExampleEntryView: View {
var entry: Provider.Entry
let data = UserDefaults.init(suiteName: widgetGroupId)
let iconPath: String?
init(entry: Provider.Entry) {
self.entry = entry
iconPath = data?.string(forKey: "dashIcon")
}
var body: some View {
VStack.init(
alignment: .center, spacing: /*@START_MENU_TOKEN@*/ nil /*@END_MENU_TOKEN@*/,
content: {
if #available(iOSApplicationExtension 17, *) {
Button(
intent: BackgroundIntent(
url: URL(string: "homeWidgetExample://titleClicked"), appGroup: widgetGroupId)
) {
Text(entry.title).bold().font( /*@START_MENU_TOKEN@*/.title /*@END_MENU_TOKEN@*/)
}.buttonStyle(.plain).frame(maxWidth: .infinity, alignment: .leading)
} else {
Text(entry.title).bold().font( /*@START_MENU_TOKEN@*/.title /*@END_MENU_TOKEN@*/).frame(
maxWidth: .infinity, alignment: .leading)
}
Text(entry.message)
.font(.body)
.widgetURL(URL(string: "homeWidgetExample://message?message=\(entry.message)&homeWidget"))
.frame(maxWidth: .infinity, alignment: .leading)
if iconPath != nil {
Image(uiImage: UIImage(contentsOfFile: iconPath!)!).resizable()
.scaledToFill()
.frame(width: 64, height: 64)
}
}
)
}
}
@main
struct HomeWidgetExample: Widget {
let kind: String = "HomeWidgetExample"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
HomeWidgetExampleEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct HomeWidgetExample_Previews: PreviewProvider {
static var previews: some View {
HomeWidgetExampleEntryView(
entry: ExampleEntry(date: Date(), title: "Example Title", message: "Example Message")
)
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}