Created
February 18, 2018 20:29
-
-
Save jasonyunjoonpark/25f4dae599ba3c32c7881f0c55db32b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
let imageView: UIImageView = { | |
let img = UIImageView() | |
img.translatesAutoresizingMaskIntoConstraints = false | |
img.image = UIImage(named: "bitstampIcon") | |
return img | |
}() | |
let priceLabel: UILabel = { | |
let priceLabel = UILabel() | |
priceLabel.translatesAutoresizingMaskIntoConstraints = false | |
priceLabel.text = "N/A" | |
priceLabel.numberOfLines = 0 | |
priceLabel.textAlignment = .center | |
priceLabel.font = UIFont.boldSystemFont(ofSize: 40) | |
return priceLabel | |
}() | |
let refreshButton: UIButton = { | |
let button = UIButton() | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.backgroundColor = UIColor.gray | |
button.setTitle("Refresh", for: .normal) | |
button.layer.cornerRadius = 5 | |
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 40) | |
button.addTarget(self, action: #selector(handlePriceUpdate), for: .touchUpInside) | |
return button | |
}() | |
@objc func handlePriceUpdate() { | |
fetchData() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = UIColor.white | |
view.addSubview(priceLabel) | |
view.addSubview(imageView) | |
view.addSubview(refreshButton) | |
setupPriceLabel() | |
setupImageView() | |
setupRefreshButton() | |
fetchData() | |
} | |
func setupImageView() { | |
//x, y, width and height constraints | |
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
imageView.bottomAnchor.constraint(equalTo: priceLabel.topAnchor).isActive = true | |
imageView.widthAnchor.constraint(equalToConstant: 150).isActive = true | |
imageView.heightAnchor.constraint(equalToConstant: 150).isActive = true | |
} | |
func setupPriceLabel() { | |
//x, y, width and height constraints | |
priceLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
priceLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true | |
priceLabel.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -40).isActive = true | |
priceLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true | |
} | |
func setupRefreshButton() { | |
//x, y, width and height constraints | |
refreshButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
refreshButton.topAnchor.constraint(equalTo: priceLabel.bottomAnchor, constant: 16).isActive = true | |
refreshButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -40).isActive = true | |
refreshButton.heightAnchor.constraint(equalToConstant: 50).isActive = true | |
} | |
} | |
extension ViewController { | |
//fetch & parse json from api | |
func fetchData() { | |
let urlString = "https://www.bitstamp.net/api/v2/ticker/btcusd/" | |
let url = URL(string: urlString) | |
let urlRequest = URLRequest(url: url!) | |
let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in | |
if error != nil { | |
print(error) | |
return | |
} | |
do { | |
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] | |
if let price = json["last"] as? String, let low = json["low"] as? String, let high = json["high"] as? String { | |
self.priceLabel.text = price | |
} | |
} catch let error { | |
print(error) | |
} | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment