SpacePod 35 SwiftUI Web Links
The api doesn’t fully support all the features of the site. Let’s provide a Safari link in the details view of each pod.
DateFormatter+Extensions.swift
- Delete
formatter.timeZone = TimeZone(secondsFromGMT: 0)
(bug fix) - New Struct
url
inNetwork
- Add
yyMMdd
formatter toDateFormatter+Extensions
- Add
yyMMdd
toDate+Extensions
- Add
webUrl
toDate+Extensions
- Create
WebLinkView
- Add
WebLinkView
toPodDetailView
1, 2 DateFormatter+Extensions
/// Formats date as "yyMMdd" (e.g. 210102)
static let yyMMdd: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyMMdd"
formatter.calendar = Calendar(identifier: .iso8601)
return formatter
}()
3, 4 Date+Extensions
/// Returns a date string as "yyMMdd" (e.g. 210102)
var yyMMdd: String {
return DateFormatter.yyMMdd.string(from: self)
}
/// Returns the web page url for date (e.g. https://apod.nasa.gov/apod/ap220102.html)
var webUrl: URL? {
return URL(string: "https://apod.nasa.gov/apod/ap" + self.yyMMdd + ".html")
}
5 WebLink.swift
import SwiftUI
/// Links to the web page for the given Pod or nil
struct WebLink: View {
var date: Date?
var body: some View {
if let url = date?.webUrl {
Link(destination: url) {
Label("Open in Safari", systemImage: "safari.fill")
.symbolRenderingMode(.hierarchical)
}
}
}
}
struct WebLink_Previews: PreviewProvider {
static var previews: some View {
List {
Section(header: Text("Today")) { WebLink(date: Date()) }
Section(header: Text("NIL")) { WebLink() }
}
}
}
6 PodDetailView.swift
WebLink(date: pod.date)