-
Notifications
You must be signed in to change notification settings - Fork 7
Roman Ekimov #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
toywar
wants to merge
1
commit into
cocoaheadsru:master
Choose a base branch
from
toywar:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Roman Ekimov #8
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,9 @@ | ||
| // | ||
| // AppDelegate.swift | ||
| // Rainy | ||
| // | ||
| // Created by Kirill Averyanov on 17/10/16. | ||
| // Copyright © 2016 Kirill Averyanov. All rights reserved. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| @UIApplicationMain | ||
| class AppDelegate: UIResponder, UIApplicationDelegate { | ||
|
|
||
| var window: UIWindow? | ||
|
|
||
|
|
||
| func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | ||
| // Override point for customization after application launch. | ||
| return true | ||
| } | ||
|
|
||
| func applicationWillResignActive(_ application: UIApplication) { | ||
| // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. | ||
| // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. | ||
| } | ||
|
|
||
| func applicationDidEnterBackground(_ application: UIApplication) { | ||
| // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. | ||
| // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. | ||
| } | ||
|
|
||
| func applicationWillEnterForeground(_ application: UIApplication) { | ||
| // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. | ||
| } | ||
|
|
||
| func applicationDidBecomeActive(_ application: UIApplication) { | ||
| // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. | ||
| } | ||
|
|
||
|
|
||
|
|
||
| func applicationWillTerminate(_ application: UIApplication) { | ||
| // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| var window: UIWindow? | ||
| func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | ||
| return true | ||
| } | ||
| } |
278 changes: 131 additions & 147 deletions
278
SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,168 +1,152 @@ | ||
| // | ||
| // FirstViewController.swift | ||
| // Rainy | ||
| // | ||
| // Created by Kirill Averyanov on 17/10/16. | ||
| // Copyright © 2016 Kirill Averyanov. All rights reserved. | ||
| // | ||
|
|
||
| import UIKit | ||
| import CoreLocation | ||
| import Alamofire | ||
| import SwiftyJSON | ||
|
|
||
|
|
||
|
|
||
| struct coords{ | ||
| var lat: Double = 0 | ||
| var lon: Double = 0 | ||
| struct coords { | ||
| var lat: Double = 0 | ||
| var lon: Double = 0 | ||
| } | ||
|
|
||
| class CurrentWeatherViewController: UIViewController, CLLocationManagerDelegate { | ||
| @IBOutlet weak var temperatureLabel: UILabel! | ||
| @IBOutlet weak var timeLabel: UILabel! | ||
| @IBOutlet weak var stateLabel: UILabel! | ||
| @IBOutlet weak var windLabel: UILabel! | ||
| @IBOutlet weak var cityNameLabel: UILabel! | ||
| @IBOutlet weak var imageWeather: UIImageView! | ||
|
|
||
| private var locationManager: CLLocationManager! | ||
| // private let constrain: Constants = Constants() | ||
| private var currentForecast: WeatherForecast? { | ||
| didSet{ | ||
| reloadUI() | ||
| } | ||
| @IBOutlet weak var temperatureLabel: UILabel! | ||
| @IBOutlet weak var timeLabel: UILabel! | ||
| @IBOutlet weak var stateLabel: UILabel! | ||
| @IBOutlet weak var windLabel: UILabel! | ||
| @IBOutlet weak var cityNameLabel: UILabel! | ||
| @IBOutlet weak var imageWeather: UIImageView! | ||
|
|
||
| private var locationManager: CLLocationManager! | ||
| private var currentForecast: WeatherForecast? { | ||
| didSet{ | ||
| reloadUI() | ||
| } | ||
| } | ||
|
|
||
| private let photoResources: [String: UIImage] = [ | ||
| "01d":#imageLiteral(resourceName: "sunny"), | ||
| "01n":#imageLiteral(resourceName: "moon"), | ||
| "02d":#imageLiteral(resourceName: "sunny_clouds"), | ||
| "02n":#imageLiteral(resourceName: "moodCloud"), | ||
| "03d":#imageLiteral(resourceName: "clouds"), | ||
| "03n":#imageLiteral(resourceName: "clouds"), | ||
| "04d":#imageLiteral(resourceName: "clouds"), | ||
| "04n":#imageLiteral(resourceName: "clouds"), | ||
| "09d":#imageLiteral(resourceName: "cloud_rain"), | ||
| "09n":#imageLiteral(resourceName: "cloud_rain"), | ||
| "10d":#imageLiteral(resourceName: "sunCloudRain"), | ||
| "10n":#imageLiteral(resourceName: "moonrain"), | ||
| "11d":#imageLiteral(resourceName: "storm"), | ||
| "11n":#imageLiteral(resourceName: "storm"), | ||
| "13d":#imageLiteral(resourceName: "CloudSnow"), | ||
| "13n":#imageLiteral(resourceName: "CloudSnow"), | ||
| "50d":#imageLiteral(resourceName: "fog"), | ||
| "50n":#imageLiteral(resourceName: "fog") | ||
| ] | ||
|
|
||
| private var myCoords: coords = coords() | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| locationManager = CLLocationManager() | ||
| locationManager.delegate = self | ||
| locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers | ||
| if CLLocationManager.authorizationStatus() == .notDetermined { | ||
| locationManager.requestWhenInUseAuthorization() | ||
| } | ||
|
|
||
| private let photoResources: [String: UIImage] = [ | ||
| "01d":#imageLiteral(resourceName: "sunny"), | ||
| "01n":#imageLiteral(resourceName: "moon"), | ||
| "02d":#imageLiteral(resourceName: "sunny_clouds"), | ||
| "02n":#imageLiteral(resourceName: "moodCloud"), | ||
| "03d":#imageLiteral(resourceName: "clouds"), | ||
| "03n":#imageLiteral(resourceName: "clouds"), | ||
| "04d":#imageLiteral(resourceName: "clouds"), | ||
| "04n":#imageLiteral(resourceName: "clouds"), | ||
| "09d":#imageLiteral(resourceName: "cloud_rain"), | ||
| "09n":#imageLiteral(resourceName: "cloud_rain"), | ||
| "10d":#imageLiteral(resourceName: "sunCloudRain"), | ||
| "10n":#imageLiteral(resourceName: "moonrain"), | ||
| "11d":#imageLiteral(resourceName: "storm"), | ||
| "11n":#imageLiteral(resourceName: "storm"), | ||
| "13d":#imageLiteral(resourceName: "CloudSnow"), | ||
| "13n":#imageLiteral(resourceName: "CloudSnow"), | ||
| "50d":#imageLiteral(resourceName: "fog"), | ||
| "50n":#imageLiteral(resourceName: "fog")] | ||
|
|
||
|
|
||
| private var myCoords: coords = coords() | ||
| if CLLocationManager.locationServicesEnabled() { | ||
| locationManager.startUpdatingLocation() | ||
| } | ||
|
|
||
| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: { | ||
| self.updateCurrentForecast() | ||
| }) | ||
| } | ||
|
|
||
| @IBAction func reloadButtonPressed(_ sender: AnyObject) { | ||
| if(CLLocationManager.authorizationStatus() == .notDetermined) { | ||
| locationManager.requestWhenInUseAuthorization() | ||
| } | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| locationManager = CLLocationManager() | ||
| locationManager.delegate = self | ||
| locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers | ||
| if(CLLocationManager.authorizationStatus() == .notDetermined){ | ||
| locationManager.requestWhenInUseAuthorization() | ||
| } | ||
|
|
||
| if CLLocationManager.locationServicesEnabled(){ | ||
| locationManager.startUpdatingLocation() | ||
| } | ||
| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: { | ||
| self.updateCurrentForecast() | ||
| }) | ||
| if CLLocationManager.locationServicesEnabled(){ | ||
| locationManager.startUpdatingLocation() | ||
| } | ||
|
|
||
| @IBAction func reloadButtonPressed(_ sender: AnyObject) { | ||
| if(CLLocationManager.authorizationStatus() == .notDetermined){ | ||
| locationManager.requestWhenInUseAuthorization() | ||
| } | ||
|
|
||
| if CLLocationManager.locationServicesEnabled(){ | ||
| locationManager.startUpdatingLocation() | ||
| updateCurrentForecast() | ||
| } | ||
|
|
||
| internal func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { | ||
| print("error: ", error) | ||
| } | ||
|
|
||
| internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
| self.myCoords.lat = locations[0].coordinate.latitude | ||
| self.myCoords.lon = locations[0].coordinate.longitude | ||
| locationManager.stopUpdatingLocation() | ||
| } | ||
|
|
||
|
|
||
| private func updateCurrentForecast() { | ||
| Alamofire.request("http://api.openweathermap.org/data/2.5/weather", method: .get, parameters: ["lat": myCoords.lat, "lon": myCoords.lon, "APPID": "", "units": "metric"], encoding: JSONEncoding.default, headers: nil).responseJSON { [weak self] response in | ||
| guard response.result.isSuccess else { | ||
| return | ||
| } | ||
|
|
||
| let json = JSON(response.result.value as Any) | ||
| self?.currentForecast = WeatherForecast(currentWeatherTempurature: round(10 * json["main"]["temp"].doubleValue) / 10, | ||
| timeStamp: (self?.getCurrentTime())!, | ||
| imageName: json["weather"][0]["icon"].string!, | ||
| locationCoordinates: (self?.myCoords.lat, self?.myCoords.lon) as? (Double, Double), | ||
| humidity: json["main"]["humidity"].int, | ||
| pressure: json["main"]["pressure"].int, | ||
| wind: round(10 * json["wind"]["speed"].doubleValue) / 10, cityName: json["name"].string, | ||
| stateWeather: json["weather"][0]["description"].string) | ||
| } | ||
| } | ||
|
|
||
| private func getTrafficInformation() { | ||
| Alamofire.request("http://www.mapquestapi.com/traffic/v2/incidents", | ||
| parameters: ["boundingBox": "\(myCoords.lat),\(myCoords.lon),\(myCoords.lat - 1),\(myCoords.lon - 1)", | ||
| "key": ""]) | ||
| .responseJSON{ response in | ||
| guard response.result.isSuccess else{ | ||
| return | ||
| } | ||
| updateCurrentForecast() | ||
| _ = JSON(response.result.value!) | ||
| } | ||
|
|
||
|
|
||
| internal func locationManager(_ manager: CLLocationManager, | ||
| didFailWithError error: Error) { | ||
| print("error: ", error) | ||
| } | ||
|
|
||
| private func getCurrentTime() -> String { | ||
| let date = Date() | ||
| let calendar = Calendar.current | ||
| var currentTime: String | ||
| let components = calendar.dateComponents([.hour, .minute], from: date as Date!) | ||
| var hour: String = String(describing: components.hour!) | ||
| var minute: String = String(describing: components.minute!) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Force castings |
||
| if hour.characters.count == 1 { | ||
| hour = "0" + hour | ||
| } | ||
|
|
||
| internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
| self.myCoords.lat = locations[0].coordinate.latitude | ||
| self.myCoords.lon = locations[0].coordinate.longitude | ||
| locationManager.stopUpdatingLocation() | ||
| if minute.characters.count == 1 { | ||
| minute = "0" + minute | ||
| } | ||
|
|
||
|
|
||
| private func updateCurrentForecast(){ | ||
| Alamofire.request("http://api.openweathermap.org/data/2.5/weather",method: .get,parameters: ["lat":myCoords.lat,"lon": myCoords.lon,"APPID": "","units":"metric"],encoding: JSONEncoding.default,headers: nil).responseJSON{response in | ||
| guard response.result.isSuccess else{ | ||
| return | ||
| } | ||
| let json = JSON(response.result.value!) | ||
| self.currentForecast = WeatherForecast(currentWeatherTempurature: round(10 * json["main"]["temp"].doubleValue) / 10, | ||
| timeStamp: self.getCurrentTime(), | ||
| imageName: json["weather"][0]["icon"].string!, | ||
| locationCoordinates: (self.myCoords.lat, self.myCoords.lon), | ||
| humidity: json["main"]["humidity"].int, | ||
| pressure: json["main"]["pressure"].int, | ||
| wind: round(10 * json["wind"]["speed"].doubleValue) / 10, cityName: json["name"].string, | ||
| stateWeather: json["weather"][0]["description"].string) | ||
| } | ||
| currentTime = "\(hour):\(minute)" | ||
| return currentTime | ||
| } | ||
|
|
||
| private func reloadUI() { | ||
| timeLabel.text = "Updated: \(String(describing: currentForecast?.timeStamp))" | ||
| if let temp = currentForecast?.currentWeatherTempurature { | ||
| temperatureLabel.text = "\(temp)℃" | ||
| } | ||
|
|
||
| private func getTrafficInformation(){ | ||
| Alamofire.request("http://www.mapquestapi.com/traffic/v2/incidents", | ||
| parameters: ["boundingBox": "\(myCoords.lat),\(myCoords.lon),\(myCoords.lat - 1),\(myCoords.lon - 1)", | ||
| "key": ""]) | ||
| .responseJSON{response in | ||
| guard response.result.isSuccess else{ | ||
| return | ||
| } | ||
| _ = JSON(response.result.value!) | ||
| } | ||
| if let city = currentForecast?.cityName { | ||
| cityNameLabel.text = city | ||
| } | ||
|
|
||
| private func getCurrentTime() -> String{ | ||
| let date = NSDate() | ||
| let calendar = NSCalendar.current | ||
| var currentTime: String | ||
| let components = calendar.dateComponents([.hour, .minute], from: date as Date!) | ||
| var hour: String = String(describing: components.hour!) | ||
| var minute: String = String(describing: components.minute!) | ||
| if hour.characters.count == 1{ | ||
| hour = "0" + hour | ||
| } | ||
| if(minute.characters.count == 1){ | ||
| minute = "0" + minute | ||
| } | ||
| currentTime = "\(hour):\(minute)" | ||
| return currentTime | ||
| if let wi = currentForecast?.wind { | ||
| windLabel.text = "\(wi)" | ||
| } | ||
|
|
||
|
|
||
|
|
||
| private func reloadUI(){ | ||
| timeLabel.text = "Updated: \(currentForecast!.timeStamp)" | ||
| if let temp = currentForecast?.currentWeatherTempurature{ | ||
| temperatureLabel.text = "\(temp)℃" | ||
| } | ||
| if let city = currentForecast?.cityName{ | ||
| cityNameLabel.text = city | ||
| } | ||
| if let wi = currentForecast?.wind{ | ||
| windLabel.text = "\(wi)" | ||
| } | ||
| if let st = currentForecast?.stateWeather{ | ||
| stateLabel.text = st | ||
| } | ||
| imageWeather.image = photoResources[(currentForecast?.imageName)!] | ||
| if let st = currentForecast?.stateWeather { | ||
| stateLabel.text = st | ||
| } | ||
| } | ||
|
|
||
|
|
||
| imageWeather.image = photoResources[(currentForecast?.imageName)!] | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не нужно столько переводов строк