Implement initial asteroid detail view controller

This commit is contained in:
CypherPoet 2019-01-21 01:05:09 -05:00
parent 810db9b6c4
commit dec6101e3e
2 changed files with 100 additions and 0 deletions

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
F3C4951721F58F39002853B6 /* AsteroidDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3C4951621F58F39002853B6 /* AsteroidDetailView.swift */; };
F3FD7F1121F3F56D009FDABA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FD7F1021F3F56D009FDABA /* AppDelegate.swift */; };
F3FD7F1321F3F56D009FDABA /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FD7F1221F3F56D009FDABA /* HomeViewController.swift */; };
F3FD7F1621F3F56D009FDABA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F3FD7F1421F3F56D009FDABA /* Main.storyboard */; };
@ -16,6 +17,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
F3C4951621F58F39002853B6 /* AsteroidDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsteroidDetailView.swift; sourceTree = "<group>"; };
F3FD7F0D21F3F56D009FDABA /* Near Earth Objects.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Near Earth Objects.app"; sourceTree = BUILT_PRODUCTS_DIR; };
F3FD7F1021F3F56D009FDABA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
F3FD7F1221F3F56D009FDABA /* HomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = "<group>"; };
@ -60,6 +62,7 @@
F3FD7F1221F3F56D009FDABA /* HomeViewController.swift */,
F3FD7F2221F420D7009FDABA /* Asteroid.swift */,
F3FD7F1421F3F56D009FDABA /* Main.storyboard */,
F3C4951621F58F39002853B6 /* AsteroidDetailView.swift */,
F3FD7F1721F3F56E009FDABA /* Assets.xcassets */,
F3FD7F1921F3F56E009FDABA /* LaunchScreen.storyboard */,
F3FD7F1C21F3F56E009FDABA /* Info.plist */,
@ -138,6 +141,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F3C4951721F58F39002853B6 /* AsteroidDetailView.swift in Sources */,
F3FD7F1321F3F56D009FDABA /* HomeViewController.swift in Sources */,
F3FD7F1121F3F56D009FDABA /* AppDelegate.swift in Sources */,
F3FD7F2321F420D7009FDABA /* Asteroid.swift in Sources */,

View File

@ -0,0 +1,96 @@
//
// AsteroidDetailView.swift
// Near Earth Objects
//
// Created by Brian Sipple on 1/21/19.
// Copyright © 2019 Brian Sipple. All rights reserved.
//
import UIKit
import WebKit
class AsteroidDetailView: UIViewController {
var webView: WKWebView!
var asteroid: Asteroid?
override func loadView() {
webView = WKWebView()
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let pageHTML = makePageHTML()
webView.loadHTMLString(pageHTML, baseURL: nil)
}
/*
// The HTML we're going to use tells iOS that the page fits mobile devices,
// and that we want the font size to be 150% of the standard font size.
//
// All that HTML will be combined with the body value from our petition, then sent to the web view.
*/
func makePageHTML() -> String {
guard let asteroid = asteroid else { return "" }
var nearMissHTML = ""
if let closeApproachDate = asteroid.closeApproachDate {
if let missDistanceKM = asteroid.missDistanceKM {
nearMissHTML = """
<p>
The asteroid made its closes approach to Earth on \(closeApproachDate) &mdash; passing
within \(missDistanceKM)!
</p>
"""
}
}
let html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>\(asteroid.name)</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-size: 150%;
padding: 1.25rem 1rem;
}
</style>
</head>
<body>
<p>
Asteroid "\(asteroid.name)" (officially designated as "\(asteroid.designation)" has an
<a href="https://en.wikipedia.org/wiki/Absolute_magnitude#Solar_System_bodies_(H)">absolute magnitude</a> of
\(asteroid.absoluteMagnitudeH)
</p>
\(nearMissHTML)
<p>
Visit this asteriod's <a href="\(asteroid.jplURL.absoluteString)">NASA JPL's page</a> to learn more
</p>
</body>
</html>
"""
return html
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}