Skip to content

Commit 0fb3a85

Browse files
committed
feat: add now playing
1 parent c9130f0 commit 0fb3a85

File tree

4 files changed

+87
-20
lines changed

4 files changed

+87
-20
lines changed

Sources/ActivityClient/Client.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,32 @@ extension ActivityClient {
4646
}
4747

4848
public struct NowPlaying: Sendable, Equatable, Codable {
49-
public let name: String
50-
public let albumn: String?
51-
public let artist: String
49+
public let title: String
50+
public let album: String?
51+
public let artist: String?
5252
public let service: Service
5353

54-
public enum Service: Sendable, Equatable, Codable {
55-
case appleMusic
54+
public enum Service: String, Sendable, Equatable, Codable {
55+
case apple
5656
case spotify
5757
}
5858
}
5959

6060
public struct Activity: Sendable, Equatable, Codable {
6161
public let location: Location?
6262
public let nowPlaying: NowPlaying?
63+
64+
public static let encoder = {
65+
let encoder = JSONEncoder()
66+
encoder.dateEncodingStrategy = .iso8601
67+
return encoder
68+
}()
69+
70+
public static let decoder = {
71+
let decoder = JSONDecoder()
72+
decoder.dateDecodingStrategy = .iso8601
73+
return decoder
74+
}()
6375
}
6476
}
6577

