-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBloggerWidget.swift
207 lines (197 loc) · 9.93 KB
/
BloggerWidget.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// BloggerWidget.swift
// BloggerWidget
//
// Created by 莊智凱 on 2022/1/9.
//
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> Entry {
Entry(date: Date(), entry: Array(repeating: Entries(isSave: nil, id: Entries.Id(id: ""), published: Entries.Published(published: Date()), category: nil, title: Entries.Title(title: ""), content: Entries.Content(content: ""), link: Array(repeating: Links(rel: "", href: ""), count: 4), author: Array(repeating: Author(name: Author.Name(name: ""), avatar: Author.Avatar(src: "")), count: 1), thumbnail: nil, commentNum: nil), count: 4), uiImage: Array(repeating: UIImage(), count: 4))
}
func getSnapshot(in context: Context, completion: @escaping (Entry) -> ()) {
fetchPosts { entries in
var uiImages: [UIImage] = Array(repeating: UIImage(), count: 4)
let currentDate = Date()
var count = 0
for index in 0..<entries.count {
if let thumbnail = entries[index].thumbnail {
let resVarId = thumbnail.url.lastIndex(of: "s") ?? thumbnail.url.endIndex
let fullResUrl = thumbnail.url[..<resVarId]
URLSession.shared.dataTask(with: URL(string: fullResUrl + "s480")!) { data, response, error in
if let data = data, let uiImage = UIImage(data: data) {
uiImages[index] = uiImage
count += 1
if count == entries.count - 1 {
let entry = Entry(date: currentDate, entry: entries, uiImage: uiImages)
completion(entry)
}
}
}.resume()
} else {
count += 1
if count == entries.count - 1 {
let entry = Entry(date: currentDate, entry: entries, uiImage: uiImages)
completion(entry)
}
}
}
}
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
fetchPosts { entries in
var uiImages: [UIImage] = Array(repeating: UIImage(), count: 4)
let currentDate = Date()
var count = 0
for index in 0..<entries.count {
if let thumbnail = entries[index].thumbnail {
let resVarId = thumbnail.url.lastIndex(of: "s") ?? thumbnail.url.endIndex
let fullResUrl = thumbnail.url[..<resVarId]
URLSession.shared.dataTask(with: URL(string: fullResUrl + "s480")!) { data, response, error in
if let data = data, let uiImage = UIImage(data: data) {
uiImages[index] = uiImage
count += 1
if count == entries.count {
let entry = Entry(date: currentDate, entry: entries, uiImage: uiImages)
let nextUpdate = Calendar.current.date(byAdding: .minute, value: 15, to: currentDate)
let timeline = Timeline(entries: [entry], policy: .after(nextUpdate!))
completion(timeline)
}
}
}.resume()
} else {
count += 1
if count == entries.count {
let entry = Entry(date: currentDate, entry: entries, uiImage: uiImages)
let nextUpdate = Calendar.current.date(byAdding: .minute, value: 15, to: currentDate)
let timeline = Timeline(entries: [entry], policy: .after(nextUpdate!))
completion(timeline)
}
}
}
}
}
}
struct BloggerWidgetEntryView : View {
@Environment(\.widgetFamily) var widgetFamily
var data: Entry
var body: some View {
switch widgetFamily {
case .systemSmall:
GeometryReader { geo in
ZStack {
Image(uiImage: data.uiImage[0])
.resizable()
.scaledToFill()
VStack {
Spacer()
Text(data.entry[0].title.title)
.font(.caption)
.foregroundColor(.white)
.lineLimit(3)
.padding(10)
.padding(.trailing, 5)
}
.frame(width: geo.size.width, height: geo.size.height)
.background(LinearGradient(gradient: Gradient(colors: [.black, .clear]), startPoint: .bottom, endPoint: .center))
}
.frame(width: geo.size.width, height: geo.size.height)
}
case .systemMedium:
GeometryReader { geo in
VStack(alignment: .leading, spacing: 8) {
Label {
Text("Latest posts").bold().font(.subheadline)
} icon: {
Text(Image(systemName: "doc.richtext")).bold().font(.subheadline).foregroundColor(.orange)
}
ForEach(0..<2) { index in
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text(data.entry[index].published.published, format: .dateTime.year().month().day()).bold().foregroundColor(.gray).font(.caption)
Text(data.entry[index].title.title).bold().font(.caption)
}
Spacer()
Image(uiImage: data.uiImage[index])
.resizable()
.scaledToFill()
.frame(width: geo.size.height/3.2, height: geo.size.height/3.2)
.cornerRadius(5)
.clipped()
}
}
}.padding().frame(width: geo.size.width, height: geo.size.height)
}
case .systemLarge:
GeometryReader { geo in
VStack(alignment: .leading, spacing: 10) {
Label {
Text("Latest posts").bold().font(.subheadline)
} icon: {
Text(Image(systemName: "doc.richtext")).bold().font(.subheadline).foregroundColor(.orange)
}
ForEach(0..<4) { index in
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text(data.entry[index].published.published, format: .dateTime.year().month().day()).bold().foregroundColor(.gray).font(.caption)
Text(data.entry[index].title.title).bold().font(.caption)
}
Spacer()
Image(uiImage: data.uiImage[index])
.resizable()
.scaledToFill()
.frame(width: geo.size.height/5.3, height: geo.size.height/5.3)
.cornerRadius(5)
.clipped()
}
}
}.padding().frame(width: geo.size.width, height: geo.size.height)
}
case .systemExtraLarge:
GeometryReader { geo in
VStack(alignment: .leading, spacing: 10) {
Label {
Text("Latest posts on Blogger").bold().font(.subheadline)
} icon: {
Text(Image(systemName: "doc.richtext")).bold().font(.subheadline).foregroundColor(.orange)
}
ForEach(0..<4) { index in
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text(data.entry[index].title.title).bold().font(.caption)
Spacer()
HStack {
ForEach(data.entry[index].category!, id: \.term) { category in
Text("-" + category.term).bold().foregroundColor(.gray).font(.caption)
}
}
Spacer()
( Text(Image(systemName: "calendar")) + Text(" ") + Text(data.entry[index].published.published, format: .dateTime.year().month().day()) + Text(" ") + Text(Image(systemName: "text.bubble")) + Text(" ") + Text("\(data.entry[index].commentNum!.commentNum) comment")).bold().foregroundColor(.gray).font(.caption)
}
Spacer()
Image(uiImage: data.uiImage[index])
.resizable()
.scaledToFill()
.frame(width: geo.size.height/5.3, height: geo.size.height/5.3)
.cornerRadius(5)
.clipped()
}
}
}.padding().frame(width: geo.size.width, height: geo.size.height)
}
default: Text("")
}
}
}
@main
struct BloggerWidget: Widget {
let kind: String = "BloggerWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
BloggerWidgetEntryView(data: entry)
}
.configurationDisplayName("Blogger Widget")
.description("Add this widget on your iPhone to view lastest posts!")
}
}