Advertisement
renurtt

Untitled

Mar 7th, 2020
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.43 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  MyStocks
  4. //
  5. //  Created by Renat Nurtdinov on 04.03.2020.
  6. //  Copyright © 2020 Renat Nurtdinov. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
  12.    
  13.     @IBOutlet weak var companyNameLabel: UILabel!
  14.     @IBOutlet weak var companyPickerView: UIPickerView!
  15.     @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
  16.     @IBOutlet weak var companySymbolLabel: UILabel!
  17.     @IBOutlet weak var priceLabel: UILabel!
  18.     @IBOutlet weak var priceChangeLabel: UILabel!
  19.    
  20.    
  21.     func numberOfComponents(in pickerView: UIPickerView) -> Int {
  22.         return 1
  23.     }
  24.    
  25.     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  26.         return self.companies.keys.count
  27.     }
  28.    
  29.     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  30.         return Array(self.companies.keys)[row]
  31.     }
  32.    
  33.     private let companies : [String: String] = ["Apple": "AAPL",
  34.                                                 "Microsoft": "MSFT",
  35.                                                 "Google": "GOOG",
  36.                                                 "Amazon": "AMZN",
  37.                                                 "Facebook": "FB"]
  38.     override func viewDidLoad() {
  39.         super.viewDidLoad()
  40.        
  41.         self.companyPickerView.dataSource = self
  42.         self.companyPickerView.delegate = self
  43.        
  44.         activityIndicator.hidesWhenStopped = true;
  45.        
  46.         self.requestQuoteUpdate()
  47.     }
  48.    
  49.     private func requestQuoteUpdate() {
  50.         activityIndicator.startAnimating()
  51.         self.companyNameLabel.text = "-"
  52.         self.companySymbolLabel.text = "-"
  53.         self.priceLabel.text = "-"
  54.         self.priceChangeLabel.text = "-"
  55.         self.priceChangeLabel.textColor = UIColor(red: 0, green: 0, blue: 0, alpha:1.0)
  56.        
  57.         let selectedRow = self.companyPickerView.selectedRow(inComponent: 0)
  58.         let selectedSymbol = Array(self.companies.values)[selectedRow]
  59.         requestQuote(for: selectedSymbol)
  60.     }
  61.     func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  62.         self.requestQuoteUpdate()
  63.     }
  64.    
  65.     private func requestQuote(for symbol: String) {
  66.         let url = URL(string: "https://cloud.iexapis.com/stable/stock/\(symbol)/quote/?token=pk_d588c24949f1445a9750a04d43d9a360")!
  67.         let dataTask = URLSession.shared.dataTask(with: url) {
  68.             data, response, error in
  69.             guard
  70.                 error == nil,
  71.                 (response as? HTTPURLResponse)?.statusCode == 200,
  72.                 let data = data
  73.                 else {
  74.                     print("Network error")
  75.                     return
  76.             }
  77.             self.parseQuote(data: data)
  78.         }
  79.         dataTask.resume()
  80.     }
  81.    
  82.     private func parseQuote(data: Data) {
  83.         do {
  84.             let jsonObject = try JSONSerialization.jsonObject(with: data)
  85.             guard
  86.                 let json = jsonObject as? [String: Any],
  87.                 let companyName = json["companyName"] as? String,
  88.                 let companySymbol = json["symbol"] as? String,
  89.                 let price = json["latestPrice"] as? Double,
  90.                 let priceChange = json["change"] as? Double
  91.                 else {
  92.                     print("Invalid JSON format")
  93.                     return
  94.             }
  95.             DispatchQueue.main.async {
  96.                 self.displayStockInfo(companyName: companyName, symbol: companySymbol, price: price, priceChange: priceChange)
  97.             }
  98.         }
  99.         catch {
  100.             print("JSON parsing error: " + error.localizedDescription)
  101.         }
  102.     }
  103.    
  104.     private func displayStockInfo(companyName: String, symbol : String, price: Double, priceChange: Double) {
  105.         activityIndicator.stopAnimating()
  106.         self.companyNameLabel.text = companyName
  107.         self.companySymbolLabel.text = symbol
  108.         self.priceLabel.text = "\(price)"
  109.        
  110.         self.priceChangeLabel.text = "\(priceChange)"
  111.         if priceChange>0 {
  112.             self.priceChangeLabel.textColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
  113.         }
  114.         if priceChange<0 {
  115.             self.priceChangeLabel.textColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement