Last active
August 8, 2022 11:01
-
-
Save Bahallack/af70c7b95150dec53e83d5f2dc94817d to your computer and use it in GitHub Desktop.
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
| // | |
| // WeatherAPIClient.swift | |
| // WeatherApp | |
| // | |
| // Created by Bahalek on 2021-12-25. | |
| // | |
| import Foundation | |
| import CoreLocation | |
| import SwiftUI | |
| final class WeatherAPIClient: NSObject, ObservableObject, CLLocationManagerDelegate { | |
| @Published var currentWeather: Weather? | |
| private let locationManager = CLLocationManager() | |
| private let dateFormatter = ISO8601DateFormatter() | |
| override init() { | |
| super.init() | |
| locationManager.delegate = self | |
| requestLocation() | |
| } | |
| func fetchWeather() async { | |
| guard let location = locationManager.location else { | |
| requestLocation() | |
| return | |
| } | |
| guard let url = URL(string: "https://api.tomorrow.io/v4/timelines?location=\(location.coordinate.latitude),\(location.coordinate.longitude)&fields=temperature&fields=weatherCode&units=metric×teps=1h&startTime=\(dateFormatter.string(from: Date()))&endTime=\(dateFormatter.string(from: Date().addingTimeInterval(60 * 60)))&apikey={$YOUR_KEY}") else { | |
| return | |
| } | |
| do { | |
| let (data, _) = try await URLSession.shared.data(from: url) | |
| if let weatherResponse = try? JSONDecoder().decode(WeatherModel.self, from: data), | |
| let weatherValue = weatherResponse.data.timelines.first?.intervals.first?.values, | |
| let weatherCode = WeatherCode(rawValue: "\(weatherValue.weatherCode)") { | |
| DispatchQueue.main.async { [weak self] in | |
| self?.currentWeather = Weather(temperature: Int(weatherValue.temperature), | |
| weatherCode: weatherCode) | |
| } | |
| } | |
| } catch { | |
| // handle the error | |
| } | |
| } | |
| private func requestLocation() { | |
| locationManager.requestWhenInUseAuthorization() | |
| locationManager.requestLocation() | |
| } | |
| func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
| Task { await fetchWeather() } | |
| } | |
| func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { | |
| // handle the error | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment