diff --git a/Clima.xcodeproj/project.pbxproj b/Clima.xcodeproj/project.pbxproj index 076632ce..0ac7131f 100644 --- a/Clima.xcodeproj/project.pbxproj +++ b/Clima.xcodeproj/project.pbxproj @@ -7,18 +7,22 @@ objects = { /* Begin PBXBuildFile section */ + 18EE1B9E2BBC028F00F5C30D /* WeatherManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18EE1B9D2BBC028F00F5C30D /* WeatherManager.swift */; }; + 18EE1BA02BBC0CAF00F5C30D /* WeatherData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18EE1B9F2BBC0CAF00F5C30D /* WeatherData.swift */; }; + 18EE1BA22BBC2CD800F5C30D /* weatherModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18EE1BA12BBC2CD800F5C30D /* weatherModel.swift */; }; ADAA27B1231BBFAF00365194 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADAA27B0231BBFAF00365194 /* AppDelegate.swift */; }; ADAA27B3231BBFAF00365194 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADAA27B2231BBFAF00365194 /* SceneDelegate.swift */; }; ADAA27B5231BBFAF00365194 /* WeatherViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADAA27B4231BBFAF00365194 /* WeatherViewController.swift */; }; ADAA27B8231BBFAF00365194 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = ADAA27B6231BBFAF00365194 /* Main.storyboard */; }; ADAA27BA231BBFB300365194 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = ADAA27B9231BBFB300365194 /* Assets.xcassets */; }; ADAA27BD231BBFB300365194 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = ADAA27BB231BBFB300365194 /* LaunchScreen.storyboard */; }; - ADAA27C6231BC02C00365194 /* light_background.pdf in Resources */ = {isa = PBXBuildFile; fileRef = ADAA27C4231BC02B00365194 /* light_background.pdf */; }; - ADAA27C7231BC02C00365194 /* dark_background.pdf in Resources */ = {isa = PBXBuildFile; fileRef = ADAA27C5231BC02B00365194 /* dark_background.pdf */; }; ADAA27CE231BC87F00365194 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = ADAA27CD231BC87F00365194 /* README.md */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 18EE1B9D2BBC028F00F5C30D /* WeatherManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherManager.swift; sourceTree = ""; }; + 18EE1B9F2BBC0CAF00F5C30D /* WeatherData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherData.swift; sourceTree = ""; }; + 18EE1BA12BBC2CD800F5C30D /* weatherModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = weatherModel.swift; sourceTree = ""; }; ADAA27AD231BBFAF00365194 /* Clima.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Clima.app; sourceTree = BUILT_PRODUCTS_DIR; }; ADAA27B0231BBFAF00365194 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; ADAA27B2231BBFAF00365194 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -27,8 +31,6 @@ ADAA27B9231BBFB300365194 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; ADAA27BC231BBFB300365194 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; ADAA27BE231BBFB300365194 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - ADAA27C4231BC02B00365194 /* light_background.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = light_background.pdf; sourceTree = ""; }; - ADAA27C5231BC02B00365194 /* dark_background.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = dark_background.pdf; sourceTree = ""; }; ADAA27CD231BC87F00365194 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; /* End PBXFileReference section */ @@ -43,6 +45,16 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 18EE1B9C2BBC027B00F5C30D /* model */ = { + isa = PBXGroup; + children = ( + 18EE1B9D2BBC028F00F5C30D /* WeatherManager.swift */, + 18EE1B9F2BBC0CAF00F5C30D /* WeatherData.swift */, + 18EE1BA12BBC2CD800F5C30D /* weatherModel.swift */, + ); + path = model; + sourceTree = ""; + }; ADAA27A4231BBFAF00365194 = { isa = PBXGroup; children = ( @@ -63,16 +75,14 @@ ADAA27AF231BBFAF00365194 /* Clima */ = { isa = PBXGroup; children = ( + 18EE1B9C2BBC027B00F5C30D /* model */, ADAA27B0231BBFAF00365194 /* AppDelegate.swift */, ADAA27B2231BBFAF00365194 /* SceneDelegate.swift */, - ADAA27CA231BC3FA00365194 /* Model */, ADAA27C9231BC3F200365194 /* View */, ADAA27C8231BC3E500365194 /* Controller */, ADAA27B9231BBFB300365194 /* Assets.xcassets */, ADAA27BB231BBFB300365194 /* LaunchScreen.storyboard */, ADAA27BE231BBFB300365194 /* Info.plist */, - ADAA27C5231BC02B00365194 /* dark_background.pdf */, - ADAA27C4231BC02B00365194 /* light_background.pdf */, ); path = Clima; sourceTree = ""; @@ -93,13 +103,6 @@ path = View; sourceTree = ""; }; - ADAA27CA231BC3FA00365194 /* Model */ = { - isa = PBXGroup; - children = ( - ); - path = Model; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -159,11 +162,9 @@ buildActionMask = 2147483647; files = ( ADAA27CE231BC87F00365194 /* README.md in Resources */, - ADAA27C7231BC02C00365194 /* dark_background.pdf in Resources */, ADAA27BD231BBFB300365194 /* LaunchScreen.storyboard in Resources */, ADAA27BA231BBFB300365194 /* Assets.xcassets in Resources */, ADAA27B8231BBFAF00365194 /* Main.storyboard in Resources */, - ADAA27C6231BC02C00365194 /* light_background.pdf in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -175,7 +176,10 @@ buildActionMask = 2147483647; files = ( ADAA27B5231BBFAF00365194 /* WeatherViewController.swift in Sources */, + 18EE1BA02BBC0CAF00F5C30D /* WeatherData.swift in Sources */, ADAA27B1231BBFAF00365194 /* AppDelegate.swift in Sources */, + 18EE1B9E2BBC028F00F5C30D /* WeatherManager.swift in Sources */, + 18EE1BA22BBC2CD800F5C30D /* weatherModel.swift in Sources */, ADAA27B3231BBFAF00365194 /* SceneDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Clima.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/Clima.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 00000000..c789ad42 Binary files /dev/null and b/Clima.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Clima.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist b/Clima.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 00000000..ab521101 --- /dev/null +++ b/Clima.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + Clima.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/Clima/Assets.xcassets/background.imageset/Contents.json b/Clima/Assets.xcassets/background.imageset/Contents.json index 790c09f1..d5790754 100644 --- a/Clima/Assets.xcassets/background.imageset/Contents.json +++ b/Clima/Assets.xcassets/background.imageset/Contents.json @@ -1,21 +1,35 @@ { "images" : [ { - "idiom" : "universal", - "filename" : "background.png", - "scale" : "1x" + "filename" : "light_background.pdf", + "idiom" : "universal" }, { - "idiom" : "universal", - "scale" : "2x" + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "light" + } + ], + "filename" : "light_background 1.pdf", + "idiom" : "universal" }, { - "idiom" : "universal", - "scale" : "3x" + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "filename" : "dark_background.pdf", + "idiom" : "universal" } ], "info" : { - "version" : 1, - "author" : "xcode" + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "original" } -} \ No newline at end of file +} diff --git a/Clima/Assets.xcassets/background.imageset/background.png b/Clima/Assets.xcassets/background.imageset/background.png deleted file mode 100644 index 22e31fdb..00000000 Binary files a/Clima/Assets.xcassets/background.imageset/background.png and /dev/null differ diff --git a/Clima/dark_background.pdf b/Clima/Assets.xcassets/background.imageset/dark_background.pdf similarity index 100% rename from Clima/dark_background.pdf rename to Clima/Assets.xcassets/background.imageset/dark_background.pdf diff --git a/Clima/light_background.pdf b/Clima/Assets.xcassets/background.imageset/light_background 1.pdf similarity index 100% rename from Clima/light_background.pdf rename to Clima/Assets.xcassets/background.imageset/light_background 1.pdf diff --git a/Clima/Assets.xcassets/background.imageset/light_background.pdf b/Clima/Assets.xcassets/background.imageset/light_background.pdf new file mode 100644 index 00000000..1f3a3e7e Binary files /dev/null and b/Clima/Assets.xcassets/background.imageset/light_background.pdf differ diff --git a/Clima/Controller/WeatherViewController.swift b/Clima/Controller/WeatherViewController.swift index 038c2bba..a25ea8a3 100644 --- a/Clima/Controller/WeatherViewController.swift +++ b/Clima/Controller/WeatherViewController.swift @@ -7,18 +7,64 @@ // import UIKit +import CoreLocation -class WeatherViewController: UIViewController { - +class WeatherViewController: UIViewController, UITextFieldDelegate, WeatherManagerDelegate { + + @IBOutlet weak var conditionImageView: UIImageView! @IBOutlet weak var temperatureLabel: UILabel! @IBOutlet weak var cityLabel: UILabel! + @IBOutlet weak var searchTextField: UITextField! + var weatherManager = WeatherManager() + let locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + weatherManager.delegate = self + searchTextField.delegate = self + } - - + + @IBAction func searchPressed(_ sender: UIButton) { + print(searchTextField.text!) + } + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + searchTextField.endEditing(true) + print(searchTextField.text!) + return true + } + func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { + if textField.text != ""{ + return true + } else{ + textField.placeholder = "Type Something" + return false + } + + } + func textFieldDidEndEditing(_ textField: UITextField) { + if let city = searchTextField.text{ + weatherManager.FetchWeather(cityName: city) + + } + searchTextField.text = "" + } + + func didUpdateWeather(_ weatherManager: WeatherManager, weather: WeatherModel){ + print(weather.tempertureString) + DispatchQueue.main.async { + self.temperatureLabel.text = weather.tempertureString + self.cityLabel.text = weather.cityName + self.conditionImageView.image = UIImage(systemName: weather.conditionName) + } + + + + + } + func didFailWithError(error: Error) { + print(error) + } + } - diff --git a/Clima/Info.plist b/Clima/Info.plist index b5f9c079..79e0d632 100644 --- a/Clima/Info.plist +++ b/Clima/Info.plist @@ -2,6 +2,8 @@ + NSLocationAlwaysAndWhenInUseUsageDescription + Clime need to your location for show weather data never you CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable diff --git a/Clima/View/Base.lproj/Main.storyboard b/Clima/View/Base.lproj/Main.storyboard index fec3bb4e..8c83bd67 100644 --- a/Clima/View/Base.lproj/Main.storyboard +++ b/Clima/View/Base.lproj/Main.storyboard @@ -1,9 +1,11 @@ - - + + - + + + @@ -19,47 +21,50 @@ - + - - + - - + - + @@ -84,7 +89,7 @@ - + @@ -93,7 +98,8 @@ - + + @@ -104,11 +110,11 @@ - + @@ -118,9 +124,18 @@ - - - - + + + + + + + + + + + + + diff --git a/Clima/model/WeatherData.swift b/Clima/model/WeatherData.swift new file mode 100644 index 00000000..779766b4 --- /dev/null +++ b/Clima/model/WeatherData.swift @@ -0,0 +1,22 @@ +// +// WeatherData.swift +// Clima +// +// Created by Apple on 02/04/24. +// Copyright © 2024 App Brewery. All rights reserved. +// + +import Foundation +struct WeatherData: Codable { + let name: String + let main: Main + let weather: [Weather] +} +struct Main : Codable{ + let temp : Double +} + +struct Weather : Codable{ + let description: String + let id: Int +} diff --git a/Clima/model/WeatherManager.swift b/Clima/model/WeatherManager.swift new file mode 100644 index 00000000..0474f7ca --- /dev/null +++ b/Clima/model/WeatherManager.swift @@ -0,0 +1,80 @@ +// +// WeatherManager.swift +// Clima +// +// Created by Apple on 02/04/24. +// Copyright © 2024 App Brewery. All rights reserved. +// + +import Foundation + + +protocol WeatherManagerDelegate { + func didUpdateWeather(_ weatherManager: WeatherManager, weather: WeatherModel) + func didFailWithError(error: Error) +} + + +struct WeatherManager { + let weatherUrl = "https://api.openweathermap.org/data/2.5/weather?appid=2fd7b32644704492ca30b84bb95736ba&units=metric&" + var delegate: WeatherManagerDelegate? + func FetchWeather(cityName: String) { + let urlString = "\(weatherUrl)q=\(cityName)" + print(urlString) + performRequest(with: urlString) + } + func performRequest(with urlString: String) { +// Create a Url + if let url = URL(string: urlString){ +// 2. create a URLSession + let session = URLSession(configuration: .default) +// Give the session a task + let task = session.dataTask(with: url) { (data, response, error) in + if error != nil { + delegate?.didFailWithError(error: error!) + print(error!) + return + } + if let safeData = data{ + print("Safe Data") +// print() + if let weather = self.parseJSon(safeData){ + print(weather) + self.delegate?.didUpdateWeather(self, weather: weather) +// let weatherVC = WeatherViewController() +// weatherVC.didUpdateWeather(weather: weather) + } + } + + } + task.resume() + + } + + + } + func parseJSon(_ weatherData : Data) -> WeatherModel?{ + let decoder = JSONDecoder() + do { + print("parseJSon") + let decoderData = try decoder.decode(WeatherData.self, from: weatherData) + print(decoderData) + let id = decoderData.weather[0].id + let temp = decoderData.main.temp + let name = decoderData.name + let weather = WeatherModel(conditionId: id, cityName: name, temperature: temp) + print(weather.conditionName) + print(weather.tempertureString) + return weather + + } catch { + print("parseJSon error") + print(error) + delegate?.didFailWithError(error: error) + return nil + } + + + } + +} diff --git a/Clima/model/weatherModel.swift b/Clima/model/weatherModel.swift new file mode 100644 index 00000000..a27bff66 --- /dev/null +++ b/Clima/model/weatherModel.swift @@ -0,0 +1,42 @@ +// +// weatherModel.swift +// Clima +// +// Created by Apple on 02/04/24. +// Copyright © 2024 App Brewery. All rights reserved. +// + +import Foundation +struct WeatherModel { + + let conditionId : Int + let cityName : String + let temperature: Double + + var tempertureString: String { + return String(format: "%.1f", temperature) + } + + var conditionName: String{ + switch conditionId { + case 200...232: + return "cloud.bolt" + case 300...321: + return "cloud.drizzle" + case 500...531: + return "cloud.rain" + case 600...622: + return "cloud.snow" + case 701...781: + return "cloud.fog" + case 800: + return "sun.max" + case 801...804: + return "cloud.bolt" + default: + return "cloud" + } + } + + +}