Created
September 13, 2022 18:02
-
-
Save betty-godier/c621e94ca3955036e21f785c6718a925 to your computer and use it in GitHub Desktop.
Make a property to store the information you want to read
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
import CoreLocation | |
import SwiftUI | |
import WeatherKit | |
extension Measurement where UnitType == UnitTemperature { | |
func narrowFormatted() -> String { | |
self.formatted(.measurement(width: .narrow)) | |
} | |
} | |
struct ContentView: View { | |
@State private var currentSymbol: String? | |
@State private var conditions: String? | |
@State private var currentTemperature: Measurement<UnitTemperature>? | |
@State private var todayHighTemperature: Measurement<UnitTemperature>? | |
@State private var todayLowTemperature: Measurement<UnitTemperature>? | |
@State private var hourForecast: [HourWeather]? | |
var body: some View { | |
ScrollView { | |
VStack { | |
if let currentSymbol { | |
Image(systemName: currentSymbol) | |
.font(.system(size: 200, weight: .light)) | |
} | |
if let currentTemperature { | |
Text(currentTemperature.formatted()) | |
.font(.system(size: 110, weight: .bold)) | |
} | |
if let conditions { | |
Text(conditions) | |
.font(.title) | |
} | |
if let todayLowTemperature, let todayHighTemperature { | |
Text("L: \(todayLowTemperature.narrowFormatted()), H: \(todayHighTemperature.narrowFormatted())") | |
} | |
} | |
.frame(maxWidth: .infinity) | |
.padding() | |
.background(.black.opacity(0.2)) | |
.clipShape(RoundedRectangle(cornerRadius: 20)) | |
.padding(.horizontal) | |
} | |
.background(.linearGradient(colors: [.blue, Color(hue: 0.6, saturation: 0.8, brightness: 0.5)], startPoint: .top, endPoint: .bottom)) | |
.preferredColorScheme(.dark) | |
.symbolVariant(.fill) | |
.symbolRenderingMode(.multicolor) | |
.task { | |
loadWeather() | |
} | |
} | |
func loadWeather() { | |
Task { | |
let location = CLLocation(latitude: 28.67, longitude: 77.23) | |
let weather = try await WeatherService.shared.weather(for: location) | |
currentSymbol = weather.currentWeather.symbolName | |
conditions = weather.currentWeather.condition.description | |
currentTemperature = weather.currentWeather.temperature | |
if let today = weather.dailyForecast.first { | |
todayLowTemperature = today.lowTemperature | |
todayHighTemperature = today.highTemperature | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment