This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsqwidget.swift
235 lines (190 loc) · 8.47 KB
/
sqwidget.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// sqwidget.swift
// sqwidget
//
// Created by Aryan Nambiar on 6/22/20.
//
import WidgetKit
import SwiftUI
import CoreData
let defaultImageColors = UIImageColors(background: UIColor.black, primary: UIColor.white, secondary: UIColor.blue, detail: UIColor.purple)
struct PlaceholderView : View {
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Now Playing")
.font(.system(.title3))
.foregroundColor(.black)
Text("Please login on app!")
.font(.system(.subheadline))
.foregroundColor(.black)
.bold()
Text("by Artist\nReleased:01/01/0000")
.font(.system(.caption))
.foregroundColor(.black)
Text("Updated at \(Date())")
.font(.system(.caption2))
.foregroundColor(.black)
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
.padding()
.background(Color.black)
.animation(.easeInOut)
// .background(LinearGradient(gradient: Gradient(colors: [.orange, .red]), startPoint: .top, endPoint: .bottom))
}
}
struct NowPlayingCheckerWidgetView : View {
let entry: LastNowPlayingEntry
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Now Playing")
.font(.system(.title3))
.foregroundColor(Color(entry.NowPlaying.imageColors.primary))
Text(entry.NowPlaying.message)
.font(.system(.subheadline))
.foregroundColor(Color(entry.NowPlaying.imageColors.primary))
.bold()
Text("by \(entry.NowPlaying.author)")
.font(.system(.caption))
.foregroundColor(Color(entry.NowPlaying.imageColors.primary))
Text("Released: \(entry.NowPlaying.date)")
.font(.system(.caption))
.foregroundColor(Color(entry.NowPlaying.imageColors.secondary))
Text("Updated at \(Self.format(date:entry.date))")
.font(.system(.caption2))
.foregroundColor(Color(entry.NowPlaying.imageColors.detail))
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
.padding()
.background(Color(entry.NowPlaying.imageColors.background))
.animation(.easeInOut)
// .background(LinearGradient(gradient: Gradient(colors: [.purple, .blue]), startPoint: .top, endPoint: .bottom))
}
static func format(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
return formatter.string(from: date)
}
}
struct NowPlaying {
let message: String
let author: String
let date: String
let imageColors: UIImageColors
}
var in_progress = false
var recent_NowPlaying: NowPlaying = NowPlaying(message: "Song", author: "Artist", date: "2020-06-23", imageColors: defaultImageColors)
struct NowPlayingLoader {
static func fetch(completion: @escaping (Result<NowPlaying, Error>) -> Void) {
if !in_progress {
in_progress = true
let currentPlaybackURL = URL(string: "https://api.spotify.com/v1/me/player")!
var currentPlaybackRequest = URLRequest(url: currentPlaybackURL)
let defaults = UserDefaults(suiteName: "group.dev.nambiar.CustomWidgets.app")!
var accessToken = defaults.string(forKey: "accessToken")
if(accessToken != nil) {
accessToken = accessToken!
currentPlaybackRequest.addValue("Bearer \(accessToken!)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: currentPlaybackRequest) { (data, response, error) in
guard error == nil else {
completion(.failure(error!))
return
}
let NowPlaying = getNowPlayingInfo(fromData: data!)
completion(.success(NowPlaying))
recent_NowPlaying = NowPlaying
in_progress = false
}
task.resume()
}
}
else {
completion(.success(recent_NowPlaying))
}
}
static func getNowPlayingInfo(fromData data: Foundation.Data) -> NowPlaying {
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
var songName = ""
var artistName = ""
var date = ""
var colors = defaultImageColors
if (json != nil) {
let itemJSON: [String: Any]? = json!["item"] as? [String: Any]
if(itemJSON != nil) {
let albumJSON = itemJSON!["album"] as! [String: Any]
songName = itemJSON!["name"] as! String
date = albumJSON["release_date"] as! String
let artists = itemJSON!["artists"] as! Array<Any>
let artist = artists[0] as! [String: Any]
artistName = artist["name"] as! String
let images = albumJSON["images"] as! Array<Any>
let imageJSON = images[0] as! [String: Any]
let url = imageJSON["url"] as! String
colors = getImageColors(url: URL(string: url)!)
}
else {
print("INVALID JSON")
}
}
return NowPlaying(message: songName, author: artistName, date: date, imageColors: colors)
}
}
func getImageColors(url: URL) -> UIImageColors {
let imageData = try? Data(contentsOf: url)
if(imageData != nil) {
let colors = UIImage(data: imageData!)!.getColors()
return colors!
}
else {
return defaultImageColors
}
}
var currentNowPlaying: NowPlaying = NowPlaying(message: "Song", author: "Artist", date: "2020-06-24", imageColors: defaultImageColors)
var newRefreshDate: Date = Date()
var initialized: Bool = false
struct NowPlayingTimeline: TimelineProvider {
typealias Entry = LastNowPlayingEntry
/* protocol methods implemented below! */
public func snapshot(with context: Context, completion: @escaping (LastNowPlayingEntry) -> ()) {
let fakeNowPlaying = NowPlaying(message: "Fixed stuff", author: "John Appleseed", date: "2020-06-23", imageColors: defaultImageColors)
let entry = LastNowPlayingEntry(date: Date(), NowPlaying: fakeNowPlaying)
completion(entry)
}
public func timeline(with context: Context, completion: @escaping (Timeline<LastNowPlayingEntry>) -> ()) {
let currentDate = Date()
let refreshDate = Calendar.current.date(byAdding: .second, value: 10, to: currentDate)!
if (currentDate >= newRefreshDate) || !initialized {
initialized = true
newRefreshDate = refreshDate
NowPlayingLoader.fetch { result in
let nowplaying: NowPlaying
if case .success(let fetchedNowPlaying) = result {
nowplaying = fetchedNowPlaying
} else {
nowplaying = NowPlaying(message: "Failed to load Now Playing", author: "", date: "", imageColors: defaultImageColors)
}
let entry = LastNowPlayingEntry(date: currentDate, NowPlaying: nowplaying)
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
currentNowPlaying = nowplaying
completion(timeline)
}
}
else {
let entry = LastNowPlayingEntry(date: currentDate, NowPlaying: currentNowPlaying)
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
completion(timeline)
}
}
}
struct LastNowPlayingEntry: TimelineEntry {
public let date: Date
public let NowPlaying: NowPlaying
}
@main
struct NowPlayingCheckerWidget: Widget {
private let kind: String = "NowPlayingCheckerWidget"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: NowPlayingTimeline(), placeholder: PlaceholderView()) { entry in
NowPlayingCheckerWidgetView(entry: entry)
}
.configurationDisplayName("Now Playing by Aryan Nambiar")
.description("Shows your Spotify Now Playing!")
}
}