From 9ad649e9d507a1579ca20104247073003ccae650 Mon Sep 17 00:00:00 2001 From: Ekaterina Date: Fri, 27 Oct 2017 12:25:34 +0300 Subject: [PATCH] SwiftStyle --- .../Rainy/Rainy.xcodeproj/project.pbxproj | 4 ++ SwiftStyle/Rainy/Rainy/AppDelegate.swift | 32 ------------- SwiftStyle/Rainy/Rainy/Constants.swift | 46 +++++++++++++++++++ .../Rainy/CurrentWeatherViewController.swift | 18 ++++---- .../Rainy/Rainy/SettingsViewController.swift | 41 ++++++++--------- 5 files changed, 78 insertions(+), 63 deletions(-) create mode 100644 SwiftStyle/Rainy/Rainy/Constants.swift diff --git a/SwiftStyle/Rainy/Rainy.xcodeproj/project.pbxproj b/SwiftStyle/Rainy/Rainy.xcodeproj/project.pbxproj index cf71440..c05fce2 100644 --- a/SwiftStyle/Rainy/Rainy.xcodeproj/project.pbxproj +++ b/SwiftStyle/Rainy/Rainy.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 7B6889F11DC4F2400014AC1D /* SwiftyJSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B6889F01DC4F2400014AC1D /* SwiftyJSON.framework */; }; 7BDCE5CE1DBBBFE100167625 /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7BDCE5CD1DBBBFE100167625 /* Alamofire.framework */; settings = {ATTRIBUTES = (Required, ); }; }; 8C1C6795E2AFA6AA9F1CF7C5 /* Pods_Rainy.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4EA8E15D92D5168D0F28BAE /* Pods_Rainy.framework */; }; + E10D115B1FA32EC900649D1F /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = E10D115A1FA32EC900649D1F /* Constants.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -38,6 +39,7 @@ 7BDCE5CD1DBBBFE100167625 /* Alamofire.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Alamofire.framework; path = "../../../../Library/Developer/Xcode/DerivedData/Rainy-aldgivkqveeobkekmqalrgpntaib/Build/Products/Debug-iphonesimulator/Alamofire/Alamofire.framework"; sourceTree = ""; }; 8EBFC4D7F526D8ADA40CD634 /* Pods-Rainy.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Rainy.release.xcconfig"; path = "Pods/Target Support Files/Pods-Rainy/Pods-Rainy.release.xcconfig"; sourceTree = ""; }; A34481B21E8E732C8A2F6650 /* Pods-Rainy.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Rainy.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Rainy/Pods-Rainy.debug.xcconfig"; sourceTree = ""; }; + E10D115A1FA32EC900649D1F /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; F4EA8E15D92D5168D0F28BAE /* Pods_Rainy.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Rainy.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -82,6 +84,7 @@ 7B11FC411DB53A6900FA6122 /* HomeWeatherViewController.swift */, 7B6469FA1DC643AE00AA44F2 /* SettingsViewController.swift */, 7B11FC531DB540E900FA6122 /* WeatherForecast.swift */, + E10D115A1FA32EC900649D1F /* Constants.swift */, 7B11FC431DB53A6900FA6122 /* Main.storyboard */, 7B11FC461DB53A6900FA6122 /* Assets.xcassets */, 7B11FC481DB53A6900FA6122 /* LaunchScreen.storyboard */, @@ -249,6 +252,7 @@ 7B11FC3E1DB53A6900FA6122 /* AppDelegate.swift in Sources */, 7B11FC401DB53A6900FA6122 /* CurrentWeatherViewController.swift in Sources */, 7B6469FB1DC643AE00AA44F2 /* SettingsViewController.swift in Sources */, + E10D115B1FA32EC900649D1F /* Constants.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/SwiftStyle/Rainy/Rainy/AppDelegate.swift b/SwiftStyle/Rainy/Rainy/AppDelegate.swift index 9acb4d2..deafa95 100644 --- a/SwiftStyle/Rainy/Rainy/AppDelegate.swift +++ b/SwiftStyle/Rainy/Rainy/AppDelegate.swift @@ -13,40 +13,8 @@ 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:. - } - - } - - - - - diff --git a/SwiftStyle/Rainy/Rainy/Constants.swift b/SwiftStyle/Rainy/Rainy/Constants.swift new file mode 100644 index 0000000..5947205 --- /dev/null +++ b/SwiftStyle/Rainy/Rainy/Constants.swift @@ -0,0 +1,46 @@ +// +// Constants.swift +// Rainy +// +// Created by Ekaterina Nesterova on 27.10.2017. +// Copyright © 2017 Kirill Averyanov. All rights reserved. +// + +import Foundation + +struct URL { + + static let weatherURL = "http://api.openweathermap.org/data/2.5/weather" + static let trafficURL = "http://www.mapquestapi.com/traffic/v2/incidents" + +} + +struct paramKeys { + + static let latitude = "lat" + static let longtitude = "lon" + static let addId = "APPID" + static let units = "units" + +} + +struct paramValues { + + static let metric = "metric" + +} + +struct jsonTags { + + static let weather = "weather" + static let temperature = "temp" + static let icon = "icon" + static let jsonMain = "main" + static let humidity = "humidity" + static let pressure = "pressure" + static let wind = "wind" + static let speed = "speed" + static let name = "name" + static let jsonDescription = "description" + +} diff --git a/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift b/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift index 89abfb3..8349132 100644 --- a/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift +++ b/SwiftStyle/Rainy/Rainy/CurrentWeatherViewController.swift @@ -11,8 +11,6 @@ import CoreLocation import Alamofire import SwiftyJSON - - struct coords{ var lat: Double = 0 var lon: Double = 0 @@ -63,7 +61,7 @@ class CurrentWeatherViewController: UIViewController, CLLocationManagerDelegate locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers - if(CLLocationManager.authorizationStatus() == .notDetermined){ + if(CLLocationManager.authorizationStatus() == .notDetermined) { locationManager.requestWhenInUseAuthorization() } @@ -80,7 +78,7 @@ class CurrentWeatherViewController: UIViewController, CLLocationManagerDelegate locationManager.requestWhenInUseAuthorization() } - if CLLocationManager.locationServicesEnabled(){ + if CLLocationManager.locationServicesEnabled() { locationManager.startUpdatingLocation() } updateCurrentForecast() @@ -100,24 +98,24 @@ class CurrentWeatherViewController: UIViewController, CLLocationManagerDelegate 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 + Alamofire.request(URL.weatherURL, method: .get,parameters: [paramKeys.latitude:myCoords.lat,paramKeys.longtitude: myCoords.lon,paramKeys.addId: "",paramKeys.units:paramValues.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, + self.currentForecast = WeatherForecast(currentWeatherTempurature: round(10 * json[jsonTags.jsonMain][jsonTags.temperature].doubleValue) / 10, timeStamp: self.getCurrentTime(), - imageName: json["weather"][0]["icon"].string!, + imageName: json[jsonTags.jsonMain][0][jsonTags.icon].string!, locationCoordinates: (self.myCoords.lat, self.myCoords.lon), - humidity: json["main"]["humidity"].int, - pressure: json["main"]["pressure"].int, + humidity: json[jsonTags.jsonMain][jsonTags.humidity].int, + pressure: json[jsonTags.jsonMain]["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", + Alamofire.request(URL.trafficURL, parameters: ["boundingBox": "\(myCoords.lat),\(myCoords.lon),\(myCoords.lat - 1),\(myCoords.lon - 1)", "key": ""]) .responseJSON{response in diff --git a/SwiftStyle/Rainy/Rainy/SettingsViewController.swift b/SwiftStyle/Rainy/Rainy/SettingsViewController.swift index 87461a0..8032fc3 100644 --- a/SwiftStyle/Rainy/Rainy/SettingsViewController.swift +++ b/SwiftStyle/Rainy/Rainy/SettingsViewController.swift @@ -9,28 +9,27 @@ import UIKit class SettingsViewController: UIViewController { - @IBOutlet weak var textField: UITextField! - @IBOutlet weak var swifthLocation: UISwitch! - - @IBAction func saveButtonPressed(_ sender: Any) { - self.view.endEditing(true) - setts.set(textField.text!, forKey: "cityName") - } - - override func touchesBegan(_ touches: Set, with event: UIEvent?) { + @IBOutlet weak var textField: UITextField! + @IBOutlet weak var swifthLocation: UISwitch! + + @IBAction func saveButtonPressed(_ sender: Any) { + self.view.endEditing(true) + setts.set(textField.text!, forKey: "cityName") + } + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { textField.resignFirstResponder() + } + + private var setts = UserDefaults.standard + override func viewDidLoad() { + super.viewDidLoad() + if let name = setts.value(forKey: "cityName"){ + textField.text = String(describing: name) } - - private var setts = UserDefaults.standard - override func viewDidLoad() { - super.viewDidLoad() - if let name = setts.value(forKey: "cityName"){ - textField.text = String(describing: name) - } - else{ - textField.text = "Saint Petersburg" - } + else{ + textField.text = "Saint Petersburg" } - - + } + }