Sources/App/Middlewares/SiteMiddleware.swift renamed to Sources/App/SiteMiddleware.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct SiteMiddleware<Context: RequestContext>: RouterController {
3838
return HomePage(codeLang: .resolve(req))
3939
case .api(.activity(.all)):
4040
do {
41-
return try JSONEncoder().encode(self.activityClient.redactedActivity(), from: req, context: ctx)
41+
return try ActivityClient.Activity.encoder.encode(self.activityClient.redactedActivity(), from: req, context: ctx)
4242
} catch {
4343
throw HTTPError(.forbidden)
4444
}

Sources/Pages/HomePage.swift

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ private struct UserView: HTML {
5757
.joined(separator: ", ")
5858
}
5959

60+
var nowPlaying: String? {
61+
guard let nowPlaying = activityClient.nowPlaying() else {
62+
return nil
63+
}
64+
65+
let nowPlayingText = [
66+
nowPlaying.title,
67+
nowPlaying.artist?.isEmpty == false ? "" : nil,
68+
nowPlaying.artist
69+
]
70+
.compactMap { $0 }
71+
.joined(separator: " ")
72+
return nowPlayingText
73+
}
74+
6075
static let aboutDescription = """
6176
I'm a passionate software developer who builds applications using Swift and modern web technologies.
6277
"""
@@ -71,7 +86,8 @@ private struct UserView: HTML {
7186
name: "Erik Bautista Santibanez",
7287
role: "Mobile & Web Developer",
7388
home: "\(residency ?? .default)"\
74-
\(currentLocation.flatMap { ",\n location: \"Currently in \($0)\"" } ?? "")
89+
\(currentLocation.flatMap { ",\n location: \"Currently in \($0)\"" } ?? "")\
90+
\(nowPlaying.flatMap { ",\n listeningTo: \"\($0)\"" } ?? "")
7591
)
7692
7793
> print(user.about())
@@ -83,7 +99,8 @@ private struct UserView: HTML {
8399
name: "Erik Bautista Santibanez",
84100
role: "Mobile & Web Developer",
85101
home: "\(residency ?? .default)"\
86-
\(currentLocation.flatMap { ",\n location: \"Currently in \($0)\"" } ?? "")
102+
\(currentLocation.flatMap { ",\n location: \"Currently in \($0)\"" } ?? "")\
103+
\(nowPlaying.flatMap { ",\n listeningTo: \"\($0)\"" } ?? "")
87104
};
88105
89106
> console.log(user.about());
@@ -95,14 +112,15 @@ private struct UserView: HTML {
95112
name: "Erik Bautista Santibanez",
96113
role: "Mobile & Web Developer",
97114
home: "\(residency ?? .default)"\
98-
\(currentLocation.flatMap { ",\n location: \"Currently in \($0)\"" } ?? "")
115+
\(currentLocation.flatMap { ",\n location: \"Currently in \($0)\"" } ?? "")\
116+
\(nowPlaying.flatMap { ",\n listeningTo: \"\($0)\"" } ?? "")
99117
};
100118
101119
> println!("{}", user.about());
102120
// \(Self.aboutDescription)
103121
"""
104122
case .none:
105-
h1(.aria.label("Name")) {
123+
h1(.aria.label("name")) {
106124
span { "#" }
107125
.inlineStyle("color", "#808080")
108126
.inlineStyle("font-weight", "700")
@@ -112,9 +130,9 @@ private struct UserView: HTML {
112130
.inlineStyle("margin-bottom", "0.25rem")
113131

114132
HTMLGroup {
115-
p(.aria.label("Occupation")) { "Mobile & Web Developer" }
133+
p(.aria.label("occupation")) { "Mobile & Web Developer" }
116134

117-
p(.aria.label("Residency")) {
135+
p(.aria.label("residency")) {
118136
svg(
119137
.xmlns(),
120138
.fill("currentColor"),
@@ -134,7 +152,7 @@ private struct UserView: HTML {
134152
}
135153

136154
if let currentLocation {
137-
p(.aria.label("Current location")) {
155+
p(.aria.label("current location")) {
138156
svg(
139157
.xmlns(),
140158
.fill("currentColor"),
@@ -158,15 +176,48 @@ private struct UserView: HTML {
158176

159177
"Currently in "
160178

161-
span { "***" }
162-
.inlineStyle("color", "#808080")
163-
.inlineStyle("font-weight", "700")
179+
// span { "***" }
180+
// .inlineStyle("color", "#808080")
181+
// .inlineStyle("font-weight", "700")
164182
em { currentLocation }
165183
.inlineStyle("font-weight", "700")
166184
.inlineStyle("color", "#fafafa")
167-
span { "***" }
168-
.inlineStyle("color", "#808080")
185+
// span { "***" }
186+
// .inlineStyle("color", "#808080")
187+
// .inlineStyle("font-weight", "700")
188+
}
189+
}
190+
191+
if let nowPlaying {
192+
p(.aria.label("music playing")) {
193+
// <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="#000000" viewBox="0 0 256 256"><path d=""></path></svg>
194+
svg(
195+
.xmlns(),
196+
.fill("currentColor"),
197+
.viewBox("0 0 256 256"),
198+
.aria.label("wave icon")
199+
) {
200+
path(
201+
.d(
202+
"M56,96v64a8,8,0,0,1-16,0V96a8,8,0,0,1,16,0ZM88,24a8,8,0,0,0-8,8V224a8,8,0,0,0,16,0V32A8,8,0,0,0,88,24Zm40,32a8,8,0,0,0-8,8V192a8,8,0,0,0,16,0V64A8,8,0,0,0,128,56Zm40,32a8,8,0,0,0-8,8v64a8,8,0,0,0,16,0V96A8,8,0,0,0,168,88Zm40-16a8,8,0,0,0-8,8v96a8,8,0,0,0,16,0V80A8,8,0,0,0,208,72Z"
203+
)
204+
)
205+
}
206+
.inlineStyle("scale", "calc(100% * -1) 100%")
207+
.svgIconStyling()
208+
.inlineStyle("margin-right", "0.25rem")
209+
210+
"Listening to "
211+
212+
// span { "***" }
213+
// .inlineStyle("color", "#808080")
214+
// .inlineStyle("font-weight", "700")
215+
em { nowPlaying }
169216
.inlineStyle("font-weight", "700")
217+
.inlineStyle("color", "#fafafa")
218+
// span { "***" }
219+
// .inlineStyle("color", "#808080")
220+
// .inlineStyle("font-weight", "700")
170221
}
171222
}
172223

Sources/Routes/APIRoute.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ extension SiteRoute.APIRoute {
3232
Route(.case(SiteRoute.APIRoute.ActivityRoute.location)) {
3333
Method.post
3434
Path { "location" }
35-
Body(.json(ActivityClient.Location?.self))
35+
Optionally {
36+
Body(.json(ActivityClient.Location.self, decoder: ActivityClient.Activity.decoder, encoder: ActivityClient.Activity.encoder))
37+
}
3638
}
3739

3840
Route(.case(SiteRoute.APIRoute.ActivityRoute.nowPlaying)) {
3941
Method.post
4042
Path { "now-playing" }
41-
Body(.json(ActivityClient.NowPlaying?.self))
43+
Optionally {
44+
Body(.json(ActivityClient.NowPlaying.self, decoder: ActivityClient.Activity.decoder, encoder: ActivityClient.Activity.encoder))
45+
}
4246
}
4347
}
4448
}

0 commit comments

Comments
 (0)