Last active
September 13, 2022 20:10
-
-
Save betty-godier/f846cfa3b3399dabfc746649edc206d1 to your computer and use it in GitHub Desktop.
Add a chart for hour forecast
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 Charts | |
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) | |
if let hourForecast { | |
ScrollView(.horizontal, showsIndicators: false) { | |
HStack { | |
ForEach(hourForecast.prefix(upTo: 24), id: \.date) { hour in | |
VStack { | |
Text(hour.date.formatted(.dateTime.hour())) | |
Image(systemName: hour.symbolName) | |
.frame(width: 50, height: 30) | |
Text(hour.temperature.narrowFormatted()) | |
} | |
} | |
} | |
.padding() | |
} | |
.background(.black.opacity(0.2)) | |
.clipShape(RoundedRectangle(cornerRadius: 20)) | |
.padding(.horizontal) | |
VStack { | |
Chart(hourForecast.prefix(upTo: 12), id: \.date) { hour in | |
AreaMark(x: .value("Time", hour.date), | |
y: .value("Chance of rain", hour.precipitationChance)) | |
} | |
.frame(height: 100) | |
.foregroundStyle(.cyan.gradient) | |
.chartYAxis(.hidden) | |
} | |
.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 | |
hourForecast = weather.hourlyForecast.filter { $0.date > .now } | |
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