Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 10.20 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  WeatherApp
  4. //
  5. //  Created by Jakub on 10/19/18.
  6. //  Copyright © 2018 Jakub. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class DetailViewController: UIViewController{
  12.    
  13.    
  14.     private var forecasts: [Weather] = []
  15.     private var currentIndex: Int = 1
  16.    
  17.     var cityForecasts: CityForecasts? {
  18.         didSet {
  19.             self.refreshUI()
  20.         }
  21.     }
  22.  
  23.     //City label
  24.     @IBOutlet weak var city: UILabel!
  25.    
  26.     //selected date label
  27.     @IBOutlet weak var date: UILabel!
  28.    
  29.     //PREVIOUS/NEXT buttons
  30.     @IBOutlet weak var PreviousBtn: UIButton!
  31.     @IBOutlet weak var NextBtn: UIButton!
  32.    
  33.     //Weather icon
  34.     @IBOutlet weak var WeatherIcon: UIImageView!
  35.    
  36.     //Summary
  37.     @IBOutlet weak var summary: UILabel!
  38.    
  39.     //TEMPERATURE
  40.     @IBOutlet weak var temperature: UILabel!
  41.     @IBOutlet weak var temperatureMax: UILabel!
  42.     @IBOutlet weak var temperatureMin: UILabel!
  43.    
  44.     //PRECIPATION
  45.     @IBOutlet weak var precipationType: UILabel!
  46.     @IBOutlet weak var precipationIntensity: UILabel!
  47.     @IBOutlet weak var precipationProbability: UILabel!
  48.    
  49.     //WIND
  50.     @IBOutlet weak var windSpeed: UILabel!
  51.     @IBOutlet weak var windDirection: UILabel!
  52.    
  53.     //AIR PRESSURE
  54.     @IBOutlet weak var airPressure: UILabel!
  55.    
  56.     @IBAction func swipeLeft(_ sender: Any) {
  57.         self.setNextDay()
  58.     }
  59.    
  60.     @IBAction func swipeRight(_ sender: Any) {
  61.         self.setPreviousDay()
  62.     }
  63.    
  64.     @IBAction func previousDayBtn(_ sender: UIButton) {
  65.         self.setPreviousDay()
  66.     }
  67.    
  68.     @IBAction func nextDayBtn(_ sender: UIButton) {
  69.         self.setNextDay()
  70.     }
  71.        
  72.     @IBOutlet var allLabels: [UILabel]!
  73.     @IBOutlet var all_Icons: [UIImageView]!
  74.     @IBOutlet var allButtons: [UIButton]!
  75.    
  76.    
  77.     override func viewDidLoad() {
  78.         super.viewDidLoad()
  79.         setBackgroundImage()
  80.        
  81.        
  82.         //        // Create the Activity Indicator
  83.         //        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
  84.         //        self.view.addSubview(activityIndicator)
  85.         //        activityIndicator.frame = view.bounds
  86.         //        activityIndicator.startAnimating()
  87.        
  88.         //self.setUIVisibility(isUIVisible: false) //make UI invisible until data is loaded
  89.        
  90.     }
  91.    
  92.     func setBackgroundImage(){
  93.         PreviousBtn.layer.cornerRadius = 23
  94.         NextBtn.layer.cornerRadius = 23
  95.         let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
  96.         backgroundImage.image = UIImage(named: "WeatherAppBG.png")
  97.         backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
  98.         self.view.insertSubview(backgroundImage, at: 0)
  99.     }
  100.    
  101.     func setUIVisibility(isUIVisible: Bool){
  102.         var alpha = 1
  103.         if(!isUIVisible) {
  104.             alpha = 0
  105.         }
  106.         for label in self.allLabels {
  107.             label.alpha = CGFloat(alpha)
  108.         }
  109.         for image in self.all_Icons {
  110.             image.alpha = CGFloat(alpha)
  111.         }
  112.         for button in self.allButtons {
  113.             button.alpha = CGFloat(alpha)
  114.             if (alpha == 0) {
  115.                 button.isEnabled = false
  116.             } else {
  117.                 button.isEnabled = true
  118.             }
  119.         }
  120.     }
  121.    
  122.     func formatDate(unixTimestamp: Int32) -> String {
  123.         let date = Date(timeIntervalSince1970: Double(unixTimestamp))
  124.         let dateFormatter = DateFormatter()
  125.         dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
  126.         dateFormatter.locale = NSLocale.current
  127.         dateFormatter.dateFormat = "dd-MM-yyyy"
  128.         let strDate = dateFormatter.string(from: date)
  129.         return strDate
  130.     }
  131.    
  132.     func setNextDay() -> (){
  133.         if(!(self.currentIndex == forecasts.count - 1)){
  134.             self.currentIndex += 1
  135.             print(self.currentIndex)
  136.             let selectedDay = forecasts[self.currentIndex]
  137.             updateView(selectedDay: selectedDay, transitionDirection: kCATransitionFromRight)
  138.             if (!PreviousBtn.isEnabled) {
  139.                 PreviousBtn.isEnabled = true
  140.                 PreviousBtn.alpha = 1
  141.             }
  142.             if(self.currentIndex == forecasts.count - 1){
  143.                 NextBtn.isEnabled = false
  144.                 NextBtn.alpha = 0.5
  145.             }
  146.         }
  147.     }
  148.    
  149.     func setPreviousDay() -> (){
  150.         if(!(self.currentIndex == 0)){
  151.             self.currentIndex -= 1
  152.             print(self.currentIndex)
  153.             let selectedDay = forecasts[self.currentIndex]
  154.             updateView(selectedDay: selectedDay, transitionDirection: kCATransitionFromLeft)
  155.             if (!NextBtn.isEnabled) {
  156.                 NextBtn.isEnabled = true
  157.                 NextBtn.alpha = 1
  158.             }
  159.             if(self.currentIndex == 0){
  160.                 PreviousBtn.isEnabled = false
  161.                 PreviousBtn.alpha = 0.5
  162.             }
  163.         }
  164.     }
  165.    
  166.  
  167.     func refreshUI() {
  168.         loadViewIfNeeded()
  169.         self.city.text = cityForecasts!.address
  170.         forecasts = cityForecasts!.forecasts
  171.         let selectedDay: Weather = forecasts[1]
  172.         print(cityForecasts!.address)
  173.         print(selectedDay)
  174.         updateView(selectedDay: selectedDay, transitionDirection: kCATransitionFromBottom)
  175.     }
  176.    
  177.    
  178.     func updateView(selectedDay: Weather, transitionDirection: String) -> (){
  179.         let t = 0.4 //transition time in seconds
  180.        
  181.         self.date.pushTransition(t, transitionDirection)
  182.         self.date.text = self.formatDate(unixTimestamp: selectedDay.time)
  183.        
  184.         self.WeatherIcon.fadeTransition(t)
  185.         self.WeatherIcon.image = chooseIconImage(iconType: selectedDay.icon)
  186.        
  187.         self.temperature.pushTransition(t, transitionDirection)
  188.         self.temperature.text = String((selectedDay.temperatureMin + selectedDay.temperatureMax)/2) + " °C"
  189.        
  190.         self.temperatureMax.pushTransition(t, transitionDirection)
  191.         self.temperatureMax.text = String(selectedDay.temperatureMax) + " °C"
  192.        
  193.         self.temperatureMin.pushTransition(t, transitionDirection)
  194.         self.temperatureMin.text = String(selectedDay.temperatureMin) + " °C"
  195.        
  196.         self.precipationType.pushTransition(t, transitionDirection)
  197.         self.precipationType.text = String(selectedDay.precipType)
  198.        
  199.         self.precipationIntensity.pushTransition(t, transitionDirection)
  200.         self.precipationIntensity.text = String(selectedDay.precipIntensity)
  201.        
  202.         self.precipationProbability.pushTransition(t, transitionDirection)
  203.         self.precipationProbability.text = String(selectedDay.precipProbability) + "%"
  204.        
  205.         self.airPressure.pushTransition(t, transitionDirection)
  206.         self.airPressure.text = String(selectedDay.pressure) + " hPa"
  207.        
  208.         self.windSpeed.pushTransition(t, transitionDirection)
  209.         self.windSpeed.text = String(selectedDay.windSpeed) + " km/h"
  210.        
  211.         self.windDirection.pushTransition(t, transitionDirection)
  212.         self.windDirection.text = calculateWindDirection(windBearing: selectedDay.windBearing)
  213.        
  214.         self.summary.pushTransition(t, transitionDirection)
  215.         self.summary.text = selectedDay.summary
  216.     }
  217.    
  218.     func chooseIconImage(iconType: String) -> UIImage {
  219.         switch(iconType){
  220.         case "clear-day":
  221.             return #imageLiteral(resourceName: "weatherIcon1@160x160")
  222.         case "clear-night":
  223.             return #imageLiteral(resourceName: "weatherIcon9@160x160")
  224.         case "rain":
  225.             return #imageLiteral(resourceName: "weatherIcon17@160x160")
  226.         case "snow":
  227.             return #imageLiteral(resourceName: "weatherIcon19@160x160")
  228.         case "sleet":
  229.             return #imageLiteral(resourceName: "weatherIcon19@160x160")
  230.         case "wind":
  231.             return #imageLiteral(resourceName: "weatherIcon15@160x160")
  232.         case "fog":
  233.             return #imageLiteral(resourceName: "weatherIcon14@160x160")
  234.         case "cloudy":
  235.             return #imageLiteral(resourceName: "weatherIcon16@160x160")
  236.         case "partly-cloudy-day":
  237.             return #imageLiteral(resourceName: "weatherIcon4@160x160")
  238.         case "partly-cloudy-night":
  239.             return #imageLiteral(resourceName: "weatherIcon4@160x160")
  240.         default:
  241.             return #imageLiteral(resourceName: "weatherIcon2@160x160")
  242.         }
  243.     }
  244.    
  245.     func calculateWindDirection(windBearing: Int) -> String {
  246.         if(windBearing > 22 && windBearing <= 67){
  247.             return "N/E"
  248.         }else if(windBearing > 67 && windBearing <= 112){
  249.             return "E"
  250.         }else if(windBearing > 112 && windBearing <= 157){
  251.             return "S/E"
  252.         }else if(windBearing > 157 && windBearing <= 202){
  253.             return "S"
  254.         }else if(windBearing > 202 && windBearing <= 247){
  255.             return "S/W"
  256.         }else if(windBearing > 247 && windBearing <= 292){
  257.             return "W"
  258.         }else if(windBearing > 292 && windBearing <= 337){
  259.             return "N/W"
  260.         }else{
  261.             return "N"
  262.         }
  263.     }
  264.    
  265.     override func didReceiveMemoryWarning() {
  266.         super.didReceiveMemoryWarning()
  267.         // Dispose of any resources that can be recreated.
  268.     }
  269. }
  270.  
  271. extension DetailViewController: CityForecastsSelectionDelegate {
  272.     func cityForecastsSelected(_ newCityForecasts: CityForecasts) {
  273.         self.cityForecasts = newCityForecasts
  274.     }
  275. }
  276.  
  277. extension UIView {
  278.     func pushTransition(_ duration:CFTimeInterval, _ direction: String) {
  279.         let animation = CATransition()
  280.         animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
  281.         animation.type = kCATransitionPush
  282.         animation.subtype = direction
  283.         animation.duration = duration
  284.         layer.add(animation, forKey: kCATransitionPush)
  285.     }
  286.    
  287.     func fadeTransition(_ duration:CFTimeInterval) {
  288.         let animation = CATransition()
  289.         animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
  290.         animation.type = kCATransitionFade
  291.         animation.duration = duration
  292.         layer.add(animation, forKey: kCATransitionFade)
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement